1. 程式人生 > 實用技巧 >SQL Server查詢執行過程

SQL Server查詢執行過程

1.查詢過程

1.1.query parse

確認語法是否正確

1.2.query binding

確認物件(表,列等)是否正確

1.3.query optimization

生成執行計劃,大多數會生成full cost-based plan(完全基於成本的),少部分生成trivial plan()

基於成本的計劃需要3個條件

  • Query processor tree
  • statistics

統計資訊影響到基數,選擇性,其由以下部分組成
1.the header – general data about a given set of statistics
2.the density graph – the selectivity, uniqueness, of the data, and, most importantly
3.a histogram – a tabulation of counts of the occurrence of a particular value, taken from
up to 200 data points that are chosen to best represent the complete data in the table.
記憶體壓力或執行計劃長期未被使用被新的計劃擠出plan cache,手動執行下面的命令也會導致釋放plan cache

sys.dm_exec_query_stats  sql_handle和plan_handle
dbcc freeproccache(<plan_handle>)
DBCC FLUSHPROCINDB(db_id)
  • Constraints

1.4.query execution phase