MSSQL2005-QUERY EXECUTION學習筆記一

sqysl發表於2009-06-09

內容來自:Chapter 3. Query Execution ---reading query plan
一、讀執行計劃:
1、查詢計劃選項;
(1)圖形計劃,可以儲存和載入;
(2)文字計劃:SET SHOWPLAN_TEXT ON只顯示執行計劃;SET SHOWPLAN_ALL ON顯示執行計劃和圖形計劃ToolTips和屬性視窗中的評估和統計資訊中的大多數資訊;
(3)XML計劃:該計劃是MSSQL2005中新出現的,它組合了文字和圖形計劃的很多特點;
(4)Estimated vs. Actual Query Plans(評估和實際查詢計劃):我們把不執行查詢語句而產生的計劃稱為“評估執行計劃”,因為在執行語句時,可能重新編譯執行不同的執行計劃;而把執行語句後生成的執行計劃稱為“實際執行計劃”,除了包含和評估執行計劃相同的資訊,還包括實際行數和操作實際執行數,注意:在實際計劃中,雖然實際執行了語句,但產生的“評估代價”和評估執行計劃中一樣,並不真正反映真正的執行代價,下面是設定產生計劃型別的命令:
Table 3-1. SET Commands for Displaying Query Plans CommandExecute Query?Include Estimated Row Counts & StatsInclude Actual Row Counts & StatsText PlanSET SHOWPLAN_TEXT ONNoNoNoSET SHOWPLAN_ALL ONNoYesNoSET STATISTICS PROFILE ONYesYesYesXML PlanSET SHOWPLAN_XML ONNoYesNoSET STATISTICS PROFILE XMLYesYesYes
開始一看XML的計劃,頭都大了,可仔細一看,還是比較好用的,首先每個操作都有節點號,其次,資訊很全,很多是透過屬性給出的,下面是一個例子:
     "SELECT O.[CustomerId], MAX(O.[Freight]) as MaxFreight
     FROM [Customers] C JOIN [Orders] O
           ON C.[CustomerId] = O.[CustomerId
     WHERE C.[Country] = @Country
     GROUP BY O.[CustomerId]
     OPTION (OPTIMIZE FOR (@Country = N'UK'))"...>
                         CONCAT_NULL_YIELDS_NULL="false" ANSI_NULLS="false"
                       ANSI_PADDING="false" ANSI_WARNINGS="false"
                       NUMERIC_ROUNDABORT="false" />
               CompileTime="20" CompileCPU="20" CompileMemory="280">
       --消耗記憶體的操作;
    
      
        
          
            
            
                                              LogicalOp="Clustered Index Scan"...>
                
                                                   Index="[PK_Orders]" Alias="[O]" />
                

              

            
          

        
      

                              LogicalOp="Clustered Index Scan"...>
                                     NoExpandHint="0">                                                                  --節點1;
                                       Index="[PK_Customers]" Alias="[C]" />
              
                
                

              

          
        
      
    
    
                          ParameterRuntimeValue="N'USA'" />
    

  


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8484829/viewspace-605580/,如需轉載,請註明出處,否則將追究法律責任。

相關文章