简体   繁体   English

我如何加快这些linq查询?

[英]How can i speed up these linq queries?

Can i combine the following two linq queries in an single one, for speeding things up? 我可以将以下两个linq查询合并为一个,以加快处理速度吗?

The first one, searches and performs the paging 第一个,搜索并执行分页

 Products.Data = db.Products.Where(x =>  x.ProductCode.Contains(search) ||
                                         x.Name.Contains(search) || 
                                         x.Description.Contains(search) || 
                                         x.DescriptionExtra.Contains(search) ||
                                         SqlFunctions.StringConvert(x.Price).Contains(search) ||
                                         SqlFunctions.StringConvert(x.PriceOffer).Contains(search) ||
                                         SqlFunctions.StringConvert(x.FinalPrice).Contains(search) ||
                                         SqlFunctions.StringConvert(x.FinalPriceOffer).Contains(search))
                                         .OrderBy(p => p.ProductID)
                                         .Skip(PageSize * (page - 1))
                                         .Take(PageSize).ToList();

while the second one counts the total filtered results. 而第二个则计算过滤后的总结果。

    int count = db.Products.Where(x => x.ProductCode.Contains(search) ||
                                       x.Name.Contains(search) ||
                                       x.Description.Contains(search) ||
                                       x.DescriptionExtra.Contains(search) ||
                                       SqlFunctions.StringConvert(x.Price).Contains(search) ||
                                       SqlFunctions.StringConvert(x.PriceOffer).Contains(search) ||
                                       SqlFunctions.StringConvert(x.FinalPrice).Contains(search) ||
                                       SqlFunctions.StringConvert(x.FinalPriceOffer).Contains(search))
                                      .Count();

Get rid of your ridiculously inefficient conversions. 摆脱您可笑的低效转换。

SqlFunctions.StringConvert(x.Price).Contains(search) || SqlFunctions.StringConvert(x.Price)。包含(搜索)||

No index use possible, full table scan, plus a conversion - that is as bad as it gets. 不可能使用索引,进行全表扫描以及进行一次转换-那样糟。

And make sure you have all indices. 并确保您拥有所有索引。

Nothing else you can do. 您无能为力。

Stop using 'contains' function cause it's a very slow thing (if you can) 停止使用“包含”功能,因为这是一件非常缓慢的事情(如果可以的话)

Make sure that Your queries can make use of index`es in the DB. 确保您的查询可以使用数据库中的索引。 If You MUST have 'contains' - the take a look at full-text search capabilieties of SQL, but You might need to change this so pure sql or customise how your Linq is translated to SQL to make use of Full-Text indexes 如果您必须“包含”-查看SQL的全文本搜索功能,但是您可能需要更改此纯sql或自定义Linq如何转换为SQL以利用全文本索引

I do not think You can combine them directly. 我认为您不能直接将它们结合起来。 This is one problem of paging - You need to know the total count of result anyway. 这是分页的问题-无论如何,您都需要知道结果的总数。 Problem of dynamic paging further is, that one page can be inconsistent with another, because it is from different time. 动态分页的问题还在于,一个页面可能与另一页面不一致,因为它来自不同的时间。 You can easily miss item completely because of this. 因此,您可以轻松地完全错过项目。 If this can be a problem, I would avoid dynamic paging. 如果这可能是一个问题,我将避免动态分页。 You can fill ids of the whole result into some temporary table on server and do paging from there. 您可以将整个结果的ID填充到服务器上的某个临时表中,然后从那里进行分页。 Or You can return all ids from fulltext search and query the rest of data on demand. 或者,您可以从全文本搜索中返回所有ID,并按需查询其余数据。

There are some more optimizations, You can start returning results when the search string is at least 3 characters long, or You can build special table with count estimates for this purpose. 还有更多优化,您可以在搜索字符串至少3个字符长时开始返回结果,或者为此目的可以构建带有计数估计的特殊表。 You can also decide, that You would return only first ten pages and save server storage for ids (or client bandwidth for ids). 您还可以决定仅返回前十页,并为ID保存服务器存储(对于ID则保存客户端带宽)。

I am sad to see "Stop using contains" answers without alternative. 我很遗憾地看到答案,而不替代“使用含有停止”。 Searching in the middle of the words is many times a must. 必须在单词中间进行多次搜索。 The fact is, SQL server is terribly slow on text processing and searching is no exception. 事实是,SQL Server的文本处理速度非常慢,搜索也不例外。 AFAIK even full-text indexes would not help You much with in-the-middle substring searching. AFAIK甚至全文本索引在中间子字符串搜索中也无济于事。

For the presented query on 10k records I would expect about 40ms per query to get counts or all results (my desktop). 对于提出的关于1万条记录的查询,我希望每个查询大约需要40毫秒才能获得计数或所有结果(我的桌面)。 You can make computed persisted colum on this table with all texts concatenated and all numbers converted and query only that column. 您可以对所有文本进行级联并转换所有数字,然后对该表进行计算得出的持久化列,并且仅查询该列。 It would speed things up significantly (under 10ms for query on my desktop). 它将大大加快速度(在我的桌面上查询不到10毫秒)。

[computedCol]  AS (((((((((([text1]+' ')+[text2])+' ')+[text3])+' ')+CONVERT([nvarchar](max),[d1]))+' ')+CONVERT([nvarchar](max),[d2]))+' ')+CONVERT([nvarchar](max),[d3])) PERSISTED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM