萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> 數據庫綜合 >> Sql Server中非聚集索引的詳細介紹

Sql Server中非聚集索引的詳細介紹

這篇文章主要是對Sql Server中非聚集索引的介紹,本文講解了非聚集索引為什麼能提速這麼快、什麼是RID、非聚集索引的運行原理等內容,需要的朋友可以參考下。

一:現象

先讓我們一睹非聚集索引的真容,看看到底在執行計劃看來是個什麼玩意。我這裡有個product表,裡面灌了8w多數據,然後在Name列上建立

一個非聚集索引,就像下圖一樣:

Sql Server中非聚集索引的介紹

從上圖中看到了兩個好玩的東西,一個就是我想看到的“索引查找[nonclustered]”,這個大家很熟悉,也是這篇要說的,然後我們還看到了一個“RID查找”,乍一看這是什麼雞巴玩意。非聚集索引跟它扯上什麼關系了?

二:什麼是RID

通過前面幾篇,我想大家都知道了數據頁中的記錄是如何尋找的?秘密就是通過slot槽位中的偏移量決定的,那問題來了,如果上升到數據頁層面,我只需要(pageID:slotID)就可以找到記錄了,對不對?那如果我上升了文件層面,那是不是只需要知道(fileID:pageID:slotID)就可以找到數據頁中的記錄了。其實這裡的RID就是站在文件的高度通過(fileID:pageID:slotID)找到表記錄的。既RID=RowID=(fileID:pageID:slotID),如果你非要眼見實的話,在sq中l還真提供了這麼個函數(sys.fn_PhysLocFormatter(%%physloc%%)),我們看下圖:

Sql Server中非聚集索引的介紹

看了上面的圖,是不是很興奮,一目了然,比如productID=18088這條記錄,然來是在1號文件,34941號數據頁,0號槽位上,productID=18089是在1號槽位上,好了,當你知道RID是個什麼東西的時候,我想你已經離徹底理解非聚集索引不遠啦。

三:非聚集索引

有一點我們肯定知道,就是非聚集索引是可以加速查找的,要是跟表掃描那樣的龜速,那也就失去了索引的目的,既然能加速,是因為它和聚集索引一樣,在底層都玩起了B樹,首先我們插入一些樣例數據。

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列設置為900個char,這也是索引的上限值,這樣的話,我DBCC就可以導出很多數據頁和索引頁了。

Sql Server中非聚集索引的介紹

可以看到,當我dbcc ind 的時候,發現Person表中已經有4個數據頁,5個索引頁,其中151號數據頁是表跟蹤頁,174號為索引跟蹤頁,這也就說明當我建立索引後,引擎給我們分配了專門的索引頁來存放我們建立的Name索引,那下一步就是我們來看看這些索引中都存放著什麼,這也是我非常關心的,接下來我導出173號索引頁。

復制代碼代碼如下:

DBCC PAGE(Ctrip,1,173,1)

Sql Server中非聚集索引的介紹

Sql Server中非聚集索引的介紹

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 &dagger;.aaaaa

0000000000000010:  20202020 20202020 20202020 20202020 &dagger;

0000000000000020:  20202020 20202020 20202020 20202020 &dagger;

0000000000000030:  20202020 20202020 20202020 20202020 &dagger;

0000000000000040:  20202020 20202020 20202020 20202020 &dagger;

0000000000000050:  20202020 20202020 20202020 20202020 &dagger;

0000000000000060:  20202020 20202020 20202020 20202020 &dagger;

0000000000000070:  20202020 20202020 20202020 20202020 &dagger;

0000000000000080:  20202020 20202020 20202020 20202020 &dagger;

0000000000000090:  20202020 20202020 20202020 20202020 &dagger;

00000000000000A0:  20202020 20202020 20202020 20202020 &dagger;

00000000000000B0:  20202020 20202020 20202020 20202020 &dagger;

00000000000000C0:  20202020 20202020 20202020 20202020 &dagger;

00000000000000D0:  20202020 20202020 20202020 20202020 &dagger;

00000000000000E0:  20202020 20202020 20202020 20202020 &dagger;

00000000000000F0:  20202020 20202020 20202020 20202020 &dagger;

0000000000000100:  20202020 20202020 20202020 20202020 &dagger;

0000000000000110:  20202020 20202020 20202020 20202020 &dagger;

0000000000000120:  20202020 20202020 20202020 20202020 &dagger;

0000000000000130:  20202020 20202020 20202020 20202020 &dagger;

0000000000000140:  20202020 20202020 20202020 20202020 &dagger;

0000000000000150:  20202020 20202020 20202020 20202020 &dagger;

0000000000000160:  20202020 20202020 20202020 20202020 &dagger;

0000000000000170:  20202020 20202020 20202020 20202020 &dagger;

0000000000000180:  20202020 20202020 20202020 20202020 &dagger;

0000000000000190:  20202020 20202020 20202020 20202020 &dagger;

00000000000001A0:  20202020 20202020 20202020 20202020 &dagger;

00000000000001B0:  20202020 20202020 20202020 20202020 &dagger;

00000000000001C0:  20202020 20202020 20202020 20202020 &dagger;

00000000000001D0:  20202020 20202020 20202020 20202020 &dagger;

00000000000001E0:  20202020 20202020 20202020 20202020 &dagger;

00000000000001F0:  20202020 20202020 20202020 20202020 &dagger;

0000000000000200:  20202020 20202020 20202020 20202020 &dagger;

0000000000000210:  20202020 20202020 20202020 20202020 &dagger;

0000000000000220:  20202020 20202020 20202020 20202020 &dagger;

0000000000000230:  20202020 20202020 20202020 20202020 &dagger;

0000000000000240:  20202020 20202020 20202020 20202020 &dagger;

0000000000000250:  20202020 20202020 20202020 20202020 &dagger;

0000000000000260:  20202020 20202020 20202020 20202020 &dagger;

0000000000000270:  20202020 20202020 20202020 20202020 &dagger;

0000000000000280:  20202020 20202020 20202020 20202020 &dagger;

0000000000000290:  20202020 20202020 20202020 20202020 &dagger;

00000000000002A0:  20202020 20202020 20202020 20202020 &dagger;

00000000000002B0:  20202020 20202020 20202020 20202020 &dagger;

00000000000002C0:  20202020 20202020 20202020 20202020 &dagger;

00000000000002D0:  20202020 20202020 20202020 20202020 &dagger;

00000000000002E0:  20202020 20202020 20202020 20202020 &dagger;

00000000000002F0:  20202020 20202020 20202020 20202020 &dagger;

0000000000000300:  20202020 20202020 20202020 20202020 &dagger;

0000000000000310:  20202020 20202020 20202020 20202020 &dagger;

0000000000000320:  20202020 20202020 20202020 20202020 &dagger;

0000000000000330:  20202020 20202020 20202020 20202020 &dagger;

00000000000003

copyright © 萬盛學電腦網 all rights reserved