<tfoot id='nmI3R'></tfoot>

      <small id='nmI3R'></small><noframes id='nmI3R'>

      <i id='nmI3R'><tr id='nmI3R'><dt id='nmI3R'><q id='nmI3R'><span id='nmI3R'><b id='nmI3R'><form id='nmI3R'><ins id='nmI3R'></ins><ul id='nmI3R'></ul><sub id='nmI3R'></sub></form><legend id='nmI3R'></legend><bdo id='nmI3R'><pre id='nmI3R'><center id='nmI3R'></center></pre></bdo></b><th id='nmI3R'></th></span></q></dt></tr></i><div id='nmI3R'><tfoot id='nmI3R'></tfoot><dl id='nmI3R'><fieldset id='nmI3R'></fieldset></dl></div>

    1. <legend id='nmI3R'><style id='nmI3R'><dir id='nmI3R'><q id='nmI3R'></q></dir></style></legend>
          <bdo id='nmI3R'></bdo><ul id='nmI3R'></ul>

        选项(重新编译)总是更快;为什么?

        OPTION (RECOMPILE) is Always Faster; Why?(选项(重新编译)总是更快;为什么?)
          <bdo id='s5Y65'></bdo><ul id='s5Y65'></ul>
        • <legend id='s5Y65'><style id='s5Y65'><dir id='s5Y65'><q id='s5Y65'></q></dir></style></legend>

        • <small id='s5Y65'></small><noframes id='s5Y65'>

              <tbody id='s5Y65'></tbody>
              • <tfoot id='s5Y65'></tfoot>
                  <i id='s5Y65'><tr id='s5Y65'><dt id='s5Y65'><q id='s5Y65'><span id='s5Y65'><b id='s5Y65'><form id='s5Y65'><ins id='s5Y65'></ins><ul id='s5Y65'></ul><sub id='s5Y65'></sub></form><legend id='s5Y65'></legend><bdo id='s5Y65'><pre id='s5Y65'><center id='s5Y65'></center></pre></bdo></b><th id='s5Y65'></th></span></q></dt></tr></i><div id='s5Y65'><tfoot id='s5Y65'></tfoot><dl id='s5Y65'><fieldset id='s5Y65'></fieldset></dl></div>
                  本文介绍了选项(重新编译)总是更快;为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我遇到了一个奇怪的情况,将 OPTION (RECOMPILE) 附加到我的查询会导致它在半秒内运行,而省略它会导致查询花费超过五分钟.

                  I encountered an odd situation where appending OPTION (RECOMPILE) to my query causes it to run in half a second, while omitting it causes the query to take well over five minutes.

                  这是从查询分析器或通过 SqlCommand.ExecuteReader() 从我的 C# 程序执行查询的情况.调用(或不调用)DBCC FREEPROCCACHEDBCC dropcleanbuffers 没有区别;查询结果总是在使用 OPTION (RECOMPILE) 时立即返回,如果没有它超过五分钟.始终使用相同的参数调用查询 [为了这个测试].

                  This is the case when the query is executed from Query Analyzer or from my C# program via SqlCommand.ExecuteReader(). Calling (or not calling) DBCC FREEPROCCACHE or DBCC dropcleanbuffers makes no difference; Query results are always returned instantaneously with OPTION (RECOMPILE) and greater than five minutes without it. The query is always called with the same parameters [for the sake of this test].

                  我使用的是 SQL Server 2008.

                  I'm using SQL Server 2008.

                  我对编写 SQL 相当熟悉,但以前从未在查询中使用过 OPTION 命令,并且在扫描本论坛上的帖子之前不熟悉计划缓存的整个概念.我从帖子中了解到 OPTION (RECOMPILE) 是一个昂贵的操作.它显然为查询创建了一个新的查找策略.那么,为什么省略 OPTION (RECOMPILE) 的后续查询如此缓慢?后续查询不应该使用在包含重新编译提示的先前调用中计算的查找策略吗?

                  I'm fairly comfortable with writing SQL but have never used an OPTION command in a query before and was unfamiliar with the whole concept of plan caches until scanning the posts on this forum. My understanding from the posts is that OPTION (RECOMPILE) is an expensive operation. It apparently creates a new lookup strategy for the query. So why is it then, that subsequent queries that omit the OPTION (RECOMPILE) are so slow? Shouldn't the subsequent queries be making use of the lookup strategy that was computed on the previous call which included the recompilation hint?

                  每次调用都需要重新编译提示的查询是否非常罕见?

                  Is it highly unusual to have a query that requires a recompilation hint on every single call?

                  抱歉这个入门级的问题,但我真的无法对此做出正面或反面.

                  Sorry for the entry-level question but I can't really make heads or tails of this.

                  更新:我被要求发布查询...

                  select acctNo,min(date) earliestDate 
                  from( 
                      select acctNo,tradeDate as date 
                      from datafeed_trans 
                      where feedid=@feedID and feedDate=@feedDate 
                  
                      union 
                  
                      select acctNo,feedDate as date 
                      from datafeed_money 
                      where feedid=@feedID and feedDate=@feedDate 
                  
                      union 
                  
                      select acctNo,feedDate as date 
                      from datafeed_jnl 
                      where feedid=@feedID and feedDate=@feedDate 
                  )t1 
                  group by t1.acctNo
                  OPTION(RECOMPILE)
                  

                  从查询分析器运行测试时,我在前面添加了以下几行:

                  When running the test from Query Analyzer, I prepend the following lines:

                  declare @feedID int
                  select @feedID=20
                  
                  declare @feedDate datetime
                  select @feedDate='1/2/2009'
                  

                  从我的 C# 程序调用它时,参数通过 SqlCommand.Parameters 属性传入.

                  When calling it from my C# program, the parameters are passed in via the SqlCommand.Parameters property.

                  出于本次讨论的目的,您可以假设参数永远不会改变,因此我们可以排除次优参数气味是原因.

                  For the purposes of this discussion, you can assume that the parameters never change so we can rule out sub-optimal parameter smelling as the cause.

                  推荐答案

                  有时使用 OPTION(RECOMPILE) 是有意义的.根据我的经验,唯一可行的选择是在您使用动态 SQL 时.在您探索这对您的情况是否有意义之前,我建议您重建您的统计数据.这可以通过运行以下命令来完成:

                  There are times that using OPTION(RECOMPILE) makes sense. In my experience the only time this is a viable option is when you are using dynamic SQL. Before you explore whether this makes sense in your situation I would recommend rebuilding your statistics. This can be done by running the following:

                  EXEC sp_updatestats
                  

                  然后重新创建您的执行计划.这将确保在创建执行计划时将使用最新信息.

                  And then recreating your execution plan. This will ensure that when your execution plan is created it will be using the latest information.

                  添加 OPTION(RECOMPILE) 会在每次查询执行时重建执行计划.我从未听说过将创建新的查找策略描述为,但也许我们只是对同一件事使用不同的术语.

                  Adding OPTION(RECOMPILE) rebuilds the execution plan every time that your query executes. I have never heard that described as creates a new lookup strategy but maybe we are just using different terms for the same thing.

                  创建存储过程时(我怀疑您是从 .NET 调用 ad-hoc sql 但 如果您使用的是参数化查询,那么这最终会成为一个存储过程调用) SQL Server 尝试根据您数据库中的数据确定此查询的最有效执行计划以及传入的参数 (parameter嗅探),然后缓存这个计划.这意味着,如果您在数据库中有 10 条记录的情况下创建查询,然后在有 100,000,000 条记录时执行该查询,则缓存的执行计划可能不再是最有效的.

                  When a stored procedure is created (I suspect you are calling ad-hoc sql from .NET but if you are using a parameterized query then this ends up being a stored proc call) SQL Server attempts to determine the most effective execution plan for this query based on the data in your database and the parameters passed in (parameter sniffing), and then caches this plan. This means that if you create the query where there are 10 records in your database and then execute it when there are 100,000,000 records the cached execution plan may no longer be the most effective.

                  总而言之 - 我认为 OPTION(RECOMPILE) 在这里没有任何好处.我怀疑你只需要更新你的统计数据和你的执行计划.根据您的情况,重建统计信息可能是 DBA 工作的重要组成部分.如果您在更新统计信息后仍然遇到问题,我建议您同时发布两个执行计划.

                  In summary - I don't see any reason that OPTION(RECOMPILE) would be a benefit here. I suspect you just need to update your statistics and your execution plan. Rebuilding statistics can be an essential part of DBA work depending on your situation. If you are still having problems after updating your stats, I would suggest posting both execution plans.

                  并且回答您的问题 - 是的,我想说,每次执行查询时重新编译执行计划的最佳选择是非常不寻常的.

                  And to answer your question - yes, I would say it is highly unusual for your best option to be recompiling the execution plan every time you execute the query.

                  这篇关于选项(重新编译)总是更快;为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                  <tfoot id='nTx2v'></tfoot>
                    • <bdo id='nTx2v'></bdo><ul id='nTx2v'></ul>
                        <tbody id='nTx2v'></tbody>

                            <small id='nTx2v'></small><noframes id='nTx2v'>

                            <i id='nTx2v'><tr id='nTx2v'><dt id='nTx2v'><q id='nTx2v'><span id='nTx2v'><b id='nTx2v'><form id='nTx2v'><ins id='nTx2v'></ins><ul id='nTx2v'></ul><sub id='nTx2v'></sub></form><legend id='nTx2v'></legend><bdo id='nTx2v'><pre id='nTx2v'><center id='nTx2v'></center></pre></bdo></b><th id='nTx2v'></th></span></q></dt></tr></i><div id='nTx2v'><tfoot id='nTx2v'></tfoot><dl id='nTx2v'><fieldset id='nTx2v'></fieldset></dl></div>
                          1. <legend id='nTx2v'><style id='nTx2v'><dir id='nTx2v'><q id='nTx2v'></q></dir></style></legend>