mysql dba課程及文件07btree僅供網(wǎng)絡(luò)培訓學員使用_第1頁
mysql dba課程及文件07btree僅供網(wǎng)絡(luò)培訓學員使用_第2頁
mysql dba課程及文件07btree僅供網(wǎng)絡(luò)培訓學員使用_第3頁
mysql dba課程及文件07btree僅供網(wǎng)絡(luò)培訓學員使用_第4頁
mysql dba課程及文件07btree僅供網(wǎng)絡(luò)培訓學員使用_第5頁
已閱讀5頁,還剩45頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

內(nèi)部注意

MySQL數(shù)據(jù)庫培訓性能優(yōu)化B+樹索引

Topics 違者

DataStructure&Algorithms

B+TreeIndexinMySQL

HowtouseB+Treeindex

Multi-RangeRead(MRR)

Reference

DataStructure&Alg 違者

BinarySearch

BinarySearchTree

BalancedBinaryTree

B+Tree

BinarySearch

違者

searchasortedarray

lookattheelementinthemiddle

Ifthekeyisequaltothat,thesearchisfinished.

Ifthekeyislessthanthemiddleelement,doabinarysearchonthefirsthalf.

Ifit'sgreater,doabinarysearchofthesecondhalf.

BinarySearch(A[0..N-1],value,low,high){if(high<low)

return-1//notfound

mid=low+(high-low)/2if(A[mid]>value)

returnBinarySearch(A,value,low,mid-1)elseif(A[mid]<value)

returnBinarySearch(A,value,mid+1,high)

else

returnmid//found

}

Usingbinarysearchtofindvalue48inarray(5、

、、、、、、、、)

SequentialSearchaveragecost:

(1+2+3+4+5+6+7+8+9+10)/10=5.5

BinarySearchaveragecost:

(4+3+2+4+3+1+4+3+2+3)/10=2.9

BinarySearchTree(BS 違者

Feature:

Theleftsubtreeofanodecontainsonlynodeswithkeyslessthanthenode'skey.

Therightsubtreeofanodecontainsonlynodeswithkeysgreaterthanthenode'skey.

Boththeleftandrightsubtreesmustalsobebinarysearchtrees.

BinarySearchTree 違者

BalancedBinaryTree

違者

Definition

abinarytree

theheightofthetwosub-treesofeverynodeneverdifferbymorethan1

B+TREE

違者

TheprimaryvalueofaB+treeisinstoringdataforefficientretrievalinablock-orientedstoragecontext—inparticular,filesystems.

IncontrasttoaB-tree,“allrecords”arestoredattheleaflevelofthetree;onlykeysarestoredininteriornodes.

B+treeshaveveryhighfanout(typicallyontheorderof100ormore),whichreducesthenumberofI/Ooperationsrequiredtofindanelementinthetree.

B+treeheight:2Recordsperpage:4Fanout:5

+Treeinsert 違者

LeafPage

Full

IndexPage

Full

Action

No

No

cetherecordinsortedpositionintheappropriateleafpage

Yes

No

Splittheleafpage

ceMiddleKeyintheindexpageinsortedorder.

Leftleafpagecontainsrecordswithkeysbelowthemiddlekey.

Rightleafpagecontainsrecordswithkeysequaltoorgreaterthanthemiddlekey.

Yes

Yes

Splittheleafpage.

Recordswithkeys<middlekeygototheleftleafpage.

Recordswithkeys>=middlekeygototherightleafpage.

Splittheindexpage.

Keys<middlekeygototheleftindexpage.

Keys>middlekeygototherightindexpage.

Themiddlekeygoestothenext(higherlevel)index.

IFthenextlevelindexpageisfull,continuesplittingtheindexpages.

B

B+TreeInsert

違者

Insertkey28

Insertkey70

Insertkey95

B+TreeRotation

違者

B+treescanincorporaterotationtoreducethenumberofpagesplits.

Arotationoccurswhenaleafpageisfull,butoneofitssiblingpagesisnotfull.

Ratherthansplittingtheleafpage,wemovearecordtoitssibling,adjustingtheindicesasnecessary.Typically,theleftsiblingischeckedfirst(ifitexists)andthentherightsibling.

Insertkey70

B+TreeDeletion 違者

FillFactor

50%

TreeDeletion

違者

LeafPage

BelowFillFactor

IndexPage

BelowFillFactor

Action

No

No

Deletetherecordfromtheleafpage.Arrangekeysinascendingordertofillvoid.

Ifthekeyofthedeletedrecordappearsintheindexpage,usethenextkeytoreceit.

Yes

No

Combinetheleafpageanditssibling.Changetheindexpagetoreflectthechange.

Yes

Yes

binetheleafpageanditssibling.

2.Adjusttheindexpagetoreflectthechange.binetheindexpagewithitssibling.

Continuecombiningindexpagesuntilyoureachapagewiththe

correctfillfactororyoureachtherootpage.

B+

B+TreeDeletion

違者

Deletekey70

Deletekey25

Deletekey60

B+TreeIndexinMySQ 違者

B+TreeIndex

InnoDBStorageEngine

MyISAMStorageEngine

B+TreeIndex 違者

B+TreeIndex

Clusteredindex

Secondaryindex(Nonclusteredindex)

Clusteredindex

Leafpagestorewholerecord

Secondaryindex

Leafpagestorerowidentifier

B+TreeHeight

IO

Performance

Disk

Randomread

Sequentialread

B+Treeindex 違者

ClusteredVSSecondary

Clusteredindexkey=4bytes

Secondaryindexkey=4bytes

Keypointer=6bytes

Averagerowlength=300bytes

Pagesize=16K=16384bytes

Averagenodeoccupancy=70%(bothforleafandindexpage)

Fan-outforclusteredindex=16384*70%/(4+6)=1000

Fan-outforsecondaryindex=16384*70%/(4+6)=1000

Averagerowperpageforclusteredindex=16384*70%/300=35

Averagerowperpageforclusteredindex=16384*70%/(4+6)=1000

H

ClusteredIndex

SecondaryIndex

2

1000*35=35,000

1000*1000=1,000,000

3

(1000)2*35=35,000,000

(1000)2*1000=1,000,000,000

4

(1000)3*35=35,000,000,000

(1000)3*1000=1,000,000,000,000

B+TreeIndexinInnoD 違者

IOT

ClusteredIndex

Leafpagestoreallrowdata

SecondaryIndex

Leafpagestorekey&primarykeyinfo

Bookmarklookup

16Kperpage

HighFanout

B+TreeIndexinInnoD 違者

B+TreeIndexinInnoD

CREATETABLEUserInfo(

useridINTNOTNULLAUTO_INCREMENT,

usernameVARCHAR(30),registdateDATETIME,

VARCHAR(50),

PRIMARYKEY(userid),

UNIQUEKEYidx_username(username),KEYidex_registdate(registdate)

);

違者

CREATETABLE

idx_username_constraint(usernameVARCHAR(30),PRIMARYKEY(username)

);

CREATETABLEUserInfo(

useridINTNOTNULLAUTO_INCREMENT,

usernameVARCHAR(30),registdateDATETIME,

VARCHAR(50),

PRIMARYKEY(userid)

);

CREATETABLE

idx_username(

useridINTNOTNULL,usernameVARCHAR(30),PRIMARYKEY

(username,userid)

);

CREATETABLE

idx_registdate(

useridINTNOTNULL,registdateDATETIME),PRIMARYKEY

(registdate,userid)

);

B+TreeIndexinInnoD 違者

INSERToperation

STARTTRANSACTION;

INSERTINTOUserInfovalues(aaa,bbb,ccc);INSERTINTOidx_username_constraint(bbb);INSERTINTOidx_username(bbb,aaa);INSERTINTOidx_registdate(ccc,aaa);COMMIT;

B+TreeIndexinMyISA 違者

HeapTable

Allindexisnonclustered/secondary

ThedifferencebetweenPrimaryKeyandKeyisnotNULLandunique

Leafpagestoredataaddress

1Kperpage

B+TreeIndexinMyISA 違者

HowtouseB+TreeIn 違者

Cardinality

NotuseB+Treeindexsituation

Compoundindex

Coveringindex

Indexwithincludedcolumn

Cardinality 違者

Thecountofnoneuniquerecord

Highselectivity

UsingB+treetoaccesslessrecord

SELECT*FROMstudentWHERE=’M’

lowselectivity

SELECT*FROMstudentWHEREname=‘DavidJiang’

Namehighselectivity

NotuseB+Treeindex 違者

SecondaryIndex

Needlotsofrandomread

Accessmorerows(20%+)

Optimizerchoosesequentialreadinstead

SELECT*FROMorderdetails

WHEREorderid>10000andorderid<102000;

CompoundIndex 違者

Indexwithmulticolumn

(a,b)

a:1,1,2,2,3,3

(a,b):(1,1),(1,2),(2,1),(2,4),(3,1),(3,2)

b:1,2,1,4,1,2

CompoundIndexAdvan 違者

Canbeused

SELECT*FROMtWHEREa=?

SELECT*FROMtWHEREa=?ANDb=?

Cannotbeused

SELECT*FROMtWHEREb=?

Sort(a,b),nofilesortneed

SELECT*FROMtwherea=?ORDERBYb

IndexCoverage

CoveringIndex

違者

Getdata(result)throughsecondaryindex

Nobookmarklookupneeded

Usingindexhint

(primarykey1,primarykey2,…,key1,key2,…)

SELECTkey2FROMtableWHEREkey1=xxx;

SELECTprimarykey2,key2FROMtableWHEREkey1=xxx;

SELECTprimarykey1,key2FROMtableWHEREkey1=xxx;

SELECTprimarykey1,primarykey2,key2FROMtableWHEREkey1=xxx;

(a,b)compoundindex

Canquerycolumn(a)or(a,b)

Generally,cannotqueryon(b)

However

SELECTCOUNT(1)FROMbuy_logWHEREbuy_date>='2011-01-01'

ANDbuy_date<'2011-02-01

(userid,buy_date)compoundindex

CREATETABLEUserInfo(

useridINTNOTNULLAUTO_INCREMENT,

usernameVARCHAR(30),registdateDATETIME,

VARCHAR(50),

PRIMARYKEY(userid),

UNIQUEKEYidx_username(username, ),KEYidex_registdate(registdate)

);

Indexwithincludedco 違者

SELECT FROMUserInfoWHEREusername=‘David’

Needbookmarklookup,twotreesneedtobelookupHowtooptimizequerylikethis?

Indexwithincludedco 違者

Howaboutcoveringindex(username, )

However,wedonotneed tobesort

Weneedindexusername,butalsoincludecolumn

Useextraspacetoimproveperformance

Indexwithincludedco 違者

CREATETABLEUserInfo(

useridINTNOTNULLAUTO_INCREMENT,

usernameVARCHAR(30),registdateDATETIME,

VARCHAR(50),

PRIMARYKEY(userid),

UNIQUEKEYidx_username(usernaKEYidex_registdate(registdate)

);

BEGIN;

INSERTINTOUs

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論