如何强制重新编译 Linq to SQL 查询的执行计划?

发布于 2024-08-24 12:44:23 字数 225 浏览 11 评论 0原文

我有一个动态创建的 LINQ to SQL 查询。有趣的是,当我在 SQL Management Studio 中运行它时,速度快如闪电。当我从 L2S 运行它时,一段时间后它变得非常慢。

这可能是由于查询计划/执行计划造成的。当我重新启动 SQL Server 时,L2S 查询也再次变得快如闪电。

现在,通过 T-SQL,您可以使用“WITH RECOMPILE”。但如何使用 L2S 做到这一点呢?

I have a LINQ to SQL query that's created dynamically. Funny thing is, when I run it in SQL Management Studio it's lightning fast. When I run it from L2S it becomes awefully slow after a while.

This is probably because of the query plan/execution plan. When I restart SQL Server the L2S query is also lightning fast again.

Now with T-SQL you can have WITH RECOMPILE. But how to do this with L2S?

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

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

发布评论

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

评论(4

反话 2024-08-31 12:44:24

正如我在下面的线程中发现的,您可以使用 DataContext.GetCommand(IQueryable) 来获取您希望执行的查询的 DbCommand。您可以将“选项(重新编译)”添加到命令文本,然后打开阅读器,然后使用 [DataContext.Translate]1 将打开的阅读器转换为您想要的实体类型。

http://social. msdn.microsoft.com/Forums/en-US/linqtosql/thread/def80609-eaf2-4631-8d3d-ad10fc9aedfa

例如,给定一个 DataContext dataContext

IQueryable<string> exampleItemsQuery = dataContext.Table.Where(…).Select(…); //etc

DbCommand command = dataContext.GetCommand(exampleItemsQuery);
command.CommandText += Environment.NewLine + "OPTION (RECOMPILE)";
if (dataContext.Connection.State != ConnectionState.Open)
   dataContext.Connection.Open();

IEnumerable<string> exampleItems = dataContext.Translate<string>(command.ExecuteReader(CommandBehavior.CloseConnection));

As I found in the thread below, you can use the DataContext.GetCommand(IQueryable) to get a DbCommand for the query you wish to execute. You can add "OPTION (RECOMPILE)" to the command text, from that, open a reader, and use [DataContext.Translate<T>]1 to translate the opened reader to the entity type you wanted.

http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/def80609-eaf2-4631-8d3d-ad10fc9aedfa

For example, given a DataContext dataContext:

IQueryable<string> exampleItemsQuery = dataContext.Table.Where(…).Select(…); //etc

DbCommand command = dataContext.GetCommand(exampleItemsQuery);
command.CommandText += Environment.NewLine + "OPTION (RECOMPILE)";
if (dataContext.Connection.State != ConnectionState.Open)
   dataContext.Connection.Open();

IEnumerable<string> exampleItems = dataContext.Translate<string>(command.ExecuteReader(CommandBehavior.CloseConnection));
じ违心 2024-08-31 12:44:24

从您描述的行为来看,您的统计数据几乎肯定已经过时。

我建议你重建它们:

exec sp_MSForeachTable 'UPDATE STATISTICS ?'

From the behaviour you describe, your statistics are almost certainly out of date.

I suggest you rebuild them:

exec sp_MSForeachTable 'UPDATE STATISTICS ?'
标点 2024-08-31 12:44:24

查看 CompiledQuery 类。这是来自 Microsoft 的教程,其中甚至介绍了更多细节。

Check out the CompiledQuery class. Here's a tutorial from Microsoft that goes into even more detail.

小嗲 2024-08-31 12:44:24

我使用这个 EF 6 参数嗅探 在 SQL 命令末尾添加“执行前的选项(重新编译)”。它对我有用。这是一个非常好的解决方法。

I used this EF 6 Parameter Sniffing to add at the end of SQL commands "option(recompile)" before executing. It work for me. It is very good workaround how to solve it.

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