lecture 14 b-trees, b*trees and virtual b-trees (sections 9.8-9.15)

24
File Structure Dr. M. Rahgozar In the Name of God In the Name of God Lecture 14 Lecture 14 B-trees, B*trees B-trees, B*trees and and Virtual B-trees Virtual B-trees (Sections 9.8-9.15) (Sections 9.8-9.15)

Upload: atalo

Post on 23-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

Lecture 14 B-trees, B*trees and Virtual B-trees (Sections 9.8-9.15). P. W. D. M. P. T. W. A. B. C. D. G. I. M. N. P. R. S. T. U. W. آشنايي با ايندکسهاي B-Tree. ساختار يک ايندکس B-Tree چگونه است؟ هر نود ميتواند يک رکورد با تعداد ثابتي کليد (مثلا 100 ) باشد. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar

In the Name of GodIn the Name of God

Lecture 14Lecture 14

B-trees, B*trees B-trees, B*trees and and

Virtual B-treesVirtual B-trees (Sections 9.8-9.15)(Sections 9.8-9.15)

Lecture 14Lecture 14

B-trees, B*trees B-trees, B*trees and and

Virtual B-treesVirtual B-trees (Sections 9.8-9.15)(Sections 9.8-9.15)

Page 2: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-2

با B-Treeايندکسهاي آشناييايندکس ساختار است؟ B-Tree يک چگونه

با نود هر رکورد يک کليد ميتواند ثابتي .100مثال ) تعداد باشد(

کليد گره در تعداد ظرفيت هر تمام تا نصف .بين ميباشد آن

نمود براي به ناضافه که نودکليد :شده تکميلآن ظرفيتي

به نودآن ميکنند جديد نود 2را ،تقسيم کليد و از بزرگترين به نود 2يکي باالتر جديد .سطح ميکند پيدا ارتقا

نمودن از حذف که نودکليد به ظرفيتي است مينيممآن :رسيدهباعث ممکن نمودن يا مجاور نودبا نودادغام است کليدها متوازن

گردد،آنها بين آن و از بايد باالترسطح نود ، پس روز نيز شود.به

P W

D M P T W

A B C D G I M N P R S T U W

Page 3: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-3

کليد B-Treeايندکس در جستجويچيست؟ B-Treeايندکس يک در کليد يجستجوروش

نود ، kکليد جستجويبراي 1( اوFل آورده( Root )ريشهبايستي حافظه بهشود.

نود 2( اين کليدهاي بين ميشود Ki کليد ،در :بطوريکه ،جستجو

و Kiيا 1( نود در کليد باشد k ≤ Ki اولين

باشد. Ki -1 < k ≤ Ki يا2(

يافتن 3( صورت مربوط ، Ki در ميشود هنود آورده حافظه ،به

برگ )تکرار 2عمل و نود به تا گردد داده( Leaveمي آدرس و برسيم. شود پيدا نظر مورد

P W

D M P T W

A B C D G I M N P R S T U W

Page 4: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-4

ايندکس ايجاد در B-Treeکليد ايجادروش( کليدInsert )در B-Tree است؟ چگونه

قبل 1( روش برگ با کليد( n )نود به . k مربوط ميشود جستجوصورت 2( الزم در فضاي :وجود

کليد k نود ،ميشود اضافهبه اگر نود کليد زرگترينباز kو در سطح نود ،باشد بزرگترموجود

.باالتر ميشود بروز نيزصورت • نود در بودن :پر

نود )1( دو به را آن نمود( n+1و( )nبايستي ،تقسيمدر kکليد 2( نود را دو از نمود يکي اضافه ،جديدسپس 3( نمود باالترسطح نود و بروز نيز ،راباعث خود که 4( است .ريشهتا 3و 2 اعمال تکرارممکن بشود P W

D M P T W

A B C D G I M N P R S T U W

Page 5: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-5

ايندکس در کليد ايجاد -B مثالTree

Input Sequence: C S D T A M P I B W N G U R K E H O L J Y Q Z F X V

C D S

Insertion of C, S, D, Tinto the initial page

D

DA C S

Insertion of A causes node to split and the largest key in each leaf node)D and T(to beplaced in the root node

T T

T

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 6: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-6

ايندکس در کليد ايجاد -B مثالTree

TD

A C M PI TS

P

M and P are inserted into the rightmost leaf node,then insertion of I causes it to split

D

Input Sequence: C S D T A M P I B W N G U R K E H O L J Y Q Z F X V

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 7: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-7

ايندکس در کليد ايجاد -B مثالTree

A B G N

PD M

C I M P

Insertions of B,W,N, and G into leaf nodes causesanother split and the root is now full

W

D WS T

Input Sequence: C S D T A M P I B W N G U R K E H O L J Y Q Z F X V

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 8: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-8

ايندکس در کليد ايجاد -B مثالTree

Insertion of U proceeds without incident, but R would have to be inserted into the rightmost leaf, which is full

A B G N

PD M

C I M P

W

D US T W

Input Sequence: C S D T A M P I B W N G U R K E H O L J Y Q Z F X V

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 9: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-9

ايندکس در کليد ايجاد -B مثالTree

Insertion of R causes the rightmost leaf node to split, insertion into the root causes the root to split and the tree grows to level three

P W

D M P T W

A B C D G I M N P R S T U W

Input Sequence: C S D T A M P I B W N G U R K E H O L J Y Q Z F X V

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 10: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-10

ايندکس در کليد ايجاد -B مثالTree

Insertions of K,E,H,O,L,J,Y,Q, and Z, continue with another node split

P Z

D I M P T Z

A B C D E G H I J K L M

Q R S T U W Y Z

N O P

Input Sequence: C S D T A M P I B W N G U R K E H O L J Y Q Z F X V

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 11: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-11

ايندکس در کليد ايجاد B-Tree مثال

Insertions of F, X, and V finish the insertion of the alphabet

I P Z

D G I T X ZM P

A B C D

E F G H I

J K L M

N O P

Q R S T

U V W X

Y Z

Input Sequence: C S D T A M P I B W N G U R K E H O L J Y Q Z F X V

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 12: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-12

ايندکس B-Tree خواص

چه B-Treeايندکس رسمي دارد؟ خواصيبطور(Formal definition)

درجه B-Tree ايندکس يک مي m (Order)با زير خواص دارايباشد:

ماکزيمم نود . فرزند mهر دارد

ريشه از غير نود برگها (Root)هر .فرزند m/2الاقل (Leaves)و دارد

فرزند الاقل ريشهنود . دو باشد برگ همان ريشه که هنگامي مگر دارد

برگهاتمام ( Leaves) سطح در . يک دارند قرار

ايندکس برگهامجموعه مرتب يک و تشکيل کامل را کليدها از شدهميدهد.

P W

D M P T W

A B C D G I M N P R S T U W

Page 13: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-13

ايندکس B-Tree خواص

Worst-Case)؟ حالت بدتريندر B-Treeدر جستجو تعداد Search)

جستجو الزم تعداد به B-Treeدر ی بستگي کليد يک يافتن سطوح براي .تعداد دارد

حالت در ظرفيت فقط بدترين از و نيمي شده استفاده نود سطوح هر تعداد.ماکزيمم ميباشد

يک درجه B-Treeدر کليد رابطه ،mبا سطوح و Nتعداد حالت dتعداد بدترين در : با است برابر

N ≥ (2*[m/2] d-1 ) → d ≤ (1 + logm/2(N/2))

مثال:

کليد تعداد درجه B-Treeو N=1000000اگر :m=512از باشد

d ≤ 1 + log256 500000 → d ≤ 3.37

اين سطوح بنابر ميباشد 3ماکزيمم تعداد

Page 14: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-14

ايندکس در کليد B-Treeحذف

کليد ) حذف است؟ B-Treeدر( Deletionروش چگونهکليد حذف نود )kبراي (:nاز

نود )1( از ( nاگر ظرفيت بيش باشد مينيمم داشته کليد :مجاز کليدk شده حذف، کليد اين صورتيکه در کليد زرگبو باالييسطح نودباشد( nنود )ترين

. شود بروز بايستي نيز

(2Merge : ( نود کليدها مينيممبه( nاگر مجاز و باشد رسيدهظرفيتآن مجاور نود در موجود بدهد (Sibling) فضاي :اجازه

يکديگر با نود دو ،شده ادغامهر کليدk شود حذف ،مي باالييسطح نودو. ميگردد بروز نيز

) چرا؟)

Page 15: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-15

ايندکس در کليد B-Treeحذف

است؟ B-Treeدر( Deletionکليد )حذفروش چگونهنود )kکليد حذفبراي ...(:nاز ادامه( )

(3Redistribute( :نود مجاز به( nاگر ظرفيت از مينيمم يکي و باشد رسيدهمجاور باشد کهنودهاي يکي آنها پدر مينيمم ( Sibling )نود از مجاز بيش

باشد داشته :کليد

نود کليدها دو شوند تقسيم بين ،مي

کليد سپسk شده حذف،

پس آن، و .باالترسطح نود از ميگردد بروز نيز

Page 16: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-16

ايندکس در کليد حذف -B مثالTree

• Deleting “T” falls into case 1• Deleting “U” falls into case 1• Deleting “C” falls into case 2: Merge node 2 with node 3

(:1مثال)

Page 17: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-17

ايندکس در کليد حذف -B مثالTree

• Deleting “W” falls into case 3: – Redistribute keys between node 4 and node 5

• Deleting “M” allows for two possibilities: case 3 or 1– Merge Node 3 with Node 2; or– Redistribute keys between Node 3 and Node 4

... :1مثال) ادامه(

Page 18: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-18

I P Z

M P T X ZD G I

F G H I

J K L

N O P

Q R S Y Z

U V W

M

E

T

X

A B C D

ايندکس در کليد حذف -B مثالTree

(:2مثال)

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 19: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-19

I P ZA B C

M P T X ZD G I

F G H I

J K L

N O P

Q R S Y Z

U V W

Removal of key C :Change occurs only in leaf nodeD

M

E

T

X

A B D

ايندکس در کليد حذف -B مثالTree

... :2مثال) ادامه(

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 20: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-20

I O Z

M O T X ZD F I

F G H I

J K L

N O

Q R S Y Z

U V W

Result of deleting P :P changes to O in the second level and the root

M

E

T

X

A B C D

ايندکس در کليد حذف -B مثالTree

... :2مثال) ادامه(

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 21: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-21

I P Z

M P T X ZD I

F G

J K L

N O P

Q R S Y Z

U V W

Result of deleting H:Caused an underflow,and two leaf nodes were merged

M

E

T

X

A B C D

I

ايندکس در کليد حذف -B مثالTree

... :2مثال) ادامه(

Prof. Hyoung-Joo Kim, Comp Eng, Seoul National Univ

Page 22: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-22

کليدها مجدد B-Tree در توزيع B-Treeدر( Redistributionکليدها )مجدد توزيعهای کاربرد

کدامند؟

کليدها مجدد مجاور بينتوزيع نود نود از کهدو انجام( sibling )باشند پدر يکپذيراست.

باعث ايجاديا حذفهنگام جويي کليد . I/Oدر صرفه ميشود آن اندختن تاخير به يا

کليد هنگام نود )،ايجاد کليدهاي تعداد باشد )ماکزيممبه( nاگر Keyرسيدهoverflow ):

مجاور نودهاي از يکي که صورتي الزم در باشد فضاي داشته ،را

کليدها با مجدد نود )توزيع شدن شکسته از نود دو جديد( nبين نود ايجاد و. ميشود جلوگيري

نود )،حذفهنگام کليدهاي تعداد مجاز به( nاگر باشد )مينيمم Keyرسيدهunderflow ):

مجاور نودهاي از يکي که صورتي اضافي در باشد کليد ،داشته

کليدها با مجدد نود )توزيع حذف از نود دو .nبين ميشود( جلوگيري

Page 23: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-23

ديگر B-Treeانواعاست؟ B*Treeايندکس چگونه

:B-Treeنوعي آن در که باشد مي

با 1( نود .ظرفيت 2/3 الاقلهر دارد کليد خود

شدن عمل 2( کمک شکسته به مجدد نودها به توزيع االمکان حتي تاخيرکليدها. شود مي انداخته

تا ريشهنود ظرفيت 3( باشد مي ديگر نودهاي از :بيش

درصورتSplitting، کدام هر جديد داشته ظرفيت2/3 نودهاي کليدباشند.

بلکه ، Splitting هنگام• شود نمي تقسيم جديد نود دو به نود يک :هيچگاه

نود 1( هم مجاور دو ،ادغامبا

سپس 2( نود و سه به شوند تبديل ،مي

کدام بطوري 3( هر .ظرفيت2/3 که باشند داشته کليد

Page 24: Lecture 14 B-trees, B*trees  and  Virtual B-trees (Sections 9.8-9.15)

File Structure Dr. M. Rahgozar14-24

ديگر B-Treeانواعاست؟ Virtual B-Treeايندکس چگونه

آن B-Treeنوعي در که روش ميباشد استفاده Buffering of Pagesازميگردد:

نودها )نگهداري از حافظه ( Pagesتعدادي در RAM در جويي صرفه باعثيا ديسک به دسترسي .I/Oتعداد ميشود

اينصورت دسترسي در لزوم نود هنگام يک و ،به شده رزرو فضاي به اوFلحافظه در موجود يک نودهاي به احتياجي شد پيدا نود اگر و شود مي رجوع

I/O. نميباشد جديد

انجام لزوم درصورتI/O حافظه به جديد نود يک آوردن صفحات ،و از يکيمد است تکه نشده استفاده و حذفي آ شده جاي جديد .انرنود ميگيرد

(Least Recently Used )

ديگر روش روش که ،LRUبجاي باشد مي االمکان اين Fي مربوط حت صفحاتحافظه به در باالتر داشته سطوح به شده نگاه مربوط صفحات نودهاي و

جايگزين .برگ شوند