Heim  >  Artikel  >  Datenbank  >  SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

WBOY
WBOYOriginal
2016-06-07 15:26:441117Durchsuche

SQLSERVER聚集索引与非聚集索引的再次研究(下) 上篇主要说了聚集索引和简单介绍了一下非聚集索引,相信大家一定对聚集索引和非聚集索引开始有一点了解了。 这篇文章只是作为参考,里面的观点不一定正确 上篇的地址:SQLSERVER聚集索引与非聚集索引的再次研

SQLSERVER聚集索引与非聚集索引的再次研究(下)

上篇主要说了聚集索引和简单介绍了一下非聚集索引,相信大家一定对聚集索引和非聚集索引开始有一点了解了。

这篇文章只是作为参考,里面的观点不一定正确

上篇的地址:SQLSERVER聚集索引与非聚集索引的再次研究(上)

下篇主要说非聚集索引

先上非聚集索引的结构图

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 先创建Department8表

<span> 1</span> <span>--</span><span>非聚集索引</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> 
<span> 3</span> <span>GO</span>
<span> 4</span> 
<span> 5</span> <span>CREATE</span> <span>TABLE</span><span> Department8(
</span><span> 6</span>     DepartmentID <span>int</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span><span> ,
</span><span> 7</span>     Name <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 8</span>     GroupName <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 9</span>     Company <span>NVARCHAR</span>(<span>300</span><span>),
</span><span>10</span>     ModifiedDate <span>datetime</span> <span>NOT</span> <span>NULL</span>  <span>DEFAULT</span> (<span>getdate</span><span>())
</span><span>11</span> <span>)
</span><span>12</span> 
<span>13</span> 
<span>14</span> <span>CREATE</span> <span>NONCLUSTERED</span> <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department8</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span><span>)
</span><span>15</span> 
<span>16</span> <span>DECLARE</span> <span>@i</span> <span>INT</span>
<span>17</span> <span>SET</span> <span>@i</span><span>=</span><span>1</span>
<span>18</span> <span>WHILE</span> <span>@i</span> <span> <span>100</span>
<span>19</span>     <span>BEGIN</span>
<span>20</span>         <span>INSERT</span>  <span>INTO</span> Department8 ( name, <span>[</span><span>Company</span><span>]</span><span>, groupname )
</span><span>21</span>         <span>VALUES</span>  ( <span>'</span><span>销售部</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span>)), <span>'</span><span>中国你好有限公司XX分公司</span><span>'</span>, <span>'</span><span>销售组</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span><span>)) )
</span><span>22</span>         <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
<span>23</span>     <span>END</span>
<span>24</span>     
<span>25</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department8</span><span>]</span>
<span>26</span> 
<span>27</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span>
<span>28</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(pratice,Department8,-1) </span><span>'</span><span>)
</span><span>29</span> 
<span>30</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> </span>

 

先说明一下:

PageType  分页类型: 1:数据页面;2:索引页面;3:Lob_mixed_page;4:Lob_tree_page;10:IAM页面

IndexID    索引ID:   0 代表堆, 1 代表聚集索引, 2-250 代表非聚集索引 ,大于250就是text或image字段

 SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

每个数据页的IndexID都是0,说明数据页不属于非聚集索引的一部分,如果你有看到本系列的上篇,你会看到聚集索引表里数据页的IndexID都是1

说明数据页属于聚集索引的一部分,这里非聚集索引表的数据页的IndexID不是2而是0

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 

-------------------------------------------------华丽的分割线--------------------------------------------------

下面看一下非聚集索引的索引页

<span>1</span> <span>DBCC</span> TRACEON(<span>3604</span>,<span>-</span><span>1</span><span>)
</span><span>2</span> <span>GO</span>
<span>3</span> <span>DBCC</span> PAGE(<span>[</span><span>pratice</span><span>]</span>,<span>1</span>,<span>14499</span>,<span>3</span><span>)
</span><span>4</span> <span>GO</span>

 SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 SQLSERVER聚集索引与非聚集索引的再次研究(下)

 聚集索引跟非聚集索引不同,聚集索引页里的一行表示一个数据页,而且标记了这个数据页索引字段的范围值

而非聚集索引跟数据表的记录一一对应,非聚集索引页里的一行记录表示数据表的一行记录,而且记录了指向实际记录的指针

其实非聚集索引的所有索引页合并在一起就是数据表的一个缩小版表中只有非聚集索引),索引页中只包含创建非聚集索引时的字段,

所以当数据量少的时候,会使用全表扫描而不用索引扫描,因为堆中的数据页包含了表的全部字段 而索引页只包含了索引的字段,当select的时候

无论你是select * 还是select 某个字段 ,在效率上会差不多但是可以select出来的数据就会多很多

 ------------------------------------------------华丽的分割线--------------------------------------------------------------

 那么非聚集索引是怎麽查找记录的?

这里分两种情况:(1)非聚集索引查找(2)非聚集索引扫描

这一次我就非聚集索引查找和非聚集索引扫描一起讲了,不像《SQLSERVER聚集索引与非聚集索引的再次研究(上)》里那样

查找和扫描分开来讲

 这里创建Department9表,由于Department8表只有99行记录,数据量少的话SQLSERVER会直接走全表扫描,看不出效果

SQLSERVER聚集索引与非聚集索引的再次研究(下)SQLSERVER聚集索引与非聚集索引的再次研究(下)

<span> 1</span> <span>--</span><span>非聚集索引</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> 
<span> 3</span> <span>GO</span>
<span> 4</span> 
<span> 5</span> <span>CREATE</span> <span>TABLE</span><span> Department9(
</span><span> 6</span>     DepartmentID <span>int</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span><span> ,
</span><span> 7</span>     Name <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 8</span>     GroupName <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 9</span>     Company <span>NVARCHAR</span>(<span>300</span><span>),
</span><span>10</span>     ModifiedDate <span>datetime</span> <span>NOT</span> <span>NULL</span>  <span>DEFAULT</span> (<span>getdate</span><span>())
</span><span>11</span> <span>)
</span><span>12</span> 
<span>13</span> 
<span>14</span> <span>CREATE</span> <span>NONCLUSTERED</span> <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span><span>)
</span><span>15</span> 
<span>16</span> <span>DECLARE</span> <span>@i</span> <span>INT</span>
<span>17</span> <span>SET</span> <span>@i</span><span>=</span><span>1</span>
<span>18</span> <span>WHILE</span> <span>@i</span> <span> <span>1000000</span>
<span>19</span>     <span>BEGIN</span>
<span>20</span>         <span>INSERT</span>  <span>INTO</span> Department9 ( name, <span>[</span><span>Company</span><span>]</span><span>, groupname )
</span><span>21</span>         <span>VALUES</span>  ( <span>'</span><span>销售部</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span>)), <span>'</span><span>中国你好有限公司XX分公司</span><span>'</span>, <span>'</span><span>销售组</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span><span>)) )
</span><span>22</span>         <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
<span>23</span>     <span>END</span>
<span>24</span>     
<span>25</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>
<span>26</span> 
<span>27</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span>
<span>28</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(pratice,Department9,-1) </span><span>'</span><span>)
</span><span>29</span> 
<span>30</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> </span>
View Code

可以看到Department9表有446142行记录,因为insert插入的时间太久了,SQLSERVER没有执行完我就停止执行了,我的本本不给力啊!!

SQLSERVER聚集索引与非聚集索引的再次研究(下)

<span>1</span> <span>SELECT</span> <span>[</span><span>GroupName</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> name<span>=</span> <span>'</span><span>销售部1</span><span>'</span> <span>--</span><span>索引查找</span>
<span>2</span> <span>SELECT</span> <span>[</span><span>GroupName</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> <span>[</span><span>GroupName</span><span>]</span><span>=</span><span>'</span><span>销售组10</span><span>'</span> <span>--</span><span>索引扫描</span>
<span>3</span> <span>SELECT</span> <span>[</span><span>GroupName</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> <span>[</span><span>DepartmentID</span><span>]</span><span>=</span><span>66</span> <span>--</span><span>全表扫描</span>
<span>4</span> <span>SELECT</span> <span>[</span><span>DepartmentID</span><span>]</span>,<span>[</span><span>ModifiedDate</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> name<span>=</span> <span>'</span><span>销售部8</span><span>'</span>  <span>--</span><span>RID查找 索引查找</span>
<span>5</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> <span>[</span><span>GroupName</span><span>]</span><span>=</span><span>'</span><span>销售组10</span><span>'</span>  <span>--</span><span>RID查找  索引扫描</span>

 SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

大家可以用《SQLSERVER聚集索引与非聚集索引的再次研究(上)》中用到的脚本来看SQLSERVER查找记录的过程中申请了什么锁来推测

查找的过程

<span> 1</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span>
<span> 2</span> <span>GO</span>
<span> 3</span> <span>SET</span> <span>TRANSACTION</span> <span>ISOLATION</span> <span>LEVEL</span> <span>REPEATABLE</span> <span>READ</span>
<span> 4</span> <span>GO</span>
<span> 5</span> 
<span> 6</span> <span>BEGIN</span> <span>TRAN</span>
<span> 7</span> <span>SELECT</span> <span>[</span><span>GroupName</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> name<span>=</span> <span>'</span><span>销售部1</span><span>'</span> <span>--</span><span>--替换相应的SQL语句</span>
<span> 8</span> 
<span> 9</span> <span>--</span><span>COMMIT TRAN--当看到结果之后要commit tran,不然锁不会释放</span>
<span>10</span> 
<span>11</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> <span>--</span><span>要查询申请锁的数据库</span>
<span>12</span> <span>GO</span>
<span>13</span> <span>SELECT</span>
<span>14</span> <span>[</span><span>request_session_id</span><span>]</span><span>,
</span><span>15</span> c.<span>[</span><span>program_name</span><span>]</span><span>,
</span><span>16</span> <span>DB_NAME</span>(c.<span>[</span><span>dbid</span><span>]</span>) <span>AS</span><span> dbname,
</span><span>17</span> <span>[</span><span>resource_type</span><span>]</span><span>,
</span><span>18</span> <span>[</span><span>request_status</span><span>]</span><span>,
</span><span>19</span> <span>[</span><span>request_mode</span><span>]</span><span>,
</span><span>20</span> <span>[</span><span>resource_description</span><span>]</span>,<span>OBJECT_NAME</span>(p.<span>[</span><span>object_id</span><span>]</span>) <span>AS</span><span> objectname,
</span><span>21</span> p.<span>[</span><span>index_id</span><span>]</span>
<span>22</span> <span>FROM</span> sys.<span>[</span><span>dm_tran_locks</span><span>]</span> <span>AS</span> a <span>LEFT</span> <span>JOIN</span> sys.<span>[</span><span>partitions</span><span>]</span> <span>AS</span><span> p
</span><span>23</span> <span>ON</span> a.<span>[</span><span>resource_associated_entity_id</span><span>]</span><span>=</span>p.<span>[</span><span>hobt_id</span><span>]</span>
<span>24</span> <span>LEFT</span> <span>JOIN</span> sys.<span>[</span><span>sysprocesses</span><span>]</span> <span>AS</span> c <span>ON</span> a.<span>[</span><span>request_session_id</span><span>]</span><span>=</span>c.<span>[</span><span>spid</span><span>]</span>
<span>25</span> <span>WHERE</span> c.<span>[</span><span>dbid</span><span>]</span><span>=</span><span>DB_ID</span>(<span>'</span><span>pratice</span><span>'</span>) <span>AND</span> a.<span>[</span><span>request_session_id</span><span>]</span><span>=</span><span>@@SPID</span>  <span>--</span><span>--要查询申请锁的数据库</span>
<span>26</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>request_session_id</span><span>]</span>,<span>[</span><span>resource_type</span><span>]</span>

 

其实非聚集索引查找跟聚集索引查找的原理基本上是一样的,只不过聚集索引查找根据keyhashvalue找到记录所在范围的时候还需要到数据页里找实际的记录,

因为聚集索引页的每一行记录只记录了聚集索引字段在数据页的范围

也正因为上面那个原因聚集索引扫描跟非聚集索引扫描也不一样

聚集索引扫描扫描的是数据页(原因就是上面说的:“聚集索引查找根据keyhashvalue找到记录所在范围的时候还需要到数据页里找实际的记录,

因为聚集索引页的每一行记录只记录了索引字段在数据页的范围”)

非聚集索引扫描扫描的是索引页

大家都是漫无目的地在数据页或者索引页里扫描记录

 

 

但是这里有一个问题:为什麽非聚集索引扫描没有到堆中的数据页里去扫描呢?而在索引页里扫描?

既然在索引页里扫描和在数据页里扫描大家都是漫无目的地去扫描,那么到堆中的数据页里去扫描不是更好??因为堆中的数据页包含了

记录的所有字段,而索引页只包含了创建非聚集索引时所包含的字段

因为非聚集索引扫描的前提是:where 后面要查找的字段不是建立索引时的第一个字段(不是索引查找),但是要查找的字段是包含创建非聚集索引时

的字段列中,这个字段已经保存在非聚集索引的索引页里,例子里就是GroupName列

<span>CREATE</span> <span>NONCLUSTERED</span> <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span>)

又因为在一个表中索引页一般会等于或者小于数据页,所以在非聚集索引页里扫描会比堆里的数据页里

扫描的时间快,扫描的次数少(当数据量很多的时候)

-----------------------------------------------------------------------------------------------------------------------------------

SELECT [GroupName] FROM [dbo].[Department9]  WHERE name= '销售部1' --索引查找

SQLSERVER聚集索引与非聚集索引的再次研究(下)

为什麽上面这条语句是索引查找,而没有RID查找?因为索引建立在GroupName和NAME上

1 <span>CREATE</span> <span>NONCLUSTERED</span> <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span>)

再看一下刚才给出的非聚集索引页结构
SQLSERVER聚集索引与非聚集索引的再次研究(下)

《SQLSERVER聚集索引与非聚集索引的再次研究(上)》提到

KeyHashValue根据主键列的第一个字段而生成的,第一个字段就是NAME

就是说,这里的KeyHashValue也是创建非聚集索引时的第一个字段生成的,第一个字段是NAME

所以根据where NAME= '销售部1' hash出一个key值跟KeyHashValue匹配,而GroupName的值本来就在索引页里面,所以找到

NAME= '销售部1'的那条记录就可以收工了o(∩_∩)o  不用再到数据页里找

SELECT [GroupName] FROM [dbo].[Department9]  WHERE [GroupName]='销售组10' --索引扫描

SQLSERVER聚集索引与非聚集索引的再次研究(下)

也是根据《SQLSERVER聚集索引与非聚集索引的再次研究(上)》里提到的

因为GroupName不是非聚集索引的第一个字段,所以只能用索引扫描

因为不知道key,所以SQLSERVER只能扫描所有索引页直到找到[GroupName]='销售组10',但是因为[GroupName]就存储在

索引页,所以没有RID查找

SELECT [GroupName] FROM [dbo].[Department9]  WHERE [DepartmentID]=66  --全表扫描

SQLSERVER聚集索引与非聚集索引的再次研究(下)

因为DepartmentID不在非聚集索引里,所以SQLSERVER只能全表扫描

SELECT [DepartmentID],[ModifiedDate] FROM [dbo].[Department9]  WHERE name= '销售部8'  --RID查找 索引查找

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 因为非聚集索引不包括[DepartmentID],[ModifiedDate]这两个字段,所以SQLSERVER先索引查找,在索引页里找出name= '销售部8'的那条记录

然后根据name= '销售部8'的那条记录存储的HEAP RID(key) 值,在数据页里找到name= '销售部8' 这条记录,然后把其他字段读出来

实际上HEAP RID(key) 存储的就是指向数据页的指针,直接指向数据页里name= '销售部8' 这条记录

 SELECT * FROM [dbo].[Department9]  WHERE [GroupName]='销售组10'  --RID查找  索引扫描

SQLSERVER聚集索引与非聚集索引的再次研究(下)

其实这条语句的前半部分查找过程跟SELECT [GroupName] FROM [dbo].[Department9]  WHERE [GroupName]='销售组10' --索引扫描

这条语句是一样的,因为其他字段不在非聚集索引的索引页里,所以需要利用HEAP RID(key) 值找到记录所在的数据页然后把其他字段的值读出来

 

补充实验

为了验证下面这句话

因为在一个表中索引页一般会等于或者小于数据页,所以在非聚集索引页里扫描会比堆里的数据页里

扫描的时间快,扫描的次数少(当数据量很多的时候)

创建CompareNonclusteredScan表 ,CompareNonclusteredScan表跟Department9表是一样的,只是没有添加任何索引

<span> 1</span> <span>--</span><span>非聚集索引扫描和全表扫描比较</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> 
<span> 3</span> <span>GO</span>
<span> 4</span> 
<span> 5</span> <span>CREATE</span> <span>TABLE</span><span> CompareNonclusteredScan(
</span><span> 6</span>     DepartmentID <span>int</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span><span> ,
</span><span> 7</span>     Name <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 8</span>     GroupName <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 9</span>     Company <span>NVARCHAR</span>(<span>300</span><span>),
</span><span>10</span>     ModifiedDate <span>datetime</span> <span>NOT</span> <span>NULL</span>  <span>DEFAULT</span> (<span>getdate</span><span>())
</span><span>11</span> <span>)
</span><span>12</span> 
<span>13</span> 
<span>14</span> <span>DECLARE</span> <span>@i</span> <span>INT</span>
<span>15</span> <span>SET</span> <span>@i</span><span>=</span><span>1</span>
<span>16</span> <span>WHILE</span> <span>@i</span> <span> <span>1000000</span>
<span>17</span>     <span>BEGIN</span>
<span>18</span>         <span>INSERT</span>  <span>INTO</span> CompareNonclusteredScan ( name, <span>[</span><span>Company</span><span>]</span><span>, groupname )
</span><span>19</span>         <span>VALUES</span>  ( <span>'</span><span>销售部</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span>)), <span>'</span><span>中国你好有限公司XX分公司</span><span>'</span>, <span>'</span><span>销售组</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span><span>)) )
</span><span>20</span>         <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
<span>21</span>     <span>END</span>
<span>22</span>     
<span>23</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.CompareNonclusteredScan</span>

<span> 1</span> <span>--</span><span>堆表扫描统计</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span>
<span> 3</span> <span>GO</span>
<span> 4</span> <span>DBCC</span><span> DROPCLEANBUFFERS
</span><span> 5</span> <span>GO</span>
<span> 6</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span> 7</span> <span>GO</span>
<span> 8</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span> 9</span> <span>GO</span>
<span>10</span> 
<span>11</span> <span>SELECT</span> <span>[</span><span>GroupName</span><span>]</span> <span>FROM</span> CompareNonclusteredScan  <span>WHERE</span> <span>[</span><span>GroupName</span><span>]</span><span>=</span><span>'</span><span>销售组10</span><span>'</span> 

<span> 1</span> <span>--</span><span>非聚集索引表扫描统计</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span>
<span> 3</span> <span>GO</span>
<span> 4</span> <span>DBCC</span><span> DROPCLEANBUFFERS
</span><span> 5</span> <span>GO</span>
<span> 6</span> <span>SET</span> <span>STATISTICS</span> IO <span>ON</span>
<span> 7</span> <span>GO</span>
<span> 8</span> <span>SET</span> <span>STATISTICS</span> TIME <span>ON</span>
<span> 9</span> <span>GO</span>
<span>10</span> 
<span>11</span> <span>SELECT</span> <span>[</span><span>GroupName</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> <span>[</span><span>GroupName</span><span>]</span><span>=</span><span>'</span><span>销售组10</span><span>'</span> 

我们就比较扫描的时候,堆表和非聚集索引表 所用IO 和所用时间的情况
Department9表的统计情况

<span>1</span> <span>SQL Server 分析和编译时间: 
</span><span>2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span>3</span> 
<span>4</span> (<span>1</span><span> 行受影响)
</span><span>5</span> 表 <span>'</span><span>Department9</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>4313</span> 次,物理读取 <span>3</span> 次,预读 <span>4304</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>6</span> 
<span>7</span> <span>SQL Server 执行时间:
</span><span>8</span>    CPU 时间 <span>=</span> <span>219</span> 毫秒,占用时间 <span>=</span> <span>1593</span> 毫秒。

CompareNonclusteredScan表的统计情况

<span>1</span> <span>SQL Server 分析和编译时间: 
</span><span>2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span>3</span> 
<span>4</span> (<span>1</span><span> 行受影响)
</span><span>5</span> 表 <span>'</span><span>CompareNonclusteredScan</span><span>'</span>。扫描计数 <span>5</span>,逻辑读取 <span>8766</span> 次,物理读取 <span>0</span> 次,预读 <span>8756</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>6</span> 
<span>7</span> <span>SQL Server 执行时间:
</span><span>8</span>    CPU 时间 <span>=</span> <span>296</span> 毫秒,占用时间 <span>=</span> <span>1059</span> 毫秒。

当我不执行DBCC DROPCLEANBUFFERS 不清空缓存

<span>1</span> <span>SQL Server 分析和编译时间: 
</span><span>2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span>3</span> 
<span>4</span> (<span>1</span><span> 行受影响)
</span><span>5</span> 表 <span>'</span><span>Department9</span><span>'</span>。扫描计数 <span>1</span>,逻辑读取 <span>4313</span> 次,物理读取 <span>0</span> 次,预读 <span>0</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>6</span> 
<span>7</span> <span>SQL Server 执行时间:
</span><span>8</span>    CPU 时间 <span>=</span> <span>78</span> 毫秒,占用时间 <span>=</span> <span>73</span> 毫秒。

 

<span>1</span> <span>SQL Server 分析和编译时间: 
</span><span>2</span>    CPU 时间 <span>=</span> <span>0</span> 毫秒,占用时间 <span>=</span> <span>0</span><span> 毫秒。
</span><span>3</span> 
<span>4</span> (<span>1</span><span> 行受影响)
</span><span>5</span> 表 <span>'</span><span>CompareNonclusteredScan</span><span>'</span>。扫描计数 <span>5</span>,逻辑读取 <span>8766</span> 次,物理读取 <span>0</span> 次,预读 <span>8756</span> 次,lob 逻辑读取 <span>0</span> 次,lob 物理读取 <span>0</span> 次,lob 预读 <span>0</span><span> 次。
</span><span>6</span> 
<span>7</span> <span>SQL Server 执行时间:
</span><span>8</span>    CPU 时间 <span>=</span> <span>250</span> 毫秒,占用时间 <span>=</span> <span>995</span> 毫秒。

为什麽逻辑读的次数、扫描的次数 、占用时间会相差这麽多??

其实原因很简单

一般在一张表里面索引页面都会比数据页面少,比如一个表有100行记录,非聚集索引页面用一个页面就装下100行记录

数据页面一个页面只能装下50个,需要用两个数据页才能装得下所有数据

如下图,一个只有非聚集索引的表,数据页面有11个,非聚集索引页面有9个

SQLSERVER聚集索引与非聚集索引的再次研究(下)

如果要找一行记录,如果扫描数据页可能要扫描到第二页才能找到那条记录,如果扫描非聚集索引页,只需要扫描一个非聚集索引页就可以了

先读取非聚集索引页面(逻辑读取)-》再扫描非聚集索引页面(扫描计数)

所以逻辑读取、扫描计数、占用时间跟堆表相差这麽大的原因就是这个

还有下面这个SQL语句也是索引扫描,不加where 筛选条件,因为GroupName包含在非聚集索引中,所以扫描非聚集索引页面比扫描数据页面的效率高

<span>1</span> <span>SELECT</span> <span>[</span><span>GroupName</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>    <span>--</span><span>索引扫描</span>

SQLSERVER聚集索引与非聚集索引的再次研究(下)


现在大家可以看出来扫描索引页和扫描数据页的优势了吧??

那么非聚集索引是不是一定会扫描非聚集索引页呢(当数据量很少的时候)???

在有聚集索引的表里,只插入少量记录,表中是不会产生聚集索引页的,因为聚集索引扫描是扫描数据页不会扫描聚集索引页

那么非聚集索引会不会跟聚集索引一样呢??

先drop掉Department9表,然后重新建立Department9表,建表脚本跟刚才一样,只插入5条记录

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

结果还是会生成非聚集索引页,就是说无论什么情况,非聚集索引只会扫描非聚集索引页

 -------------------------------------------华丽的分割线------------------------------------------------------

 覆盖索引 INCLUDE()

 覆盖索引只能建立在非聚集索引上,那么覆盖索引是怎样的呢?

建立Department10表

<span> 1</span> <span>--</span><span>覆盖索引</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> 
<span> 3</span> <span>GO</span>
<span> 4</span> <span>DROP</span> <span>TABLE</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department10</span><span>]</span>
<span> 5</span> <span>CREATE</span> <span>TABLE</span><span> Department10(
</span><span> 6</span>     DepartmentID <span>int</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span><span> ,
</span><span> 7</span>     Name <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 8</span>     GroupName <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 9</span>     Company <span>NVARCHAR</span>(<span>300</span><span>),
</span><span>10</span>     ModifiedDate <span>datetime</span> <span>NOT</span> <span>NULL</span>  <span>DEFAULT</span> (<span>getdate</span><span>())
</span><span>11</span> <span>)
</span><span>12</span> 
<span>13</span> 
<span>14</span> <span>CREATE</span>  <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department10</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span><span>) INCLUDE(ModifiedDate)
</span><span>15</span> 
<span>16</span> 
<span>17</span> <span>DECLARE</span> <span>@i</span> <span>INT</span>
<span>18</span> <span>SET</span> <span>@i</span><span>=</span><span>1</span>
<span>19</span> <span>WHILE</span> <span>@i</span> <span> <span>10000</span>
<span>20</span>     <span>BEGIN</span>
<span>21</span>         <span>INSERT</span>  <span>INTO</span> Department10( name, <span>[</span><span>Company</span><span>]</span><span>, groupname )
</span><span>22</span>         <span>VALUES</span>  ( <span>'</span><span>销售部</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span>)), <span>'</span><span>中国你好有限公司XX分公司</span><span>'</span>, <span>'</span><span>销售组</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span><span>)) )
</span><span>23</span>         <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
<span>24</span>         <span>WAITFOR</span> DELAY <span>'</span><span>00:00:00:100</span><span>'</span>
<span>25</span>     <span>END</span>
<span>26</span>     
<span>27</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department10</span><span>]</span>
<span>28</span> 
<span>29</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span>
<span>30</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(pratice,Department10,-1) </span><span>'</span><span>)
</span><span>31</span> 
<span>32</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> </span>

 

查看一下索引页14549

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

你会发现覆盖索引跟非聚集索引的索引页结构是一样的,只不过多了ModifiedDate列,但是ModifiedDate列没有在旁边加上(key)

大家注意看

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 那么既然覆盖索引只是在索引页加上一个字段,那么倒不如创建非聚集索引的时候,把ModifiedDate列也纳入到非聚集索引中

<span>1</span> <span>CREATE</span>  <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department10</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span>,<span>[</span><span>ModifiedDate</span><span>]</span>) 

究竟覆盖索引有什么存在的价值呢???
我们看一下MSDN的解释

http://msdn.microsoft.com/zh-cn/library/ms190806(SQL.90).aspx

大家注意看里面其中一句话

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 

看了MSND的介绍,本人觉得覆盖索引最大的优势是突破了索引列大小的限制,将尽可能多的列(字段)放到索引页,

这样查询数据的时候就可以尽量使用索引扫描而不用RID查找或全表扫描,覆盖索引其他特别的用途或者特点或者优势就找不到了

还有MSDN里面提到“只能对表或索引视图的非聚集索引定义非键列”,为什麽聚集索引不能使用覆盖索引??

 

不知道大家有没有留意到本人在上面说到的一句话

其实非聚集索引查找跟聚集索引查找的原理基本上是一样的,只不过聚集索引查找根据keyhashvalue找到记录所在范围的时候还需要到数据页里找实际的记录,

因为聚集索引页的每一行记录只记录了索引字段在数据页的范围

也正因为上面那个原因聚集索引扫描跟非聚集索引扫描也不一样

聚集索引扫描扫描的是数据页(原因就是上面说的:“聚集索引查找根据keyhashvalue找到记录所在范围的时候还需要到数据页里找实际的记录,

因为聚集索引页的每一行记录只记录了索引字段在数据页的范围”)

非聚集索引扫描扫描的是索引页

 

 

如果聚集索引扫描的是数据页,那么就算你把覆盖索引加到聚集索引的索引页也没有用,因为SQLSERVER使用聚集索引扫描的时候扫描的

是数据页而不像非聚集索引那样扫描的是索引页

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 --------------------------------------------------华丽的分割线-------------------------------------------------------

 聚集索引和非聚集索引并存

 SQLSERVER聚集索引与非聚集索引的再次研究(下)

<span> 1</span> <span>--</span><span>非聚集索引和聚集索引</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> 
<span> 3</span> <span>GO</span>
<span> 4</span> 
<span> 5</span> <span>CREATE</span> <span>TABLE</span><span> Department11(
</span><span> 6</span>     DepartmentID <span>int</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span> <span>PRIMARY</span> <span>KEY</span><span>,
</span><span> 7</span>     Name <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 8</span>     GroupName <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 9</span>     Company <span>NVARCHAR</span>(<span>300</span><span>),
</span><span>10</span>     ModifiedDate <span>datetime</span> <span>NOT</span> <span>NULL</span>  <span>DEFAULT</span> (<span>getdate</span><span>())
</span><span>11</span> <span>)
</span><span>12</span> 
<span>13</span> <span>CREATE</span> <span>NONCLUSTERED</span>  <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department11</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span><span>)
</span><span>14</span> 
<span>15</span> <span>DECLARE</span> <span>@i</span> <span>INT</span>
<span>16</span> <span>SET</span> <span>@i</span><span>=</span><span>1</span>
<span>17</span> <span>WHILE</span> <span>@i</span> <span> <span>1000</span>
<span>18</span>     <span>BEGIN</span>
<span>19</span>         <span>INSERT</span>  <span>INTO</span> Department11( name, <span>[</span><span>Company</span><span>]</span><span>, groupname )
</span><span>20</span>         <span>VALUES</span>  ( <span>'</span><span>销售部</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span>)), <span>'</span><span>中国你好有限公司XX分公司</span><span>'</span>, <span>'</span><span>销售组</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span><span>)) )
</span><span>21</span>         <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
<span>22</span>     <span>END</span>
<span>23</span>     
<span>24</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department11</span><span>]</span>
<span>25</span> 
<span>26</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span>
<span>27</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(pratice,Department11,-1) </span><span>'</span><span>)
</span><span>28</span> 
<span>29</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> </span>

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 先来看一下非聚集索引页,可以看到非聚集索引中多了一个字段就是建立聚集索引时的第一个字段DepartmentID

SQLSERVER聚集索引与非聚集索引的再次研究(下)

 

再来看聚集索引页

SQLSERVER聚集索引与非聚集索引的再次研究(下)

MSDN中的解释:http://msdn.microsoft.com/zh-cn/library/ms177484(v=SQL.105).aspx

如果表有聚集索引或索引视图上有聚集索引,则行定位器是行的聚集索引键。

如果聚集索引不是唯一的索引,SQL Server 将添加在内部生成的值(称为唯一值)以使所有重复键唯一。

此四字节的值对于用户不可见。仅当需要使聚集键唯一以用于非聚集索引中时,才添加该值。

SQL Server 通过使用存储在非聚集索引的叶行内的聚集索引键搜索聚集索引来检索数据行

根据MSDN的解释,实际上相当于在非聚集索引上建立多一个字段,而这个字段就是联系聚集索引和非聚集索引的桥梁

<span>1</span> <span>CREATE</span> <span>NONCLUSTERED</span>  <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department11</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span>,<span>[</span><span>DepartmentID</span><span>]</span>)

 SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

那么当要查询的字段不在非聚集索引的索引页的时候,那么就要到聚集索引的叶子节点(数据页)去找记录,那么这个查找记录的过程是怎样的呢?

这个查找记录的过程实际上就是“书签查找”,在本文章的下面会讲到

 

-------------------------------------------------华丽的分割线-------------------------------------------------------------

 网上有人说,只有堆表才有IAM页,并且IAM页面维护着数据页的前后顺序,那么索引页是不是就没有IAM页维护索引页面的前后顺序呢?

有索引的表的数据页有没有IAM页来维护数据页的前后顺序呢?

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

MSDN中的解释:

http://msdn.microsoft.com/zh-cn/library/ms189051%28SQL.90%29.aspx

表、索引或索引视图分区的页分配由一个 IAM 页链管理。sys.system_internals_allocation_units 中的 first_iam_page 列指向 IAM 页链(用于管理分配给 IN_ROW_DATA 分配单元中的表、索引或索引视图的空间)中的第一个 IAM 页。
sys.partitions 为表或索引中每个分区返回一行。
堆在 sys.partitions 中有一行,其 index_id = 0。
sys.system_internals_allocation_units 中的 first_iam_page 列指向指定分区中堆数据页集合的 IAM 链。服务器使用 IAM 页查找数据页集合中的页,因为这些页没有链接。
表或视图的聚集索引在 sys.partitions 中有一行,其 index_id = 1。
sys.system_internals_allocation_units 中的 root_page 列指向指定分区内聚集索引 B 树的顶端。服务器使用索引 B 树查找分区中的数据页。
为表或视图创建的每个非聚集索引在 sys.partitions 中有一行,其 index_id > 1。
sys.system_internals_allocation_units 中的 root_page 列指向指定分区内非聚集索引 B 树的顶端。
至少有一个 LOB 列的每个表在 sys.partitions 中也有一行,其 index_id > 250。
first_iam_page 列指向管理 LOB_DATA 分配单元中的页的 IAM 页链。

 

再说明一下在DBCC IND的结果中PageType 字段和IndexID字段的含义

PageType  分页类型: 1:数据页面;2:索引页面;3:Lob_mixed_page;4:Lob_tree_page;10:IAM页面

IndexID    索引ID:   0 代表堆, 1 代表聚集索引, 2-250 代表非聚集索引 ,大于250就是text或image字段

 -------------------------------------------------------华丽的分割线----------------------------------------------------

 书签查找 BookMark Lookup

 建立Department12表

<span> 1</span> <span>--</span><span>非聚集索引和聚集索引  书签查找</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> 
<span> 3</span> <span>GO</span>
<span> 4</span> 
<span> 5</span> <span>CREATE</span> <span>TABLE</span><span> Department12(
</span><span> 6</span>     DepartmentID <span>int</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span> <span>PRIMARY</span> <span>KEY</span><span>,
</span><span> 7</span>     Name <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 8</span>     GroupName <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 9</span>     Company <span>NVARCHAR</span>(<span>300</span><span>),
</span><span>10</span>     ModifiedDate <span>datetime</span> <span>NOT</span> <span>NULL</span>  <span>DEFAULT</span> (<span>getdate</span><span>())
</span><span>11</span> <span>)
</span><span>12</span> 
<span>13</span> <span>CREATE</span> <span>NONCLUSTERED</span>  <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department12</span><span>]</span><span>(Name)
</span><span>14</span> 
<span>15</span> <span>DECLARE</span> <span>@i</span> <span>INT</span>
<span>16</span> <span>SET</span> <span>@i</span><span>=</span><span>1</span>
<span>17</span> <span>WHILE</span> <span>@i</span> <span> <span>10000</span>
<span>18</span>     <span>BEGIN</span>
<span>19</span>         <span>INSERT</span>  <span>INTO</span> Department12( name, <span>[</span><span>Company</span><span>]</span><span>, groupname )
</span><span>20</span>         <span>VALUES</span>  ( <span>'</span><span>销售部</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span>)), <span>'</span><span>中国你好有限公司XX分公司</span><span>'</span>, <span>'</span><span>销售组</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span><span>)) )
</span><span>21</span>         <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
<span>22</span>     <span>END</span>
<span>23</span>     
<span>24</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department12</span><span>]</span>
<span>25</span> 
<span>26</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span>
<span>27</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(pratice,Department12,-1) </span><span>'</span><span>)
</span><span>28</span> 
<span>29</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> </span>

书签查找的主体是“非聚集索引” 

所以书签查找只会出现在下面两种表中:

(1)只有非聚集索引的表

(2)聚集索引和非聚集索引并存的表

而只有聚集索引的表是不会出现书签查找的

 

 为什麽非聚集索引才会出现书签查找???

大家可以再看一下非聚集索引的结构图和MSDN的定义

SQLSERVER聚集索引与非聚集索引的再次研究(下)

SQLSERVER聚集索引与非聚集索引的再次研究(下)

而书签查找的定义:

MSDN定义:

定义:当查询优化器使用非聚集索引进行查找时,如果所选择的列或查询条件中的列只部分包含在使用的非聚集索引和聚集索引中时,就需要一个查找(lookup)来检索其他字段来满足请求。对一个有聚簇索引的表来说是一个键查找(key lookup),对一个堆表来说是一个RID查找(RID lookup),这种查找即是——书签查找(bookmark lookup)。简单的说就是当你使用的sql查询条件和select返回的列没有完全包含在索引列中时就会发生书签查找

因为无论是(1)只有非聚集索引的表 还是(2)聚集索引和非聚集索引并存的表 数据页都不是非聚集索引的一部分

所以如果所查找的数据不在非聚集索引的索引页就需要到数据页去取数据,这种情况就叫“书签查找”

其实数据表里的数据就像书本里的内容,而非聚集索引就像书签,因为书本里的内容不可能全部在书签里,但是要找到书本里的内容需要书签去定位

 

其实简单来讲,就四种情况:

我们再回到Department9表

 第一种

<span>1</span> <span>SELECT</span> <span>[</span><span>DepartmentID</span><span>]</span>,<span>[</span><span>ModifiedDate</span><span>]</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> name<span>=</span> <span>'</span><span>销售部8</span><span>'</span>  <span>--</span><span>RID查找 索引查找</span>


前面说到的:

因为非聚集索引不包括[DepartmentID],[ModifiedDate]这两个字段,所以SQLSERVER先索引查找,在索引页里找出name= '销售部8'的那条记录

然后根据name= '销售部8'的那条记录存储的HEAP RID(key) 值,在数据页里找到name= '销售部8' 这条记录,然后把其他字段读出来

实际上HEAP RID(key) 存储的就是指向数据页的指针,直接指向数据页里name= '销售部8' 这条记录

 

第二种

<span>1</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department9</span><span>]</span>  <span>WHERE</span> <span>[</span><span>GroupName</span><span>]</span><span>=</span><span>'</span><span>销售组10</span><span>'</span>  <span>--</span><span>RID查找  索引扫描</span>

前面说到的:

其实这条语句的前半部分查找过程跟SELECT [GroupName] FROM [dbo].[Department9]  WHERE [GroupName]='销售组10' --索引扫描

这条语句是一样的,因为其他字段不在非聚集索引的索引页里,所以需要利用HEAP RID(key) 值找到记录所在的数据页然后把其他字段的值读出来

 

 第三种

我们先建立Department13表 ,Department13表和Department11表的表结构是一样的

不过在Department13表里添加了1000000条记录,因为记录不够多(数据量很少)会走聚集索引扫描

 

<span> 1</span> <span>--</span><span>非聚集索引和聚集索引</span>
<span> 2</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> 
<span> 3</span> <span>GO</span>
<span> 4</span> 
<span> 5</span> <span>CREATE</span> <span>TABLE</span><span> Department13(
</span><span> 6</span>     DepartmentID <span>int</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span> <span>PRIMARY</span> <span>KEY</span><span>,
</span><span> 7</span>     Name <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 8</span>     GroupName <span>NVARCHAR</span>(<span>200</span>) <span>NOT</span> <span>NULL</span><span>,
</span><span> 9</span>     Company <span>NVARCHAR</span>(<span>300</span><span>),
</span><span>10</span>     ModifiedDate <span>datetime</span> <span>NOT</span> <span>NULL</span>  <span>DEFAULT</span> (<span>getdate</span><span>())
</span><span>11</span> <span>)
</span><span>12</span> 
<span>13</span> <span>CREATE</span> <span>NONCLUSTERED</span>  <span>INDEX</span> NCL_Name_GroupName <span>ON</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department13</span><span>]</span>(Name,<span>[</span><span>GroupName</span><span>]</span><span>)
</span><span>14</span> 
<span>15</span> <span>DECLARE</span> <span>@i</span> <span>INT</span>
<span>16</span> <span>SET</span> <span>@i</span><span>=</span><span>1</span>
<span>17</span> <span>WHILE</span> <span>@i</span> <span> <span>1000000</span>
<span>18</span>     <span>BEGIN</span>
<span>19</span>         <span>INSERT</span>  <span>INTO</span> Department13( name, <span>[</span><span>Company</span><span>]</span><span>, groupname )
</span><span>20</span>         <span>VALUES</span>  ( <span>'</span><span>销售部</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span>)), <span>'</span><span>中国你好有限公司XX分公司</span><span>'</span>, <span>'</span><span>销售组</span><span>'</span><span>+</span><span>CAST</span>(<span>@i</span> <span>AS</span> <span>VARCHAR</span>(<span>200</span><span>)) )
</span><span>21</span>         <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>+</span> <span>1</span>
<span>22</span>     <span>END</span>
<span>23</span>     
<span>24</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Department13</span><span>]</span></span>

 

<span>1</span> <span>SELECT</span> <span>[</span><span>ModifiedDate</span><span>]</span> <span>FROM</span> <span>[</span><span>Department13</span><span>]</span> <span>WHERE</span> GroupName<span>=</span><span>'</span><span>销售组168</span><span>'</span>  <span>--</span><span>索引扫描  键查找 并行</span>

SQLSERVER聚集索引与非聚集索引的再次研究(下)

那么第三种情况的查找过程是怎样的??

先用之前用到的测试语句测试一下用了什么锁

<span> 1</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span>
<span> 2</span> <span>GO</span>
<span> 3</span> <span>SET</span> <span>TRANSACTION</span> <span>ISOLATION</span> <span>LEVEL</span> <span>REPEATABLE</span> <span>READ</span>
<span> 4</span> <span>GO</span>
<span> 5</span> 
<span> 6</span> <span>BEGIN</span> <span>TRAN</span>
<span> 7</span> <span>SELECT</span> <span>[</span><span>ModifiedDate</span><span>]</span> <span>FROM</span> <span>[</span><span>Department13</span><span>]</span> <span>WHERE</span> GroupName<span>=</span><span>'</span><span>销售组168</span><span>'</span>  <span>--</span><span>索引扫描  键查找 并行</span>
<span> 8</span> 
<span> 9</span> <span>--</span><span>COMMIT TRAN</span>
<span>10</span> 
<span>11</span> <span>USE</span> <span>[</span><span>pratice</span><span>]</span> <span>--</span><span>要查询申请锁的数据库</span>
<span>12</span> <span>GO</span>
<span>13</span> <span>SELECT</span>
<span>14</span> <span>[</span><span>request_session_id</span><span>]</span><span>,
</span><span>15</span> c.<span>[</span><span>program_name</span><span>]</span><span>,
</span><span>16</span> <span>DB_NAME</span>(c.<span>[</span><span>dbid</span><span>]</span>) <span>AS</span><span> dbname,
</span><span>17</span> <span>[</span><span>resource_type</span><span>]</span><span>,
</span><span>18</span> <span>[</span><span>request_status</span><span>]</span><span>,
</span>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn