简体   繁体   English

SQL 服务器是否缓存 LINQ 到 SQL 查询?

[英]Does SQL Server cache LINQ to SQL queries?

As far as I know, in MS SQL Server 2000+ stored procedures are compiled, and run faster than normal non-compiled queries.据我所知,在 MS SQL Server 2000+ 中编译存储过程,并且比普通的非编译查询运行得更快。 I wonder if MS SQL Server also compiles LINQ to SQL queries, and caches that compilation, for performance purposes.我想知道 MS SQL 服务器是否也出于性能目的将 LINQ 编译为 SQL 查询和缓存该编译。

When it comes to the execution plan in SQL Server there is no major differences in speed with stored procedures vs normal non-compiled queries.当谈到 SQL 服务器中的执行计划时,存储过程与普通非编译查询的速度没有重大差异。 See this for more information about other factors about speed.有关速度的其他因素的更多信息, 请参阅此内容。

LINQ-to-SQL generates normal paramatised queries (SQL Server knows nothing about LINQ, the app generates normal SQL) so the balance is the same. LINQ-to-SQL 生成正常的参数化查询(SQL Server 对 LINQ 一无所知,应用程序生成正常的 SQL)所以余额是一样的。 That is if you aren't using the LINQ-to-SQL functions which call Stored Procs (assuming you meant the query stuff).也就是说,如果您不使用调用存储过程的 LINQ-to-SQL 函数(假设您的意思是查询内容)。

You may also want to read this , which is a similar question, but focuses on the more important benefits of LINQ vs StoredProcs您可能还想阅读这个,这是一个类似的问题,但侧重于 LINQ 与 StoredProcs 的更重要的好处

SQL Server doesn't know about LINQ. SQL 服务器不知道 LINQ。

As far as I know, LINQ runs just as a library that uses the queries to generate SQL queries that are then sent to the server as SQL text commands.据我所知,LINQ 作为一个库运行,它使用查询生成 SQL 查询,然后作为 SQL 文本命令发送到服务器。

CompiledQueries of LinQ get cached by SQL Server, that's why they are so much faster. LinQ 的 CompiledQueries 被 SQL 服务器缓存,这就是它们快得多的原因。 Normal LinQ Queries don't get cached by the SQL Server.正常 LinQ 查询不会被 SQL 服务器缓存。

For testing.用于检测。

Create a normal LinQ Query Create the same compiled LinQ query.创建一个普通的 LinQ 查询 创建相同的编译 LinQ 查询。

After the compiled query ran several time, clean the SQL Cache and you will notice that it will have almost the same execution time as the normal LinQ Query the first time.编译查询运行几次后,清理 SQL 缓存,您会注意到它的执行时间与第一次正常的 LinQ 查询几乎相同。

PS. PS。 You can clean the SQL Server cache with the query: DBCC DROPCLEANBUFFERS您可以使用以下查询清理 SQL 服务器缓存:DBCC DROPCLEANBUFFERS

So, to answer your question about LinQ queries and caching.因此,回答您关于 LinQ 查询和缓存的问题。

Normal LinQ queries don't get cached and should be used when you only have to run it once.普通的 LinQ 查询不会被缓存,只需要运行一次时应该使用它。 Compiled LinQ queries DO get cached by the SQL Server and will have performance improvements if you run it more than once.编译后的 LinQ 查询确实会被 SQL 服务器缓存,如果您多次运行它,性能会有所提高。

I just found out about it and i think it's handy to know.我刚刚发现了它,我认为知道它很方便。

As far as I know, it behaves as normal queries to SQL Server.据我所知,它表现为对 SQL 服务器的正常查询。 You can always call Stored Procedures with LINQ to SQL if benefits exist for your specific case.如果您的特定情况存在好处,您始终可以使用 LINQ 到 SQL 调用存储过程。

MS SQL Server caches the query plans for anything TSQL that is run against it. MS SQL 服务器缓存针对它运行的任何 TSQL 的查询计划。 Once the plan is cached, the query will run a bit faster.一旦计划被缓存,查询将运行得更快一些。 I know that there is an expiration time for this cache, but I haven't asked the dba's about it.我知道这个缓存有一个过期时间,但我还没有问过dba。

As far as I know, MS SQL Server 2000 was the last one that compiled the stored procs.据我所知,MS SQL Server 2000 是最后一个编译存储过程的。 That's not done anymore.那不再做了。 That's why a lot of inline queries are just as fast (with SQL Server 2000+).这就是为什么很多内联查询都一样快(使用 SQL Server 2000+)。

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

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