bases and Information Systems 1 Part 3: Storage Structures and Indices Prof. Dr. Stefan Böttcher Fakultät EIM, Institut für Informatik Universität Paderborn WS 2009 / 2010 Contents: - database buffer - storage structures - indices
base buffer - why? unsafe data database in main memory DB in main memory organized in pages load store safe data database on disk disk read or write = 100.000-1.000.000 main memory accesses optimize / reduce disk access changes of tuples are collected in pages + page is stored on disk from time to time bases and Information Systems I WS 2009/2010 Storage Structures and Indices 2/12
base buffer - own management unsafe data Log safe data load database database in on main memory log and disk store DBMS has own main memory management because of: - controlled page replacement (needed for recoverability) - page replacement optimized for query optimization may store uncommitted data on disk recovery uses additional Log to be able to restore consistent data bases and Information Systems I WS 2009/2010 Storage Structures and Indices 3/12
Coupling database buffer and application program database in main memory Communication area application program in main memory load store database on disk data exchange between application program and main memory part of DB Application program must not directly read or write to database in main memory Communication area can be used to write to database in main memory bases and Information Systems I WS 2009/2010 Storage Structures and Indices 4/12
Storage structures and indices primary index database secondary index bases and Information Systems I WS 2009/2010 Storage Structures and Indices 5/12
Storage Structures - B-trees p = maximum number of sub-tree pointers q = given number of pointers, q p q-1 = number of keys unique tree depth at least p/2 pointers per node (except root and leaf nodes) Ptr 1 Key 1 Ptr 1 Ptr 2 Key q-1 Ptr q-1 Ptr q B- 1 B- 2 B- q key in sub-b-tree K < key K key in sub-b- K+1 bases and Information Systems I WS 2009/2010 Storage Structures and Indices 6/12
Example of B-trees 64 bit addresses for data, i.e. 8 Byte for each pointer 4K = 4096 Byte per disk block (and main memory page) 4 Byte for Integer-key at most 2^32 values ==> pages can store (p-1) triples of (pointer to left sub-tree, key, pointer to data record) plus pointer to right-most sub-tree ==> fan-out of the tree is (4096-8) div ( 8+4+8 ) + 1 = 205 each page can address at most 204 data records B-tree depth items addressable at least / at most 1 0 204 2 2*102 205*204 4*10 4 3 2*102*102 205*205*204 8*10 6 4 2*102*102*102 205*205*205*204 1.7*10 9 bases and Information Systems I WS 2009/2010 Storage Structures and Indices 7/12
Improvement: B+-trees - inner nodes p = maximum number of pointers q = given number of pointers, q p q-1 = number of keys unique tree depth at least p/2 pointers per node (except root and leaf nodes) Ptr 1 Key 1 Ptr 1 Ptr 2 Key q-1 Ptr q-1 Ptr q B + - 1 B + - 2 B + - q Key in Sub-B + - K < Key K Key in Sub-B + - K+1 bases and Information Systems I WS 2009/2010 Storage Structures and Indices 8/12
Leaf nodes of B+-s contain pointers to the data do not contain any pointer to a sub-tree contain a pointer to the next leaf node Ptr 1 Key 1 Ptr 1 Ptr 2 Key q-1 Ptr q-1 Leaf- Ptr q keys correspond with keys in the data. bases and Information Systems I WS 2009/2010 Storage Structures and Indices 9/12
Root nodes of B+-trees p = maximum number of pointers minimum number of pointers: 2 (except for trivial case of less than 2 data records, where root node is leaf node) trivial case is ignored here bases and Information Systems I WS 2009/2010 Storage Structures and Indices 10/12
Example of B+-trees 64 bit addresses for data, i.e. 8 Byte for each pointer 4K = 4096 Byte per disk block (and main memory page) 4 Byte for Integer-key at most 2^32 values ==> leaf pages can store one pointer to next leaf page (8 Byte) and 4096 div ( 4+8 ) = 340 (key, data pointer) - pairs ==> fan-out 340 internal pages can store one pointer to last sub-tree (8 Byte) and 4096 div ( 4+8 ) = 340 (Key, data pointer) - pairs ==> fan-out 341 B + -tree depth items addressable at least / at most 1 0 340 2 2*170 341*340 10 5 3 2*171*170 341*341*340 4* 10 7 4 2*171*171*170 341*341*341*340 10 10 bases and Information Systems I WS 2009/2010 Storage Structures and Indices 11/12
Storage Structures - Hashing hash-function h : key bucket (=data container) insert: full? overflow container search: also in overflow container h(key) overflow container bases and Information Systems I WS 2009/2010 Storage Structures and Indices 12/12