I am using the .NET framework 4.5.1 Linq to SQL.
I have this product class using code first:
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; } }
I have approximately 2500000 of these objects in the database.
First approach
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());
Second approach
var query = db.Parts.Where(s => s.Manufacturer_Number == sstring || s.Part_Number == sstring || s.Title == sstring).ToList();
The first approach is 100 times faster than the second approach. Can anyone explain this?
Moshe pestov
source share