我做上使用通配符(該contains
擴展名)數據庫表中的搜索下面是代碼LINQ與大型數據庫表提高性能SQL
// This gets a list of the primary key IDs in a table that has 5000+ plus records in it
List<int> ids = context.Where(m => m.name.ToLower().Contains(searchTerm.ToLower())).Select(m => m.Id).ToList();
// Loop through all the ids and get the ones that match in a different table (So basically the FKs..)
foreach (int idin nameId)
{
total.AddRange(context2.Where(x => x.NameID == id).Select(m => m.Id).ToList());
}
在有什麼我可以在LINQ改變那會導致ID更快?
感謝
在'context2'的item類中,是否存在對'name','context1'的item類的引用?換句話說,謂詞中可能有'x.Name.name'? – Yogu 2014-11-02 17:06:55
不,Context2具有Context1的PK的FKS,在COntext2中使用虛擬類型會更快嗎?所以'Context.name.where..' – user3428422 2014-11-02 17:10:42
我不認爲這會更快,但更具可讀性。 – Yogu 2014-11-02 17:16:36