版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
Chapter8
Indexing2023/11/8Indexing2Indexing8.1TheConceptofIndexing8.2DiskStorage8.3TheB-TreeIndex8.4ClusteredandNon-ClusteredIndexes8.5AHashPrimaryIndex8.6ThrowingDartsatRandomSlots2023/11/8Indexing38.1TheConceptofIndexingIndexanindexislikeacardcataloginalibrary.Eachcard(entry)has:(keyvalue,row-pointer)keyvalueisforlookuprow-pointer(ROWID)isenoughtolocaterowondisk(oneI/O)2023/11/8Indexing48.1TheConceptofIndexingIndex:(keyvalue,row-pointer)EntriesareplacedinAlphabeticalorderbylookupkeyin"B-tree"(usually),alsomightbehashed.Anindexisalotlikememoryresidentstructures.Butindexisdiskresident.Likethedataitself,oftenwon'tallfitinmemoryatonce.Indexcanberegardedasatablewithtwocolumns:keyvalueandrow-pointer2023/11/8Indexing58.1TheConceptofIndexingFigure8.1:theSQLCreateIndexStatementCREATE[UNIQUE]INDEXindexname
ONtablename(colname
[ASC|DESC]{,colname
[ASC|DESC]...});DROPINDEXindexname;UNIQUEkeyvalueandrowisone-to-onewecanuseUNIQUEINDEXtoimplementUNIQUEconstraintsinCreateTableStatement.2023/11/8Indexing68.1TheConceptofIndexingExample8.1.1createindexcitiesxoncustomers(city);eachindexkeyvalue(i.e.,cityname)inthecitiesxindexcancorrespondtoalargenumberofdifferentcustomersrows.2023/11/8Indexing78.1TheConceptofIndexingExample8.1.2createuniqueindexcidxoncustomers(cid);eachindexkeyvalueinthecidxindexonlycorrespondtoacustomersrows.Indexkeyisquitedifferentfromrelationalconceptofprimarykeyorcandidatekey.But,wecanuseUNIQUEINDEXandNOTNULLtoimplementprimarykeyorcandidatekey.2023/11/8Indexing88.1TheConceptofIndexingIndexingAfterbeingcreated,indexissortedandplacedondisk.Sortisbycolumnvalueascordesc,asinSORTBYdescriptionofSelectstatement.NOTELATERCHANGEStoatableareimmediatelyreflectedintheindex,don'thavetocreateanewindex.2023/11/8Indexing98.2DiskStorageDatabaseStorageComputermemoryveryfastbutVolatile(揮發(fā)性的)storage.Diskstorageveryslowbutnon-volatileandverycheap.2023/11/8Indexing108.2DiskStoragemainmemorysecondarystoragetertiarystoragecachememorytapeCDdisksizesmallbigspeedfastslowcosthighlow2023/11/8Indexing118.2DiskStorageDiskAccessSeektime .008secondsThediskarmmovesinorouttothepropercylinderposition.Rotationallatency .004secondsThediskplatterrotatestotheproperangularposition.Transfertime .0005secondsThediskarmreads/writesthediskpageontheappropriatesurface.accessmemory:10-8~10-7seconds2023/11/8Indexing128.2DiskStorageMemoryBuffer(Figure8.2,pg.472)Readpagesintomemorybuffersocanaccessthem.Oncetorightplaceondisk,transfertimeischeap.Everytimewantapagefromdisk,hashondkpgaddr,h(dkpgaddr)toentryinHashlookasidetabletoseeifthatpageisalreadyinbuffer.2023/11/8Indexing138.2DiskStorageMemoryBuffer(cont.)AdvantagessaveddiskI/OfindsomethingforCPUtodowhilewaitingforI/O.Thisisoneoftheadvantagesofmulti-usertimesharing.CandoCPUworkforotheruserswhilewaitingforthisdiskI/O.2023/11/8Indexing148.2DiskStorageCreateTablespaceinORACLEFigure8.3,pg.476TablespacemadeupofOSfilescross2023/11/8Indexing158.2DiskStorageDataStoragePagesandRowPointers(Figure8.6,pg.480)Arowonmostarchitecturesisacontiguoussequenceofbytes.Nrowsplacedononepage(calledaBlockinORACLE).Headerinfonamesthekindofpage(datasegment,indexsegment),andthepagenumber(inORACLEtheOSfileandpagenumber).2023/11/8Indexing168.2DiskStorageROWsinpageRowsaddedrighttoleftfromrightendonblock.RowDirectoryentrieslefttorightonleftafterheader.Giveoffsetsofcorrespondingrowbeginnings.Providenumberofrowslotonpage.2023/11/8Indexing178.2DiskStorageWhennewrowadded,tellimmediatelyifwehavefreespacefornewdirectoryentryandnewrow.Conceptually,allspaceinmiddle,implieswhendeleterowandreclaimspacemustshifttofillingap.Alsomighthave"TableDirectory"inblockwhenhaveCLUSTER.2023/11/8Indexing188.2DiskStorageDiskPointerArowinatablecanbeuniquelyspecifiedwiththepagenumber(P)andslotnumber(S).RowsExtendingOverSeveralPagesProductVariations2023/11/8Indexing19Indexing8.1TheConceptofIndexing8.2DiskStorage8.3TheB-TreeIndex8.4ClusteredandNon-ClusteredIndexes8.5AHashPrimaryIndex8.6ThrowingDartsatRandomSlots2023/11/8Indexing208.3TheB-TreeIndexTheB-TreeEachnodeofthetreetakesupafulldiskpagehasalotoffanout2023/11/8Indexing21CreateIndexstatementinORACLECREATE[UNIQUE|BITMAP]INDEX[schema.]indexnameONtablename(colname
[ASC|DESC]{,colname
[ASC|DESC]…})[TABLESPACEtblespace
]
[STORAGE...][PCTFREEn
][
][NOSORT]UNIQUE|BITMAPASC|DESCTABLESPACEPCTFREENOSORT2023/11/8Indexing228.3TheB-TreeIndexCreatingIndexreadsthroughallrowsondisk(assumeN)pullsout(keyvalue,rowid)pairsforeachrowGetfollowinglist(inorderbykeyvals)putoutondisk(keyval1,rowid1)(keyval2,rowid2)...(keyvalN,rowidN)IfhaveNOSORTclause,rowsareinrightorder,sodon’thavetosort.2023/11/8Indexing23createuniqueindexsdxonS(sno)NOSORT;createindexndxonS(name)PCTFREE25;18PHShenS8817MASenS7717CSWangS6619PHLinS5518CSLoS4418MAXuS3317CSLiS2218CSLuS11agedeptnamesnoROWID8S87S76S65S54S43S32S21S1ROWIDsnoTableSIndexsdxIndexndxnameROWIDLi2Lin5Lo4Lu1Sen7Shen8Wang6Xu32023/11/8Indexing24Example8.3.1(pg.486)IdeaofBinarySearch/*binsearch:returnKsothatarr[K].keyval==x,or-1ifnomatch;*/intbinsearch(intx){intprobe=3,diff=2;while(diff>0){ /*loopuntilKtoreturn*/if(probe<=6&&x>arr[probe].keyval)probe=probe+diff;elseprobe=probe-diff;diff=diff/2;} /*wehavereachedfinalK*/if(probe<=6&&x==arr[probe].keyval)returnprobe;elseif(probe+1<=6&&x==arr[probe+1].keyval)returnprobe+1;elsereturn-1;}2023/11/8Indexing258.3TheB-TreeIndexExp8.3.2BinaryofaMillionIndexEntriesnumberofentries:1000000(106)sizeofentry:8bytessizeofdiskpage:2kbytesnumberofentriesineachdiskpage:250totalnumberofdiskpages:4000log24000
+1
≈13averagenumberofdiskI/O2023/11/8Indexing268.3TheB-TreeIndexExample8.3.3B-TreeStructureLayoutallentriesonleafnodesLevel1:RootNodeLevel2~LevelN-1:DirectoryNodesLevel2~LevelN-1:DirectoryNodesLevelN:LeafNode2023/11/8Indexing278.3TheB-TreeIndexTheStructureofB-TreeNodeK1,K2,…,Kmarekeyvalues,andK1<K2<…<Kmnismaximumnumberofkeyvalueinanode
LeafNode:
(n+1)/2
m
nPiisaROWIDwhichkeyvalueisKi(i=1,2,…,m)Pm+1isapointertonextleafnodeP1K1P2K2……PmKmPm+12023/11/8Indexing288.3TheB-TreeIndexRootNode:1
m
nInterNode:
(n-1)/2
m
nPiisapointertochildnode(rootnodeofsub-treeTi)foreachkeyvalueKonsub-treeTi,wehaveIfi=1: K<K1If1<i
m: Ki-1
K<KiIfi=m+1: K
KmP1K1P2K2……PmKmPm+12023/11/8Indexing298.3TheB-TreeIndexExample:leafnode(n=3)2023/11/8Indexing308.3TheB-TreeIndexExample:directoryorrootnode(n=3)2023/11/8Indexing31Example8.3.3(cont.)numberofentries:1000000(106)sizeofentry:8bytessizeofdiskpage:2kbytesnumberofentriesineachnode(diskpage)2k/8≈250totalnumberofleafnode1000000/250=4000totalnumberofdirectorynode:4000/250=16rootnodeaveragenumberofdiskI/O3(depthoftheB-Tree)
+1=4(rootnode)+(directorynode)+(leafnode)2023/11/8Indexing328.3TheB-TreeIndexHowtocreateanindexsortallentriesonleafnodescreatedirectory,anddirectorytodirectoryFanoutfofB-TreethemaximumnumberofentriesonaB-TreenodeDepthofB-TreeIftotalnumberofrowsisN,thendepth=logf(N)2023/11/8Indexing338.3TheB-TreeIndexDynamicchangesintheB-treeFigure8.12(pg.491)InsertfollowingkeyvaluesintoB-Tree(n=3)7,96,41,39,88,65,55,62Ideaof[PCTFREEn]2023/11/8Indexing34Def8.3.1:PropertiesoftheB-Tree(B+Tree)Everynodeisdisksizedandresidesinawell-definedlocationonthedisk.Nodesabovetheleaflevelcontaindirectoryentries,with(n–1)separatorkeysandndiskpointerstolower-levelB-treenodes.Nodesattheleaflevelcontainentrieswith(keyval,ROWID)pairspointingtoindividualrowsindexed.Allnodesbelowtherootareatleasthalffullwithentryinformation.Thisisnotoftenenforcedaftermultipledeletes.Therootnodecontainsatleasttwoentries(onekeyvalue).exceptwhenonlyonerowisindexedandtherootisaleafnode.2023/11/8Indexing358.3TheB-TreeIndexAlgorithmonB-Treesearchinsertdeleteupdate=delete+insert2023/11/8Indexing368.3TheB-TreeIndexTheORACLEBitmapIndexAbitmapindexusesONEbitmapforeachdistinctkeyval.AbitmaptakestheplaceofaROWIDlist,specifyingasetofrows.onebitvsaROWID(row)1:therowhavethekeyval0:therowdon’thavethekeyvalExampleFigure8.18(pg.501)2023/11/8Indexing378.4ClusteredandNon-ClusteredIndexesTheideaofaclusteredindexTherowsofthetableareinthesameorderastheindexentries—bykeyvalue.Inmostdatabaseproducts,defaultplacemenofrowsondatapagesondiskisinorderby
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小區(qū)垃圾處理與循環(huán)經(jīng)濟(jì)案例分析
- 學(xué)校生態(tài)教育的課程開發(fā)與實施策略
- 嵌入式AI技術(shù)推動智能農(nóng)業(yè)發(fā)展
- 2025年度鋼筋行業(yè)勞動力租賃合同3篇
- 2024淘寶天貓京東電商平臺客服人員投訴處理合同3篇
- 二零二五年度父子房產(chǎn)交易全權(quán)委托與管理合同3篇
- 二零二五年度文化活動合作分成協(xié)議合同3篇
- 2025企業(yè)外來員工勞動合同范本下載
- 2025年度鋼材訂購與供應(yīng)合同3篇
- 二零二五年度草原草場承包租賃合同含草原資源開發(fā)與利用協(xié)議3篇
- 江西省外經(jīng)貿(mào)融資擔(dān)保有限公司招聘筆試題庫2024
- 2024年國家公務(wù)員考試行政職業(yè)能力測驗真題及答案
- 2024年便攜式儲能行業(yè)分析報告
- 2024-2025學(xué)年廣東省廣州市廣州大附中初三3月教學(xué)質(zhì)量監(jiān)測考試數(shù)學(xué)試題(理甲卷)版含解析
- 科研倫理與學(xué)術(shù)規(guī)范期末考試試題
- 2024年秋季人教版新教材七年級上冊語文全冊教案(名師教學(xué)設(shè)計簡案)
- 2024中華人民共和國農(nóng)村集體經(jīng)濟(jì)組織法詳細(xì)解讀課件
- 2025屆湖南省長沙市青竹湖湘一外國語學(xué)校七年級數(shù)學(xué)第一學(xué)期期末經(jīng)典試題含解析
- 2024中國食藥同源大健康產(chǎn)業(yè)消費洞察與產(chǎn)業(yè)發(fā)展分析白皮書
- 郵政銀行借款合同
- 2024屆廣州市番禺區(qū)重點名校中考數(shù)學(xué)全真模擬試題含解析
評論
0/150
提交評論