Sql-Server

計劃記憶體中儲存了哪個執行計劃?

  • May 26, 2022

執行查詢時,SQL Server 會生成一個查詢計劃列表,並啟發式地選擇成本較低的計劃。

當看到相同的查詢時,選擇的計劃將儲存在計劃記憶體中以供後續使用。

當表的某些屬性發生變化或重建索引時,它會再次生成一個查詢計劃列表,並試探性地選擇成本較低的一個,並將其儲存在計劃記憶體中。

但是,MSDN 似乎表明估計的計劃儲存在計劃記憶體中,請參見下面的螢幕截圖。那是對的嗎?

在此處輸入圖像描述

是的,這是正確的,儘管使用的術語並不完美。

術語*’estimated’‘actual’是區分沒有執行時統計的計劃(’estimated’)和還包括來自特定執行的統計資訊(‘actual’*)的一種方便的方法。

記憶體計劃不包括來自任何特定執行的執行時統計資訊,因此它是*’estimated’*。

Query Store 還保存*“估計的”*計劃,但也單獨記錄一些執行時資訊。

僅供參考,想將其添加到偉大的保羅的回答中。

這是關於執行計劃的微軟文件:

https://docs.microsoft.com/en-us/sql/relational-databases/performance/execution-plans?view=sql-server-ver16

如您所見,它是這樣說的:

  • 估計的執行計劃是編譯計劃,由查詢優化器根據估計生成。這是儲存在計劃記憶體中的查詢計劃
  • 實際執行計劃是編譯計劃加上它的執行上下文。它在查詢執行完成後變為可用。這包括實際執行時資訊,例如執行警告,或在較新版本的數據庫引擎中,執行期間已用時間和 CPU 時間。
  • The Live Query Statistics is the compiled plan plus its execution context. It is available for in-flight query executions, and updated every second. This includes runtime information such as the actual number of rows flowing through the operators, elapsed time, and the estimated query progress.

In the Query Store you can also find estimated execution plans and query statistcs.

引用自:https://dba.stackexchange.com/questions/312552