决定使用 LINQ to SQL 与 StoredProcs
在开发应用程序时,我通常会使用存储过程来包含CRUD逻辑,以提高性能和可维护性。但在尝试 LINQ to SQL 后,我想知道使用对存储过程编译的 LINQ-to-SQL 查询是否有助于提高性能?
While developing applications, I usually go for Stored Procedures to contain CRUD logic, so as improve performance and maintainability. But after experimenting with LINQ to SQL, I was wondering whether, using compiled LINQ-to-SQL queries over stored procedures will that help improve performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ to SQL 不会提高性能,因为您将通过线路将每个 CRUD 操作作为字符串发送。
使用存储过程的性能仍然会更好,但像 Linq to SQL 这样的 ORM 通常可以缩短开发时间。
LINQ to SQL will not improve your performance, because you will be sending each CRUD operation as a string over the wire.
Performance will still be better with Stored Procedures, but ORM's like Linq to SQL usually make development time faster.
根据我的经验,我可以对性能进行如下排名:
ESQL< /p>
2,3,4 可能会根据查询的性质改变它们的顺序,但一般来说,原始 sql 查询的执行速度会更快。
From my experience, I can rank performance as following:
ESQL
2,3,4 may change their order depends on the nature of the queries, but in general raw sql query is executed fater.
根据您对 DevSlick 和 a1ex07 的评论,您似乎对 LINQ 存在根本性的误解。为了让 LINQ 查询允许链接,
LINQ 查询的执行必须是 延迟直到枚举:
这意味着查询
.Where(o => o.Active).OrderBy(o => o.Ordering)。 Select(o => o.Name)
在此之前您的计算机实际上也不会解释。如果您运行相同的查询 100 次,这意味着计算机必须重新解释该查询 100 次。对于 LINQ-to-SQL,这意味着在每次将 SQL 发送到数据库之前将查询转换为 SQL 100 次,即使每次 SQL 都完全相同。提前编译查询会导致它仅生成一次 SQL,并在每次调用查询时使用该 SQL。这与存储过程无关 - 您可以像编译任何其他查询一样将查询编译为存储过程。询问“哪个提供更好的性能”是没有意义的,因为它们并不相互排斥。
尽管编译查询听起来像是一件好事,但实际上解释 LINQ 查询(通常称为“评估表达式树”)与实际对数据库执行 SQL 相比只需要很少的时间,因此您对于编译查询来说几乎没有什么好处。同时,编译查询的语法是atrocious:
为此出于原因,通常建议不要编译 LINQ-to-SQL 查询,因为代码噪音与效益的比率非常糟糕。
Based on your comments to both DevSlick and a1ex07, it seems you have a fundamental misunderstanding of what LINQ is. In order for LINQ queries to allow chaining, like
the execution of the LINQ query must be delayed until it is enumerated:
This means that the query
.Where(o => o.Active).OrderBy(o => o.Ordering).Select(o => o.Name)
is not actually interpreted by your computer until that point as well. If you run the same query 100 times, that means the computer has to reinterpret that query 100 times. For LINQ-to-SQL, that means translating the query to SQL 100 times before that SQL is sent to the database each time, even if the SQL is exactly the same every time.Compiling the query ahead of time causes it to generate the SQL only once, and use that SQL every time the query is called. This has nothing to do with stored procedures - you would compile a query-to-a-stored-procedure in the same way that you would compile any other query. Asking "which gives better performance" is meaningless, as they are not mutually exclusive.
Though compiling a query sounds like a good thing, in practice interpreting a LINQ query (usually called "evaluating the expression tree") takes very very little time compared to actually executing the SQL against the database, so you get very little benefit for compiling the query. In the meanwhile, the syntax for compiling a query is atrocious:
For this reason, it is usually recommended to simply not compile your LINQ-to-SQL queries, because the ratio of code-noise-to-benefit is terrible.