LINQ到SQL优化查询 [英] Linq-To-Sql optimization for queries

查看:206
本文介绍了LINQ到SQL优化查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我建立一个网络应用程序,它搜索其 SQL 数据库用户输入查询。一个简单的搜索引擎

提出让我有很多行 - 以上几十万。我想拨打电话效率。

我知道,我的电话是可能只有一个* SQL 查询。我想知道,如果的LINQ到SQL 做这种类型的优化。

我知道它使用懒人其查询。我现在可以,因为我没有在我的数据库足够多的数据未基准它。

 的foreach(VAR字字)
{
VAR niqquh =号== 666? :语言[数字];
变种S = db.Uploads.Where(W =>若干== 666真:?(w.Language == niqquh));
如果(S.Count()→20)
S = S.Take(20);
S = S.OrderBy(U =>(u.Tags.Contains(字)15:0)+(u.Name.Contains(字)10:0)+(u.Description.Contains? (字)5:0));
listOfList.Add(S.ToList());
}





正如你看到这里,我有幻数 666 这是习惯说whatevery语言是确定的。

之后,我想只取前20个元素,所以我称之为计数()然后

然后,我对结果进行排序我的需求。

的一点是,我的认为的是,表达得到在评估计数 - 因此它不能被优化包含的只有的前20排在 SQL查询水平 - 但客户端的( ASP.NET)的水平。所以,我基本上整个数据库下载到客户端,这是可怕的。


是否得到优化与否,如果没有,我怎么能做到这一点的一种有效的方式,即使我有回到平原 SQL 字符串声明?



*一个每个


解决方案

 <$ C查询$ C>如果(S.Count()→20)

这运行一个查询,没错。 ..

 
SELECT COUNT(*)

所以你没有下载整个表。






  S = S.Take(20); 

这不执行查询。它不会对(无序)查询限制为20个项目。一般来说,你希望你限制结果之前申请顺序。



有没有异常,如果你需要更多的比是存在的。你只得到较少的项目。正因为如此,伯爵是没有必要的。






  S.ToList() ; 

这运行的查询。






 的IQueryable<&上传GT;查询= db.Uploads; 
如果
{
VAR niqquh =语言[数字](数= 666!);
=查询query.Where(W => w.Language == niqquh);
}
=查询查询
.OrderBy(U =>(u.Tags.Contains(字)15:0)+(u.Name.Contains(字)10:?? 0)+(u.Description.Contains(字)5:0))
。取(20);
listOfList.Add(query.ToList());



I am building a web application which searches its SQL database for the query entered by the user. A simple search engine.
Lets propose I have plenty of rows - above a couple of thousands. I want to make the call efficient.
I know that my call is possible with only one* SQL query. I would like to know if Linq-To-Sql does this type of optimization.
I am aware the it uses Lazy for its queries. I cannot benchmark it now because I don't have enought data on my database.

    foreach(var word in words)
    {
        var niqquh = number == 666 ? "" : Languages[number];
        var S = db.Uploads.Where(w => number == 666 ? true : (w.Language == niqquh));
        if(S.Count() > 20)
            S = S.Take(20);
        S = S.OrderBy(u => (u.Tags.Contains(word) ? 15 : 0) + (u.Name.Contains(word) ? 10 : 0) + (u.Description.Contains(word) ? 5 : 0));
        listOfList.Add(S.ToList());
    }


As you see here, I have the 'magic number' 666 which is used to say "whatevery language is ok".
Then, I want to take only the top 20 elements, so I call Count() and then Take.
Then, I sort the results by my needs.
The point is, that I think that the lazy expression gets evaluated at Count - therefore it cannot be optimized to contain only the top 20 rows at the SQL Query level - but on the client's (ASP.NET) level. therefore, I basically download the whole database to the client, which is terrible.

Does it get optimized or not, if it doesn't, how can I do it in an efficient way, even if I have to go back to plain SQL string statements?

*A query for each word

解决方案

if(S.Count() > 20) 

This runs a query, yes...

SELECT COUNT(*) FROM

So you didn't download the whole table.


S = S.Take(20);

This does not execute the query. It does limit the (unordered) query to 20 items. Generally you want to apply order before you limit the result.

There is no exception if you Take more than is there. You'll just get fewer items. Because of this, the Count is not needed.


S.ToList();

This runs a query.


IQueryable<Upload> query = db.Uploads;
if (number != 666)
{
   var niqquh = Languages[number];
   query = query.Where(w => w.Language == niqquh);
}
query = query
  .OrderBy(u => (u.Tags.Contains(word) ? 15 : 0) + (u.Name.Contains(word) ? 10 : 0) + (u.Description.Contains(word) ? 5 : 0))
  .Take(20);
listOfList.Add(query.ToList()); 

这篇关于LINQ到SQL优化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆