决定使用 LINQ to SQL 与 StoredProcs

发布于 2024-10-19 10:17:52 字数 122 浏览 1 评论 0原文

在开发应用程序时,我通常会使用存储过程来包含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 技术交流群。

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

发布评论

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

评论(3

醉南桥 2024-10-26 10:17:52

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.

薆情海 2024-10-26 10:17:52

根据我的经验,我可以对性能进行如下排名:

  1. 存储过程
  2. 本机查询(使用 DBCommand)
  3. Linq to 实体(编译查询,EF4)
  4. Linq to SQL(编译)
  5. Linq to 实体(未编译 EF4)
  6. Linq to SQL
  7. ESQL< /p>

    2,3,4 可能会根据查询的性质改变它们的顺序,但一般来说,原始 sql 查询的执行速度会更快。

From my experience, I can rank performance as following:

  1. Stored procedures
  2. Native queries (using DBCommand)
  3. Linq to entity (compiled query, EF4)
  4. Linq to SQL (compiled)
  5. Linq to entity (not compiled EF4)
  6. Linq to SQL
  7. ESQL

    2,3,4 may change their order depends on the nature of the queries, but in general raw sql query is executed fater.

寄人书 2024-10-26 10:17:52

根据您对 DevSlick 和 a1ex07 的评论,您似乎对 LINQ 存在根本性的误解。为了让 LINQ 查询允许链接,

var activePeople = peopleList.Where(o => o.Active).OrderBy(o => o.Ordering).Select(o => o.Name);

LINQ 查询的执行必须是 延迟直到枚举:

foreach(var person in activePeople)
{
   //If this is LINQ-to-SQL, the query to peopleList has waited until now to request anything from the database
}

这意味着查询.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

static readonly Func<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>> s_compiledQuery2 = 
    CompiledQuery.Compile<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>>(
        (ctx, total) => from order in ctx.SalesOrderHeaders
                        where order.TotalDue >= total
                        select order);

var orders = s_compiledQuery2.Invoke(context, totalDue);

为此出于原因,通常建议不要编译 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

var activePeople = peopleList.Where(o => o.Active).OrderBy(o => o.Ordering).Select(o => o.Name);

the execution of the LINQ query must be delayed until it is enumerated:

foreach(var person in activePeople)
{
   //If this is LINQ-to-SQL, the query to peopleList has waited until now to request anything from the database
}

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:

static readonly Func<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>> s_compiledQuery2 = 
    CompiledQuery.Compile<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>>(
        (ctx, total) => from order in ctx.SalesOrderHeaders
                        where order.TotalDue >= total
                        select order);

var orders = s_compiledQuery2.Invoke(context, totalDue);

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.

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