MSSQL2005-QUERY EXECUTION学习笔记之二

来源:这里教程网 时间:2026-03-02 10:00:38 作者:

内容来自:Chapter 3. Query Execution---Analyzing plans 一、scan和seek:读表或索引数据。 1、Seekable Predicates and Covered Columns: (1)single-column indexes:一般来说,列上有函数或LIKE谓词中有先导匹配符,都会组织使用该列上的索引,例如: ABS([Col1]) = 1 [Col1] + 1 = 9 [Col1] LIKE '%abc' (2)composite indexes:一般只有在索引第一列上有相等谓词,才能是第二列上的谓词使用索引,例如: [Col1] = 3.14 AND [Col2] = 'pi' [Col1] = 'xyzzy' AND [Col2] <= 0 而有些情况,我们只能使用第一列上的谓词走索引,而第二列不走索引,这时我们称第二列上上的谓词为“剩余谓词”,例如: [Col1] > 100 AND [Col2] > 100                      --第一列的谓词不是相等谓词 [Col1] LIKE 'abc%' AND [Col2] = 2 最后,我们可能连第一列上的谓词都不能使用索引,这种情况下,就是直接不能使用索引,而应该更换其他索引,例如: [Col2] = 0                                                                      --跳过了第一列 [Col1] + 1 = 9 AND [Col2] BETWEEN 1 AND 9        --第一列用在了计算表达式中 [Col1] LIKE '%abc' AND [Col2] IN (1, 3, 5)                --第一列谓词LIKE中用到了先导通配符 (3)Identifying an Index's Keys:在一个已经有簇索引的表上建立一个非唯一非簇索引时,该索引会隐含簇索引的键值。 (4)Covered Columns:堆和簇索引包含表的所有列,而非簇索引仅仅覆盖基表的一部分列,当然,如果基表上有簇索引,非簇索引还包括簇索引的键值,无论给非簇索引是否包含簇关键字,在mssql2005中,我们能用create index的include子句为索引添加非键列,索引的排序和这些添加的非键列无关,例如: CREATE TABLE T_heap (a int, b int, c int, d int, e int, f int)CREATE INDEX T_heap_a ON T_heap (a)CREATE INDEX T_heap_bc ON T_heap (b, c)CREATE INDEX T_heap_d ON T_heap (d) INCLUDE (e)CREATE UNIQUE INDEX T_heap_f ON T_heap (f)CREATE TABLE T_clu (a int, b int, c int, d int, e int, f int)CREATE UNIQUE CLUSTERED INDEX T_clu_a ON T_clu (a)CREATE INDEX T_clu_b ON T_clu (b)CREATE INDEX T_clu_ac ON T_clu (a, c)CREATE INDEX T_clu_d ON T_clu (d) INCLUDE (e)CREATE UNIQUE INDEX T_clu_f ON T_clu (f) Table 3-3. Key Columns and Covered Columns in a Set of Nonclustered IndexesIndexKey ColumnsCovered ColumnsT_heap_aaaT_heap_bcb, cb, cT_heap_ddd, eT_heap_fffT_clu_aaa, b, c, d, e, fT_clu_bb, aa, bT_clu_aca, ca, cT_clu_dd, aa, d, eT_clu_ffa, f 这里注意,唯一性索引T_chu_f的键值列中,并不包含簇索引的键值列,但它覆盖了簇索引键值列,这是唯一性索引的与其他非簇索引的特别之处。 (5)Bookmark Lookup:

相关推荐