非聚集索引,這個(gè)是大家都非常熟悉的一個(gè)東西,有時(shí)候我們由于業(yè)務(wù)原因,sql寫(xiě)的非常復(fù)雜,需要join很多張表,然后就淚流滿面了。。。這時(shí)候就有DBA或者資深的開(kāi)發(fā)給你看這個(gè)猥瑣的sql,通過(guò)執(zhí)行計(jì)劃一分析。。?;蛟S就看出了不該有的表掃描。。。萬(wàn)惡之源。。。然后給你在關(guān)鍵的字段加上非聚集索引后。。。才發(fā)現(xiàn)提速比阿斯頓馬丁還要快。。。那么一個(gè)問(wèn)題來(lái)了,為什么非聚集索引能提速這么快。。。怎么做到的???是不是非常的好奇???
這篇我們來(lái)解開(kāi)神秘面紗。
一:現(xiàn)象
先讓我們一睹非聚集索引的真容,看看到底在執(zhí)行計(jì)劃看來(lái)是個(gè)什么玩意。。。我這里有個(gè)product表,里面灌了8w多數(shù)據(jù),然后在Name列上建立
一個(gè)非聚集索引,就像下圖一樣:
從上圖中看到了兩個(gè)好玩的東西,一個(gè)就是我想看到的“索引查找[nonclustered]”,這個(gè)大家很熟悉,也是這篇要說(shuō)的,然后我們還看到了一個(gè)“RID查找”,乍一看這是什么雞巴玩意。。。非聚集索引跟它扯上什么關(guān)系了???
二:什么是RID
通過(guò)前面幾篇,我想大家都知道了數(shù)據(jù)頁(yè)中的記錄是如何尋找的?秘密就是通過(guò)slot槽位中的偏移量決定的,那問(wèn)題來(lái)了,如果上升到數(shù)據(jù)頁(yè)層面,我只需要(pageID:slotID)就可以找到記錄了,對(duì)不對(duì)?那如果我上升了文件層面,那是不是只需要知道(fileID:pageID:slotID)就可以找到數(shù)據(jù)頁(yè)中的 記錄了?其實(shí)這里的RID就是站在文件的高度通過(guò)(fileID:pageID:slotID)找到表記錄的。。。既RID=RowID=(fileID:pageID:slotID),如果你非要眼見(jiàn)實(shí)的話,在sq中l(wèi)還真提供了這么個(gè)函數(shù)(sys.fn_PhysLocFormatter(%%physloc%%)),我們看下圖:
看了上面的圖,是不是很興奮,一目了然,比如productID=18088這條記錄,然來(lái)是在1號(hào)文件,34941號(hào)數(shù)據(jù)頁(yè),0號(hào)槽位上,productID=18089是在1號(hào)槽位上,好了,當(dāng)你知道RID是個(gè)什么東西的時(shí)候,我想你已經(jīng)離徹底理解非聚集索引不遠(yuǎn)啦。。。
三:非聚集索引
有一點(diǎn)我們肯定知道,就是非聚集索引是可以加速查找的,要是跟表掃描那樣的龜速,那也就失去了索引的目的,既然能加速,是因?yàn)樗途奂饕粯樱诘讓佣纪嫫鹆薆樹(shù),首先我們插入一些樣例數(shù)據(jù)。
DROP TABLE dbo.Person
CREATE TABLE Person(ID INT IDENTITY,NAME CHAR(900))
CREATE INDEX idx_Person_Name ON dbo.Person(Name)
DECLARE @ch AS INT=97
WHILE @ch=122
BEGIN
INSERT INTO dbo.Person VALUES(REPLICATE(CHAR(@ch),5))
SET @ch=@ch+1
END
上面的sql,我故意在Name列設(shè)置為900個(gè)char,這也是索引的上限值,這樣的話,我DBCC就可以導(dǎo)出很多數(shù)據(jù)頁(yè)和索引頁(yè)了。
可以看到,當(dāng)我dbcc ind 的時(shí)候,發(fā)現(xiàn)Person表中已經(jīng)有4個(gè)數(shù)據(jù)頁(yè),5個(gè)索引頁(yè),其中151號(hào)數(shù)據(jù)頁(yè)是表跟蹤頁(yè),174號(hào)為索引跟蹤頁(yè),這也就說(shuō)明當(dāng)我建立索引后,引擎給我們分配了專(zhuān)門(mén)的索引頁(yè)來(lái)存放我們建立的Name索引,那下一步就是我們來(lái)看看這些索引中都存放著什么,這也是我非常關(guān)心的,接下來(lái)我導(dǎo)出173號(hào)索引頁(yè)。
復(fù)制代碼 代碼如下:
DBCC PAGE(Ctrip,1,173,1)
Slot 0, Offset 0x60, Length 912, DumpStyle BYTE
Record Type = INDEX_RECORD Record Attributes = NULL_BITMAP Record Size = 912
Memory Dump @0x000000000EF1C060
0000000000000000: 16616161 61612020 20202020 20202020 †.aaaaa
0000000000000010: 20202020 20202020 20202020 20202020 †
0000000000000020: 20202020 20202020 20202020 20202020 †
0000000000000030: 20202020 20202020 20202020 20202020 †
0000000000000040: 20202020 20202020 20202020 20202020 †
0000000000000050: 20202020 20202020 20202020 20202020 †
0000000000000060: 20202020 20202020 20202020 20202020 †
0000000000000070: 20202020 20202020 20202020 20202020 †
0000000000000080: 20202020 20202020 20202020 20202020 †
0000000000000090: 20202020 20202020 20202020 20202020 †
00000000000000A0: 20202020 20202020 20202020 20202020 †
00000000000000B0: 20202020 20202020 20202020 20202020 †
00000000000000C0: 20202020 20202020 20202020 20202020 †
00000000000000D0: 20202020 20202020 20202020 20202020 †
00000000000000E0: 20202020 20202020 20202020 20202020 †
00000000000000F0: 20202020 20202020 20202020 20202020 †
0000000000000100: 20202020 20202020 20202020 20202020 †
0000000000000110: 20202020 20202020 20202020 20202020 †
0000000000000120: 20202020 20202020 20202020 20202020 †
0000000000000130: 20202020 20202020 20202020 20202020 †
0000000000000140: 20202020 20202020 20202020 20202020 †
0000000000000150: 20202020 20202020 20202020 20202020 †
0000000000000160: 20202020 20202020 20202020 20202020 †
0000000000000170: 20202020 20202020 20202020 20202020 †
0000000000000180: 20202020 20202020 20202020 20202020 †
0000000000000190: 20202020 20202020 20202020 20202020 †
00000000000001A0: 20202020 20202020 20202020 20202020 †
00000000000001B0: 20202020 20202020 20202020 20202020 †
00000000000001C0: 20202020 20202020 20202020 20202020 †
00000000000001D0: 20202020 20202020 20202020 20202020 †
00000000000001E0: 20202020 20202020 20202020 20202020 †
00000000000001F0: 20202020 20202020 20202020 20202020 †
0000000000000200: 20202020 20202020 20202020 20202020 †
0000000000000210: 20202020 20202020 20202020 20202020 †
0000000000000220: 20202020 20202020 20202020 20202020 †
0000000000000230: 20202020 20202020 20202020 20202020 †
0000000000000240: 20202020 20202020 20202020 20202020 †
0000000000000250: 20202020 20202020 20202020 20202020 †
0000000000000260: 20202020 20202020 20202020 20202020 †
0000000000000270: 20202020 20202020 20202020 20202020 †
0000000000000280: 20202020 20202020 20202020 20202020 †
0000000000000290: 20202020 20202020 20202020 20202020 †
00000000000002A0: 20202020 20202020 20202020 20202020 †
00000000000002B0: 20202020 20202020 20202020 20202020 †
00000000000002C0: 20202020 20202020 20202020 20202020 †
00000000000002D0: 20202020 20202020 20202020 20202020 †
00000000000002E0: 20202020 20202020 20202020 20202020 †
00000000000002F0: 20202020 20202020 20202020 20202020 †
0000000000000300: 20202020 20202020 20202020 20202020 †
0000000000000310: 20202020 20202020 20202020 20202020 †
0000000000000320: 20202020 20202020 20202020 20202020 †
0000000000000330: 20202020 20202020 20202020 20202020 †
0000000000000340: 20202020 20202020 20202020 20202020 †
0000000000000350: 20202020 20202020 20202020 20202020 †
0000000000000360: 20202020 20202020 20202020 20202020 †
0000000000000370: 20202020 20202020 20202020 20202020 †
0000000000000380: 20202020 20940000 00010000 00020000 † ...........
復(fù)制代碼 代碼如下:
Row - Offset
7 (0x7) - 6480 (0x1950)
6 (0x6) - 5568 (0x15c0)
5 (0x5) - 4656 (0x1230)
4 (0x4) - 3744 (0xea0)
3 (0x3) - 2832 (0xb10)
2 (0x2) - 1920 (0x780)
1 (0x1) - 1008 (0x3f0)
0 (0x0) - 96 (0x60)
從上面至少可以發(fā)現(xiàn)三個(gè)有趣的現(xiàn)象:
1>:173號(hào)索引頁(yè)中slot0和slot1槽位指向記錄的內(nèi)容已經(jīng)有序了,比如:aaaaa,bbbbb。。。。這樣。。。。原來(lái)非聚集索引也是有序呀。。。
2>:6161616161就是16進(jìn)制的aaaaa。
9400000001000000 :這幾個(gè)數(shù)字非常重要,因?yàn)槭?6進(jìn)制表示,所以2位16進(jìn)制表示一個(gè)字節(jié),所以可以這么解釋?zhuān)懊?個(gè)字節(jié)表示
pageID,中間2個(gè)字節(jié)表示fileID,后面2個(gè)字節(jié)表示slot,看到這里你是不是想起了RID。。。因?yàn)镽ID就是這三樣的組合。。。原來(lái)非聚集索
引的記錄存放的就是“key+RowID”呀。。。。
3>:通過(guò)最后的槽位列表,可以得知173號(hào)索引頁(yè)上存放著8條索引記錄。
好了,看完了葉子節(jié)點(diǎn),我們?cè)倏捶种Ч?jié)點(diǎn),也就是IndexLevel=1的那條索引數(shù)據(jù)頁(yè),也就是78號(hào)。ok,dbcc看看吧。
當(dāng)看到這個(gè)列表的時(shí)候,不知道你腦子里面是不是有一幅圖出來(lái)了,就像上一篇看到聚集索引一樣,因?yàn)樗慕Y(jié)構(gòu)和聚集索引非常像,只不過(guò)非聚集索引這里多了一個(gè)RID而已。。。最后我也把圖貢獻(xiàn)一下。
總結(jié)一下:在走非聚集索引的時(shí)候,比如你的條件是where name='jjjjj' 時(shí),它的邏輯是這樣的,根據(jù)78號(hào)索引數(shù)據(jù)頁(yè)的key的范圍,然后通過(guò)rowid走到了79號(hào)索引數(shù)據(jù)頁(yè),然后在79號(hào)索引數(shù)據(jù)頁(yè)中順利的找到了jjjjj,這時(shí)候就可以拿出jjjjj的rowid去表數(shù)據(jù)頁(yè)中直接定位記錄,最后輸出。。。。。這個(gè)也就是博客開(kāi)頭的地方為什么會(huì)出現(xiàn)RID的查找。。。
您可能感興趣的文章:- mssql 建立索引
- SQL2000 全文索引完全圖解
- MSSQL 大量數(shù)據(jù)時(shí),建立索引或添加字段后保存更改提示超時(shí)的解決方法
- 關(guān)于重新組織和重新生成索引sp_RefreshIndex的介紹
- SQL2005CLR函數(shù)擴(kuò)展 - 關(guān)于山寨索引
- MSSQL自動(dòng)重建出現(xiàn)碎片的索引的方法分享
- 理解Sql Server中的聚集索引
- 在SQL SERVER中導(dǎo)致索引查找變成索引掃描的問(wèn)題分析
- 詳解sqlserver查詢(xún)表索引
- SQL2005重新生成索引的的存儲(chǔ)過(guò)程 sp_rebuild_index