简体   繁体   English

Linq查询耗时太长

[英]Linq query taking too long

I wonder if someone can help me with this. 我想知道是否有人可以帮助我。 I have a LINQ query - no problems with it, but it takes far too long to return data 我有一个LINQ查询 - 没有问题,但返回数据需要太长时间

var result = Context.paf_wgs84.Where(c => c.Postcode.Contains(postcode)).Take(15);

Very simple, the idea as the user is typing, using AJAX, it returns a set of 15 possible matches. 很简单,用户输入的想法,使用AJAX,它返回一组15种可能的匹配。

The problem is, there are 1.6 million records 问题是,有160万条记录

Running the following code in management studio takes around 3 seconds 在管理工作室中运行以下代码大约需要3秒钟

SELECT   code
FROM     paf_wgs84
WHERE    (code LIKE '%EC1%')

where as running the following code takes less than a second 运行以下代码的时间不到一秒

SELECT   TOP 15  code
FROM     paf_wgs84
WHERE    (code LIKE '%EC1%')

Is there a way of doing something similar in LINQ without using .take() ? 有没有办法在不使用.take()情况下在LINQ中做类似的事情?

You can try something like this. 你可以尝试这样的事情。 This will only return one column. 这只会返回一列。

var result = Context.paf_wgs84.Where(c => c.Postcode.Contains(postcode)).Select(x=>new {x.Postcode}).Take(15);

The generated Sql statement will look like this. 生成的Sql语句如下所示。

/*
-- Region Parameters
DECLARE @p0 VarChar(1000) = '%s%'
-- EndRegion
SELECT TOP (15) [t0].[code]
FROM [paf_wgs84] AS [t0]
WHERE [t0].[code] LIKE @p0
*/

the problem might be that your contains method in the statemnt it is not being mapped to a like statement in sql and you end up getting all the rows into sql and then doing a cotains search in your web tier instead of doing the same in your DB. 问题可能是你在statemnt中的contains方法没有被映射到sql中的like语句,你最终得到所有的行进入sql,然后在你的web层进行cotains搜索,而不是在你的数据库中做同样的事情。

Use SqlMethods for the same.. somethig as follows: 使用SqlMethods相同.. somethig如下:

SqlMethods.Like(c.Postcode, string.Format("%{0}%",postcode));

sometimes you can also use the string methods like: String.StartsWith or String.Ends with but in this you cant.. 有时您也可以使用字符串方法,如:String.StartsWith或String.Ends,但在此你不能...

Also - LIKE clauses starting % are rarely a good idea - not least, it can't make effective use of any index. 另外 - 启动%的LIKE子句很少是一个好主意 - 尤其是,它无法有效地使用任何索引。 You might have better performance using "full text search"; 使用“全文搜索”可能会有更好的表现; but this isn't directly available via LINQ 但这不能通过LINQ直接获得

hope that helps your problem. 希望能帮到你解决问题。

The performance of these types of queries has been greatly improved in SQL Server 2012 using the new FETCH and OFFSET statements (although I haven't seen much benchmarking data to date...). SQL Server 2012中使用新的FETCH和OFFSET语句大大提高了这些类型查询的性能(尽管到目前为止我还没有看到太多基准测试数据......)。

The trouble is EF doesnt utilize these new features yet. 问题是EF还没有利用这些新功能。

A possible solution is to use SQL Server 2012 and write a sproc with FETCH/OFFSET and then target this using EF. 一种可能的解决方案是使用SQL Server 2012并使用FETCH / OFFSET编写一个sproc,然后使用EF进行目标。 Definitely not a short term solution but I wanted to point out this option. 绝对不是短期解决方案,但我想指出这个选项。

I altered the statement to 我把声明改成了

var result = Context.paf_wgs84.Where(c => c.Postcode.StartsWith(postcode)).Take(10).Select(c => c.Postcode).ToArray();

I used a foreach look to add to a string array - I think this is where I was losing time! 我使用foreach外观添加到字符串数组 - 我认为这是我失去时间的地方!

Thanks for everyones help though. 感谢大家的帮助。

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

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