Sto usando .NET framework 4.5.1 Linq to SQL.Query singola più lenta di 3 query
ho questa classe di prodotti utilizzando il codice prima:
public class Part
{
public int PartID { get; set; }
[Required(ErrorMessage = "xxx")]
public string Title { get; set; }
[MaxLength(50)]
[Index(IsClustered = false, IsUnique = false,Order =1)]
public string Part_Number { get; set; }
[MaxLength(50)]
[Index(IsClustered = false, IsUnique = false, Order = 2)]
public string Manufacturer_Number { get; set; }
}
ho circa 2,5 milioni di queste entità nel database.
Primo approccio
var query = db.Parts.Where(s => s.Manufacturer_Number == sstring).ToList();
query.AddRange(db.Parts.Where(s => s.Part_Number == sstring).ToList());
query.AddRange(db.Parts.Where(s => s.Title == sstring).ToList());
Secondo approccio
var query = db.Parts.Where(s => s.Manufacturer_Number == sstring
|| s.Part_Number == sstring || s.Title == sstring).ToList();
Il primo approccio è 100 volte più veloce rispetto al secondo approccio. Qualcuno può spiegarlo?
Si dovrebbe guardare SQL che viene prodotto dal proprio linq e quindi controllare il piano di esecuzione in ManagmentStudio. Potrebbe essere che tu abbia indici su ogni campo e le query semplici possono usarli ma non il tuo ultimo. –
Spero che abbia indici se hai 2.5 milioni di record in una tabella ... –
Cosa vuoi esattamente raggiungere ..? –