Indexing and Hashingالفهرسة والتجزئة
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- Basic Concepts
- Ordered Indices
- B+-Tree Index
- Static Hashing
- Dynamic Hashing
- Comparison of Ordered Indexing and Hashing
- Index Definition in SQL
- Multiple-Key Access
أقسام الفصل
- 1Basic concepts and index evaluation metricsالمفاهيم الأساسية ومعايير تقييم الفهارس
- 2Ordered indices: primary, secondary, dense and sparseالفهارس المرتبة: الأساسي والثانوي والكثيف والمتناثر
- 3Multilevel indices and multiple-key accessالفهارس متعددة المستويات والوصول بمفاتيح متعددة
- 4Index update: deletion and insertionتحديث الفهرس: الحذف والإدخال
- 5B+-tree index filesملفات فهرس B+-Tree
- 6B-tree insertion example (order M = 5)مثال الإدخال في شجرة رتبتها 5
- 7Hashing, static hashing and bucket overflowالتجزئة والتجزئة الثابتة وفيضان الدلاء
- 8Dynamic hashing and comparison with ordered indexingالتجزئة الديناميكية والمقارنة بالفهرسة المرتبة
- 9Bitmap indices and index definition in SQLفهارس البت وتعريف الفهرس في SQL
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
Basic conceptsالمفاهيم الأساسية
- Indexing mechanisms are used to speed up access to desired data (e.g., author catalog in a library), minimizing the number of disc visits required to fulfill a query.
- Index files are typically much smaller than the original file.
Attribute or set of attributes used to look up records in a file.
The address of the disk block where the instance corresponding to the key can be found.
Ordered indices
- Indices which are sorted
Hash indices
- Search keys distributed uniformly across "buckets" using a "hash function"
Index evaluation metrics (slide 6)
- Access types supported efficiently
- Access time
- Insertion time
- Deletion time
- Space overhead
Primary and secondary indicesالفهرس الأساسي والثانوي
Ordered indices: primary, secondary, dense and sparse
Search key specifies the sequential order of the file; usually, but not necessarily, the primary key.
Search key specifies an order different from the sequential order of the file. E.g., B-tree index, hash-index, bitmap index.
Ordered sequential file with a primary index.
- Secondary index example: file stored by ID, but we search all instructors of a department, or with a salary (range). An index record points to a bucket of pointers to all records with that value.
- Updating indices imposes overhead on database modification: every index on the file must be updated.
- Sequential scan using a primary index is efficient; using a secondary index is expensive — each record access may fetch a new block: 5 to 10 milliseconds per block fetch versus about 100 nanoseconds for memory access.
Dense and sparse index filesالفهرس الكثيف والمتناثر
Ordered indices: primary, secondary, dense and sparse
| Dense index | Sparse index | |
|---|---|---|
| Index records | For every search-key value in the file | For only some search-key values |
| Requirement | لا تذكر الشرائح شيئًا هنا. | Records sequentially ordered on the search-key |
| Space / maintenance | More | Less space and maintenance overhead for insertion and deletion |
| Locating records | Faster | Generally slower |
Locating search-key value K with a sparse index (slide 10)
- 1
Find the index record with the largest search-key value < K.
- 2
Search the file sequentially starting at the record to which that index record points.
- Good tradeoff: a sparse index with an index entry for every block in the file, corresponding to the least search-key value in the block.
Multilevel indexالفهرس متعدد المستويات
Multilevel indices and multiple-key access
- If the primary index does not fit in memory, access becomes expensive.
- Solution: keep the primary index on disk as a sequential file and construct a sparse index on it.
- If even the outer index is too large to fit in main memory, yet another level of index can be created, and so on.
A sparse index of the primary index.
The primary index file.
Multiple-key access and composite indicesالمفاتيح المتعددة والمركبة
Multilevel indices and multiple-key access
WHERE dept_name = "Finance" and salary = 80000 with single-attribute indices (slide 18)
- 1
Use the dept_name index to find Finance instructors; test salary = 80000.
- 2
Use the salary index to find instructors with $80000; test dept_name = "Finance".
- 3
Use both indices to get pointers for each condition, and take the intersection of both sets of pointers.
Contains more than one attribute, e.g. (dept_name, salary); lexicographic ordering: (a1, a2) < (b1, b2) if a1 < b1, or a1 = b1 and a2 < b2.
Handled efficiently
- dept_name = "Finance" and salary = 80000 (only records satisfying both)
- dept_name = "Finance" and salary < 80000
Not handled efficiently
- dept_name < "Finance" and balance = 80000 — may fetch many records that satisfy the first but not the second condition
Index update: deletion and insertionتحديث الفهرس
Dense index
- Deletion: like file record deletion
- Insertion: if the search-key value does not appear in the index, insert it
Sparse index
- Deletion: replace the entry with the next search-key value in the file; if that value already has an entry, delete the entry instead
- Insertion (entry per block): no change unless a new block is created; then insert the first search-key value of the new block
- A search key is deleted from the index only if the deleted record was the only record with that search-key value.
- Multilevel insertion and deletion are simple extensions of the single-level algorithms.
Why B+-trees?لماذا B+-Tree؟
B+-tree index files
Indexed-sequential files
- Performance degrades as the file grows (many overflow blocks)
- Periodic reorganization of the entire file is required
B+-tree index files
- Automatically reorganizes itself with small, local, changes on insertions and deletions
- No reorganization of the entire file
- Minor disadvantage: extra insertion/deletion overhead, space overhead
- Used extensively
B+-tree properties and node structureخصائص B+-Tree
B+-tree index files
A B+-tree with order m (slide 26)
- All paths from root to leaf are of the same length.
- Each node that is not a root or a leaf has between ⌈m/2⌉ and m children.
- A leaf node has between ⌈m/2⌉ and (m−1) values.
- If the root is not a leaf, it has at least 2 children; if the root is a leaf, it can have between 0 and (m−1) values.
The search-key values; in a node K_1 < K_2 < … < K_(m−1).
Pointers to children (non-leaf nodes) or to records or buckets of records (leaf nodes).
Points to the next leaf node in search-key order.
Form a multi-level sparse index on the leaf nodes: keys under P_1 are < K_1; under P_i (2 ≤ i ≤ m−1) are ≥ K_(i−1) and < K_i; under P_m are ≥ K_(m−1).
Queries on B+-treesالبحث في B+-Tree
B+-tree index files
Find record with search-key value V (slide 46)
- 1
C = root.
- 2
While C is not a leaf: let i be the least value such that V ≤ K_i; if none, C = last non-null pointer in C; else if V = K_i, C = P_(i+1), else C = P_i.
- 3
In the leaf: if some K_i = V, follow P_i to the record; else no record exists.
المعطيات
- Search-key values
- 1,000,000
- m
- 100
الصيغة
height ≤ ⌈log_⌈m/2⌉(K)⌉
خطوات الحل
- ⌈m/2⌉ = 50
- log_50(1,000,000) = 4 (as printed on slide 47)
النتيجة
At most 4 nodes are accessed in a lookup, versus around 20 in a balanced binary tree with 1 million keys — each node access may need a disk I/O of around 20 milliseconds.
- A node is generally the same size as a disk block, typically 4 kilobytes; m is typically around 100 (40 bytes per index entry).
- Insertions and deletions can be handled efficiently: the index can be restructured in logarithmic time.
B-tree insertion example (order M = 5)مثال الإدخال
A node can have a maximum of 5 children and 4 values (M−1 data). Insert: C N G A H E K Q M F W L T Z.
| After inserting | Tree (worked out from the slide steps) |
|---|---|
| C, N, G, A | [A C G N] |
| H |
|
| E, K, Q |
|
| M |
|
| F, W, L, T |
|
| Z |
|
- A fifth value makes a node overflow: it splits and its middle value moves to the parent (G after H; M; then T after Z).
- E, K, Q and F, W, L, T are added without any split.
Exercise (slide 44)
- Consider the below set of values and create a B+-Tree with order M=5: 5, 2, 7, 15, 45, 13, 29, 11, 21, 9, 6, 30, 25. (The slide gives no answer.)
Hashing and static hashingالتجزئة الثابتة
Hashing, static hashing and bucket overflow
A technique to quickly locate a data record irrespective of the size of the database.
A unit of storage containing one or more records (typically a disk block); each hash-table location.
The bucket of a record is obtained directly from its search-key value using a hash function. Different keys may map to the same bucket, so the entire bucket is searched sequentially.
The hash function always generates the same bucket address; number of buckets remains constant; simple hash function; good for known size.
المسألة
Slide 52: hash dept_name into 10 buckets — sum of the character codes, modulo 10.
خطوات الحل
- h(Mus) = (77 + 117 + 115) % 10 = 309 % 10
- h(Hist) = (72 + 105 + 115 + 116) % 10 = 408 % 10
- h(Phy) = (80 + 104 + 121) % 10 = 305 % 10
النتيجة
Mus → bucket 9; Hist → bucket 8; Phy → bucket 5. (Slide 51: H(107) % 5 = 2.)
Hash functions and bucket overflowدوال التجزئة والفيضان
Hashing, static hashing and bucket overflow
Worst hash function
- Maps all search-key values to the same bucket
- Access time proportional to the number of search-key values
Ideal hash function
- Uniform: each bucket gets the same number of search-key values
- Random: same number of records per bucket irrespective of the actual distribution
- Bucket overflow (collision) causes: insufficient buckets; skew (many records with the same key; non-uniform hash function). It can be reduced but not eliminated: handled by overflow buckets.
- Overflow chaining (overflow buckets in a linked list) is closed hashing; open hashing (no overflow buckets) is not suitable for database applications.
- A hash index organizes search keys with their record pointers into a hash file structure; hash indices are always secondary indices.
Dynamic hashingالتجزئة الديناميكية
Deficiencies of static hashing (slide 59)
- Too few buckets + growing file → performance degrades due to too much overflows.
- Space allocated for anticipated growth → significant space wasted initially; a shrinking database wastes space again.
- Periodic re-organization with a new hash function is expensive and disrupts normal operations.
- Better solution: allow the number of buckets to be modified dynamically.
Good for databases that grow and shrink in size frequently; adds and removes data buckets on demand. Components: data bucket, flexible hash function, directories (directories store the pointers to buckets).
The number of bits in each directory id; the more records, the more bits (example: global depth = 2).
Comparison of ordered indexing and hashingالفهرسة مقابل التجزئة
Dynamic hashing and comparison with ordered indexing
| Indexing | Hashing | |
|---|---|---|
| Technique (slide 63) | Quickly retrieve records from the database file | Search the location of desired data on disk without using an index structure |
| Main purpose | Basis for rapid random lookups and efficient access of ordered records | Organize data into easily searchable buckets |
| Types | Ordered, primary, secondary, clustered indexing | Static and dynamic hashing |
| Locating records | Data reference holds the address of the disk block | Hash function calculates the direct location of records on disk |
| Better for (slide 62) | Range queries | Records with a specified value of the key |
- In practice: PostgreSQL supports hash indices; Oracle supports static hash organization, but not hash indices; SQLServer supports only B+-trees.
Bitmap indicesفهارس البت
A special type of index designed for efficient querying on multiple keys: a bitmap for each value of the attribute, with as many bits as records — the bit is 1 if the record has the value v, otherwise 0.
المسألة
Bitmap operations (slide 66)
خطوات الحل
- 100110 AND 110011
- 100110 OR 110011
- NOT 100110
- Males with income level L1: 10010 AND 10100
النتيجة
100010; 110111; 011001; 10000.
- Useful for queries on multiple attributes; not particularly useful for single attribute queries.
- Counting the number of matching tuples is even faster.
- Bitmap indices are generally very small compared with relation size.
Index definition in SQLالفهرس في SQL
create index <index-name> on <relation-name> (<attribute-list>) — e.g., create index b-index on branch(branch_name).
Indirectly specifies and enforces that the search key is a candidate key (not really required if the SQL unique integrity constraint is supported).
drop index <index-name>.
- Most database systems allow specification of the type of index, and clustering.
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- Basic concepts and index evaluation metricsالمفاهيم الأساسية ومعايير تقييم الفهارس
- Ordered indices: primary, secondary, dense and sparseالفهارس المرتبة: الأساسي والثانوي والكثيف والمتناثر
- Multilevel indices and multiple-key accessالفهارس متعددة المستويات والوصول بمفاتيح متعددة
- Index update: deletion and insertionتحديث الفهرس: الحذف والإدخال
- B+-tree index filesملفات فهرس B+-Tree
- B-tree insertion example (order M = 5)مثال الإدخال في شجرة رتبتها 5
- Hashing, static hashing and bucket overflowالتجزئة والتجزئة الثابتة وفيضان الدلاء
- Dynamic hashing and comparison with ordered indexingالتجزئة الديناميكية والمقارنة بالفهرسة المرتبة
- Bitmap indices and index definition in SQLفهارس البت وتعريف الفهرس في SQL