Index & B+ tree

Index

An index is a data structure that speeds up selections on the search key field(s)

Search key:any subset of the fields of a relation
the search key is not the same as the key(minimal set of fields that uniquely identify a record in a relation).

Entries in an index: (k, r)
k: search key
r: records or record ids

Index分类:

  • Clustered index
    • records sorted & stored in the order of search key
    • row data is stored with the key, so that's why the entry is (key, record) in index
    • For MySQL database, it automatically creates a clustered index for
      1. primary key if exists;
      2. if no pk, they create a clustered index for 1st unique key;
      3. if no pk and unique key, they use row ID (this is a hidden attribute for MySQL)
  • unclustered index:
    • records are not sorted in key order

search key 分类:

  • unique search key
  • composite search key: a list of fields

for different queries, the index can be useful or useless

index performance.png

B+ tree

B+ tree is a data structure that helps search data, it is often used in DBMS or file system

  • Search Tree
  • in the disk, often makes 1 node = 1 block
  • there is a parameter called d in the B+ tree

结构:

root, internal node, leaf node

  • root has [1,2d] keys
  • each internal node and leaf node has [d,2d] keys
  • each leaf node connects to its next sibling by the end pointer, so leaves are like a linked list. which supports range query efficiently
待补充.png
待补充.png

B+树结构在硬盘上的存储:

assumption:
key size = 4 bytes
pointer size = 8 size
Block size = 4 KB

according to the rules, each node has the number of keys between d and 2d
then if one node stores maximum keys:
42d + 8(2d+1) <= 4096
d = 170

In practice, d is often 100
Typically, the fill-factor(average proportion storage usage in a node) is 2/3, so the average number of keys in node is 200*2/3 = 133

Keep in mind that an index is composed of a key and its corresponding records/ record ids.
When we are ready for the keys' storage, let's look back to the records:

  • for a height 1 tree(tree only with a single root), it can store 133 records ;
  • for a height 2 tree: it can store133^2= 17689 records (134*133 to be exact)
  • for a height 3 tree: it can store133^3= 2352637 records (1342*133)
  • for a height 4 tree: it can store 133^4= 312900721 records (1343* 133 that's the reason why the B+ tree is not so deep)

When storing these records in a buffer pool, we can see:

  • Level 1, it only needs 1 page = 4KB cause there is only one node
  • Level 2, it needs 133 pages = 532KB
  • Level 3 it need 17,689 pages = 70, 756KB ~ 70MB

B+ tree上的检索

  • Equality search
    1.start as the root
    2.proceed down to the leaf

  • Range query [-,a], [a,b], [b, -]
    [-,a]
    1.find the left-most leaf
    2.sequential traverse until ends

    [a,b]
    1.find the first leaf in the range
    2.sequential traverse until ends

    [b,-]
    1.find the leaf with b
    2.sequential traverse until ends

B+ tree上index的插入

In theory, only inserting an index enough
risk: overflow => disobey the [d,2d] rule

3 cases:
a.leaf node has free space to keep key
b.leaf node overflows when inserting the key but its parent has free space
c. leaf node overflows when inserting the key and its parent is also full of storage

case1:
find leaf where k belongs to, then insert

case2:
find leaf where k belongs to, then insert (logically)
now this leaf node is overflow, we should split the node and insert the middle into its parent node

case 3:
just like case 2, but do it recursively

Difference between splitting a leaf node and an internal node:


Before splitting for a leaf node.png

After split, 5 keys and 7 pointers


After splitting for a leaf node.png

Before splitting an internal node, there are 5 keys and 6 pointers;
After splitting, there are still 5 keys and 6 pointers


spliiting an internal node.png

Insertion cascaded:
splitting a leaf node may lead to splitting of its parent and ancestors.

B+ tree 上index的删除

Deletion is the opposite operation of insertion
risk: underflow
strategy: merging

-If a node is below the min capacity after deletion, which is producing underflow, try the following in the given order

  1. move a key from an immediate left sibling;
  2. move a key from an immediate right sibling;
  3. merge with an immediate left sibling;
  4. merge with an immediate right sibling
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,258评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,335评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,225评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,126评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,140评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,098评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,018评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,857评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,298评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,518评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,400评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,993评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,638评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,661评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352

推荐阅读更多精彩内容