简体   繁体   English

LINQ to SQL与存储过程的效率

[英]Efficiency of linq to sql vs stored procedure

Hi I'm writing a app which has a search page and does a search on the database. 嗨,我正在编写一个具有搜索页面并在数据库上进行搜索的应用程序。

I'm wondering whether I should do this in linq or a stored procedure. 我想知道是否应该在linq或存储过程中执行此操作。

Is the performance of a stored procedure much better than that of linq to sql? 存储过程的性能是否比linq to sql好得多?

I'm thinking it would be because in order to write the linq query you need to use the datacontext to access the table on which to query. 我在想这是因为要编写linq查询,您需要使用datacontext访问要查询的表。 I'm imagining this in itself means that if the table is big it might become inefficient. 我在想这本身意味着如果表很大,可能会变得效率低下。

That is if you were using: 也就是说,如果您使用的是:

context.GetTable<T>();

Can any one advise me here? 有人可以在这里建议我吗?

There is unlikely to be much difference UNLESS you encounter a situation where the TSQL produced by Linq to SQL is not optimal. 除非您遇到Linq to SQL生成的TSQL不是最佳的情况,否则差异不会太大。

If you want absolute control over the TSQL use a stored procedure. 如果要对TSQL进行绝对控制,请使用存储过程。

If speed is critical, benchmark both and also examine the TSQL produced by your Linq to SQL solution. 如果速度至关重要,则既要进行基准测试,也要检查Linq to SQL解决方案产生的TSQL。

Also, you should be wary of pulling back entire tables (unless they are small, such as frequently accessed lookup data) across the wire in either solution. 另外,在任何一种解决方案中,您都应该警惕在整个线路上拉回整个表(除非它们很小,例如经常访问的查询数据)。

If the speed is so critical to you then you should go ahead and benchmark both options on a reasonable set of data. 如果速度对您而言至关重要,则应继续进行,并根据一组合理的数据对这两个选项进行基准测试。 Technically I would expect the SP to be faster but it might not be that much of a difference. 从技术上讲,我希望SP更快,但可能没有太大的区别。

What does "efficient" mean to you? “有效”对您意味着什么?

I'm working on a website where sub seconds (preferably sub 500ms) is the goal. 我正在一个网站上,目标是不到几秒(最好不到500毫秒)。 We're using Linq for search on most of our stuff. 我们正在使用Linq来搜索大部分内容。 The only time we're actually using a SP is when we're using the hierarchyid and other SqlServer data types that don't exist in EF. 我们真正使用SP的唯一时间是当我们使用EF中不存在的hierarchyid和其他SqlServer数据类型时。

GetTable probably isn't going to be that different between the two, as fundamentally it's just SELECT * FROM T. You'll see more significant gains from stored procedures in cases where the query isn't being written very optimally by Linq, or in some very high load situations were caching the execution plan makes a difference. GetTable可能在两者之间不会有太大的不同,因为从根本上来说,它只是SELECT * FROMT。在查询不是由Linq或以最佳方式编写的情况下,您将从存储过程中获得更大的收益。在某些非常高负载的情况下,缓存执行计划会有所作为。

Benchmarking it is the best answer, but from what it looks like you're doing I don't think the difference is going to amount to much. 对它进行基准测试是最好的答案,但是从您的行为看来,我认为差异不会太大。

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

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