使用编译查询时我们还需要存储过程吗?

发布于 2024-09-25 01:12:57 字数 466 浏览 0 评论 0原文

当实体框架(或 linq-to-sql)中的编译查询与 SQL Server 结合使用时,使用存储过程实际上仍然有任何性能优势吗?

已编译的查询将被缓存为参数化查询,因此性能应接近等于存储过程。是否有任何情况下存储过程的性能会显着提高?

- 编辑 -

作为对下面 Yakimych 的回答的回应,我并不是想暗示编译的查询与存储过程相同。我试图弄清楚如果您已经在应用程序端完成了所有可能的优化(在本例中是编译查询),存储过程是否仍然是必要的。所以我想我正在寻找存储过程比应用程序端优化和参数化查询的组合更好的原因(这实际上是编译查询的本质)。

我问这个问题的原因之一是因为有很多人似乎认为由于不同的原因不再需要存储过程(即 这篇文章)。

When using compiled queries in entity framework (or linq-to-sql) in combination with SQL Server, is there actually still any performance benefit in using stored procedures?

Compiled queries will be cached as parameterized queries, so performance should be near equal to stored procedures. Is there any situation where stored procedures would perform significantly better?

-- EDIT --

In response to Yakimych's answer below, I didn't mean to imply that compiled queries are the same as stored procedures. I am trying to figure out if sprocs are still necessary if you have done all possible optimizations on the application side (in this case compiled queries). So I guess I'm looking for reasons why a stored procedure would be better than the combination of application-side optimizations and parameterized queries (which is what compiled queries effectively are).

One of the reasons I'm asking this, is because there are many people who seem to think that stored proedures are no longer necessary for different reasons (i.e. this post).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就像说晚安 2024-10-02 01:12:57

首先,编译 EF 查询与使用存储过程可以获得的性能优势无关。

根据 http://msdn.microsoft.com/en-us/library/cc853327 .aspx - 当针对概念模型执行查询时,会发生以下操作:

  • 正在加载元数据
  • 打开数据库连接
  • 生成视图
  • 准备查询
  • 执行查询
  • 加载和验证类型
  • 跟踪
  • 具体化对象

关于准备查询的说明:

包括编写查询命令、基于模型和映射元数据生成命令树以及定义返回数据的形状的成本。由于 Entity SQL 查询命令被缓存,因此稍后执行同一查询所需的时间甚至更少。您还可以使用编译的 LINQ 查询来降低以后执行中的成本。

因此,如果您编译该查询并稍后重新使用它,则会在每次后续查询执行期间节省应用程序中此操作的时间。但是,您不会影响针对数据库执行的生成的 SQL 代码。 编译查询时获得的性能优势是在应用程序级别。

另一方面,如果您对生成的 SQL 代码不满意并希望优化性能,则通常会使用存储过程在数据库级别。

编辑以回应您的评论和编辑。

在我看来,您的印象是编译 EF 查询会以某种方式更改将针对数据库运行的生成的 SQL 代码(您提到编译的查询会导致参数化 SQL 查询?)。事实并非如此。无论您是直接运行查询还是使用compiledQuery.Invoke,都会针对数据库运行相同的SQL代码。此外,您无法完全控制它,您宁愿依赖 ORM 以最佳方式生成它。在某些情况下,它并不是最优的,这就是 SP 的用武之地。

所以总结一下:

  • 编译查询纯粹是一个
    应用端优化。它
    节省编译查询的时间
    在代码中被重用。
  • 存储过程可用于调整 SQL 代码并使其尽可能符合您的目标,从而提供在数据库级别获得最佳性能的可能性。

任何一种技术都不能替代另一种技术。

First of all, compiling EF queries has nothing to do with performance benefits that can be achieved by using stored procedures.

According to http://msdn.microsoft.com/en-us/library/cc853327.aspx - the following operations occur when a query executes against a conceptual model:

  • Loading metadata
  • Opening the database connection
  • Generating views
  • Preparing the query
  • Executing the query
  • Loading and validating types
  • Tracking
  • Materializing the objects

And the explanation regarding Preparing the query:

Includes the costs to compose the query command, generate a command tree based on model and mapping metadata, and define the shape of the returned data. Because Entity SQL query commands are cached, later executions of the same query take even less time. You can also use compiled LINQ queries to reduce this cost in later executions.

So, if you compile the query and re-use it later, what happens is that you save time on this operation in your application during every subsequent query execution. What you don't do, however, is you don't influence the generated SQL code that is executed against the database. The performance benefits you get when compiling queries are at the application level.

On the other hand, you would typically use stored procedures in case you aren't satisfied with the generated SQL code and would like to optimize performance at the database level.

EDIT in response to your comment and edit.

It seems to me that you are under the impression that compiling an EF query would somehow change the generated SQL code that will be run against the database (you mention that compiled queries result in parametrized SQL queries?). That is not the case. No matter whether you run the query directly or use compiledQuery.Invoke, the same SQL code will be run against the DB. Furthermore, you don't have full control over it, you rather rely on the ORM to generate it in the best possible way. In some cases it is not optimal, and this is where SP's come in.

So to sum up:

  • Compiling queries is purely an
    application-side optimization. It
    saves the time to compile a query
    that gets re-used in the code.
  • Stored procedures can be used to tweak your SQL code and get it to match your goal as closely as possible, thus providing a possibility to get the best performance at the database level.

In no way is one technique a substitute for the other.

寄居者 2024-10-02 01:12:57

“是否有任何情况下存储过程的性能会显着提高?”

给定在 EF 或存储过程中生成的一段类似的参数化 SQL,它们的性能将相同。

然而,DBA 总是有机会根据他们对数据库模式及其使用模式的经验来进一步优化查询。存储过程允许他们在独立于使用它的应用程序的情况下轻松地完成此操作,而 ORM 则不然。

我们有一个极其复杂的 SQL Server 数据库,其中有许多外部系统通过触发器复制数据。对于 EF 来说,我们面临的问题是,在使用任何 ORM 而不是 DBA 时,应用程序开发人员将对在 DB 上触发的 SQL 负责。

"Is there any situation where stored procedures would perform significantly better?"

Given a comparable piece of parametrized SQL generated in either EF or a stored proc, they will perform equally.

However, a DBA always has the opportunity to further optimise a query based on their experience with the DB schema and its usage patterns. A stored procedure allows them to do this easily in isolation of the applications using it, whereas an ORM doesn't.

We have an extremely complicated SQL Server DB that has many external systems replicating data in and out via triggers. The issue for us with EF is that the responsibility for the SQL that gets fired at the DB will become the application developers responsibility when using any ORM rather than the DBAs.

独守阴晴ぅ圆缺 2024-10-02 01:12:57

来自知名专家的一些固定答案:Paul Nielsen 为什么要使用存储过程?亚当

·马哈尼克:

A few canned answers from well known experts: Paul Nielsen Why use Stored Procedures?

Adam Machanic: No, stored procedures are NOT bad

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文