[英]Linq to SQL Slow Query
My ASP.Net application has the following Linq to SQL function to get a distinct list of height values from the product table. 我的ASP.Net应用程序具有以下Linq to SQL函数,以从产品表中获取不同的高度值列表。
public static List<string> getHeightList(string catID)
{
using (CategoriesClassesDataContext db = new CategoriesClassesDataContext())
{
var heightTable = (from p in db.Products
join cp in db.CatProducts on p.ProductID equals cp.ProductID
where p.Enabled == true && (p.CaseOnly == null || p.CaseOnly == false) && cp.CatID == catID
select new { Height = p.Height, sort = Convert.ToDecimal(p.Height.Replace("\"", "")) }).Distinct().OrderBy(s => s.sort);
List<string> heightList = new List<string>();
foreach (var s in heightTable)
{
heightList.Add(s.Height.ToString());
}
return heightList;
}
}
I ran Redgate SQL Monitor which shows that this query is using a lot of resources. 我运行了Redgate SQL Monitor,它显示此查询正在使用大量资源。
Redgate is also showing that I am running the following query: Redgate还显示我正在运行以下查询:
select count(distinct [height]) from product p
join catproduct cp on p.productid = cp.productid
join cat c on cp.catid = c.catid
where p.enabled=1 and p.displayfilter = 1 and c.catid = 'C2-14'
My questions are: 我的问题是:
There are 90,000 records in the products. 产品中有90,000条记录。 This category which I am trying to get the distinct list of heights has 50,000 product records
我正在尝试获取高度的这个类别的类别有50,000个产品记录
Thank you in advance, 先感谢您,
Nick 缺口
First of all your posted sql query and linq query doesn't match at all. 首先,您发布的sql查询和linq查询根本不匹配。 it's not the LINQ query rather the underlying SQL query itself performing slow.
它不是LINQ查询,而是底层SQL查询本身执行缓慢。 Make sure, all the columns involved in
JOIN ON
clause and WHERE
clause and ORDER BY
clause are indexed properly in order to have a better execution plan; 确保对
JOIN ON
子句, WHERE
子句和ORDER BY
子句中涉及的所有列进行正确索引,以制定更好的执行计划; else you will end up getting a FULL Table Scan
and a File Sort
and query will deemed to perform slow. 否则,您将获得
FULL Table Scan
并且File Sort
和查询将被认为执行缓慢。
The join multiplies the number of Product
s the query returns. 联接乘以查询返回的
Product
的数量。 To undo that, you apply Distinct
at the end. 要撤消该操作,请在最后应用
Distinct
。 It will certainly reduce db resources if you return unique Product
s right away: 如果立即返回唯一的
Product
肯定会减少数据库资源:
var heightTable = (from p in db.Products
where p.CatProducts.Any(cp => cp.CatID == catID)
&& p.Enabled && (p.CaseOnly == null || !p.CaseOnly)
select new
{
Height = p.Height,
sort = Convert.ToDecimal(p.Height.Replace("\"", ""))
}).OrderBy(s => s.sort);
This changes the join
into a where
clause. 这会将
join
更改为where
子句。 It saves the db engine the trouble of deduplicating the result. 它为db引擎省去了对结果进行重复数据删除的麻烦。
If that still performs poorly, you should try to do the conversion and ordering in memory, ie after receiving the raw results from the database. 如果仍然无法令人满意,则应尝试在内存中进行转换和排序,即从数据库接收到原始结果之后。
As for the count. 至于数。 I don't know where it comes from.
我不知道它从哪里来。 Such queries typically get generated by paging libraries such as PagedList, but I see no trace of that in your code.
此类查询通常由诸如PagedList之类的页面库生成,但是我在您的代码中看不到任何痕迹。
Side note: you can return ... 旁注:您可以退货...
heightList.Select(x => x.Height.ToString()).ToList()
... instead of creating the list yourself. ...而不是自己创建列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.