這篇文章將為大家詳細(xì)講解有關(guān)Sql Server中非聚集索引的作用是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比撫州網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式撫州網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋撫州地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。一:現(xiàn)象
先讓我們一睹非聚集索引的真容,看看到底在執(zhí)行計(jì)劃看來是個(gè)什么玩意。。。我這里有個(gè)product表,里面灌了8w多數(shù)據(jù),然后在Name列上建立
一個(gè)非聚集索引,就像下圖一樣:
從上圖中看到了兩個(gè)好玩的東西,一個(gè)就是我想看到的“索引查找[nonclustered]”,這個(gè)大家很熟悉,也是這篇要說的,然后我們還看到了一個(gè)“RID查找”,乍一看這是什么雞巴玩意。。。非聚集索引跟它扯上什么關(guān)系了???
二:什么是RID
通過前面幾篇,我想大家都知道了數(shù)據(jù)頁中的記錄是如何尋找的?秘密就是通過slot槽位中的偏移量決定的,那問題來了,如果上升到數(shù)據(jù)頁層面,我只需要(pageID:slotID)就可以找到記錄了,對(duì)不對(duì)?那如果我上升了文件層面,那是不是只需要知道(fileID:pageID:slotID)就可以找到數(shù)據(jù)頁中的記錄了?其實(shí)這里的RID就是站在文件的高度通過(fileID:pageID:slotID)找到表記錄的。。。既RID=RowID=(fileID:pageID:slotID),如果你非要眼見實(shí)的話,在sq中l(wèi)還真提供了這么個(gè)函數(shù)(sys.fn_PhysLocFormatter(%%physloc%%)),我們看下圖:
看了上面的圖,是不是很興奮,一目了然,比如productID=18088這條記錄,然來是在1號(hào)文件,34941號(hào)數(shù)據(jù)頁,0號(hào)槽位上,productID=18089是在1號(hào)槽位上,好了,當(dāng)你知道RID是個(gè)什么東西的時(shí)候,我想你已經(jīng)離徹底理解非聚集索引不遠(yuǎn)啦。。。
三:非聚集索引
有一點(diǎn)我們肯定知道,就是非聚集索引是可以加速查找的,要是跟表掃描那樣的龜速,那也就失去了索引的目的,既然能加速,是因?yàn)樗途奂饕粯?,在底層都玩起了B樹,首先我們插入一些樣例數(shù)據(jù)。
DROP TABLE dbo.PersonCREATE TABLE Person(ID INT IDENTITY,NAME CHAR(900))CREATE INDEX idx_Person_Name ON dbo.Person(Name)DECLARE @ch AS INT=97WHILE @ch<=122BEGIN INSERT INTO dbo.Person VALUES(REPLICATE(CHAR(@ch),5)) SET @ch=@ch+1END
上面的sql,我故意在Name列設(shè)置為900個(gè)char,這也是索引的上限值,這樣的話,我DBCC就可以導(dǎo)出很多數(shù)據(jù)頁和索引頁了。
可以看到,當(dāng)我dbcc ind 的時(shí)候,發(fā)現(xiàn)Person表中已經(jīng)有4個(gè)數(shù)據(jù)頁,5個(gè)索引頁,其中151號(hào)數(shù)據(jù)頁是表跟蹤頁,174號(hào)為索引跟蹤頁,這也就說明當(dāng)我建立索引后,引擎給我們分配了專門的索引頁來存放我們建立的Name索引,那下一步就是我們來看看這些索引中都存放著什么,這也是我非常關(guān)心的,接下來我導(dǎo)出173號(hào)索引頁。
復(fù)制代碼 代碼如下: DBCC PAGE(Ctrip,1,173,1)
Slot 0, Offset 0x60, Length 912, DumpStyle BYTERecord Type = INDEX_RECORD Record Attributes = NULL_BITMAP Record Size = 912Memory Dump @0x000000000EF1C0600000000000000000: 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)索引頁中slot0和slot1槽位指向記錄的內(nèi)容已經(jīng)有序了,比如:aaaaa,bbbbb。。。。這樣。。。。原來非聚集索引也是有序呀。。。
<2>:6161616161就是16進(jìn)制的aaaaa。
9400000001000000 :這幾個(gè)數(shù)字非常重要,因?yàn)槭?6進(jìn)制表示,所以2位16進(jìn)制表示一個(gè)字節(jié),所以可以這么解釋,前面4個(gè)字節(jié)表示
pageID,中間2個(gè)字節(jié)表示fileID,后面2個(gè)字節(jié)表示slot,看到這里你是不是想起了RID。。。因?yàn)镽ID就是這三樣的組合。。。原來非聚集索
引的記錄存放的就是“key+RowID”呀。。。。
<3>:通過最后的槽位列表,可以得知173號(hào)索引頁上存放著8條索引記錄。
關(guān)于Sql Server中非聚集索引的作用是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站名稱:SqlServer中非聚集索引的作用是什么-創(chuàng)新互聯(lián)
URL標(biāo)題:http://jinyejixie.com/article40/piceo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、微信公眾號(hào)、網(wǎng)頁設(shè)計(jì)公司、Google、自適應(yīng)網(wǎng)站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容