Indexing and Hashingالفهرسة والتجزئة

الشريحة 1 من 18

مقدمة الفصل

تفاصيل المصدرالفصل 3 · الشريحة ⁦2⁩
المصدر
الفصل 3 · الشريحة ⁦2⁩

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.
Search keyمفتاح البحث
Attribute or set of attributes used to look up records in a file.
Pointerالمؤشر
The address of the disk block where the instance corresponding to the key can be found.
Two basic kinds of indices (slide 4)

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

Which index evaluation metric is "the extra time it takes to update the index when new data is added"?

تفاصيل المصدرالفصل 3 · الشرائح ⁦3, 4, 6⁩
المصدر
الفصل 3 · الشرائح ⁦3, 4, 6⁩

Primary and secondary indicesالفهرس الأساسي والثانوي

Ordered indices: primary, secondary, dense and sparse

Primary (clustering) index
Search key specifies the sequential order of the file; usually, but not necessarily, the primary key.
Secondary (non-clustering) index
Search key specifies an order different from the sequential order of the file. E.g., B-tree index, hash-index, bitmap index.
Index-sequential file
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.

An instructor file is stored sorted by ID. An index on dept_name is built. What kind of index is it?

تفاصيل المصدرالفصل 3 · الشرائح ⁦7, 13, 14, 15⁩
المصدر
الفصل 3 · الشرائح ⁦7, 13, 14, 15⁩

Dense and sparse index filesالفهرس الكثيف والمتناثر

Ordered indices: primary, secondary, dense and sparse

Dense vs sparse indices (slides 8–11)
Dense indexSparse index
Index recordsFor every search-key value in the fileFor only some search-key values
Requirementلا تذكر الشرائح شيئًا هنا.Records sequentially ordered on the search-key
Space / maintenanceMoreLess space and maintenance overhead for insertion and deletion
Locating recordsFasterGenerally slower

Locating search-key value K with a sparse index (slide 10)

  1. 1

    Find the index record with the largest search-key value < K.

  2. 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.

A sparse index on ID has entries 10101, 32343 and 76766. To find ID 58583, what does the system do?

تفاصيل المصدرالفصل 3 · الشرائح ⁦8–11⁩
المصدر
الفصل 3 · الشرائح ⁦8–11⁩

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.
Outer index
A sparse index of the primary index.
Inner index
The primary index file.
تفاصيل المصدرالفصل 3 · الشرائح ⁦16–17⁩
المصدر
الفصل 3 · الشرائح ⁦16–17⁩

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. 1

    Use the dept_name index to find Finance instructors; test salary = 80000.

  2. 2

    Use the salary index to find instructors with $80000; test dept_name = "Finance".

  3. 3

    Use both indices to get pointers for each condition, and take the intersection of both sets of pointers.

Composite search key
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.
Index on (dept_name, salary) (slide 20)

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

With an index on the composite key (dept_name, salary), which condition can NOT be handled efficiently?

تفاصيل المصدرالفصل 3 · الشرائح ⁦18–20⁩
المصدر
الفصل 3 · الشرائح ⁦18–20⁩

Index update: deletion and insertionتحديث الفهرس

Single-level index update (slides 21, 23)

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.

A sparse index stores an entry for each block of the file. A new record is inserted and fits into an existing block. What changes in the index?

تفاصيل المصدرالفصل 3 · الشرائح ⁦21, 23⁩
المصدر
الفصل 3 · الشرائح ⁦21, 23⁩

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

A file keeps growing and an indexed-sequential file would need overflow blocks and periodic reorganization. What does a B+-tree offer instead?

تفاصيل المصدرالفصل 3 · الشريحة ⁦24⁩
المصدر
الفصل 3 · الشريحة ⁦24⁩

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.
K_i
The search-key values; in a node K_1 < K_2 < … < K_(m−1).
P_i
Pointers to children (non-leaf nodes) or to records or buckets of records (leaf nodes).
P_m in a leaf
Points to the next leaf node in search-key order.
Non-leaf nodes
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).

In a B+-tree of order m = 6, how many children can a node that is neither the root nor a leaf have?

تفاصيل المصدرالفصل 3 · الشرائح ⁦26–29⁩
المصدر
الفصل 3 · الشرائح ⁦26–29⁩

Queries on B+-treesالبحث في B+-Tree

B+-tree index files

Find record with search-key value V (slide 46)

  1. 1

    C = root.

  2. 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. 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)⌉

خطوات الحل

  1. ⌈m/2⌉ = 50
  2. 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.

With 1 million search-key values and m = 100, at most how many nodes are accessed in a B+-tree lookup?

تفاصيل المصدرالفصل 3 · الشرائح ⁦45–47⁩
المصدر
الفصل 3 · الشرائح ⁦45–47⁩

B-tree insertion example (order M = 5)مثال الإدخال

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.
Inserting C N G A H E K Q M F W L T Z (slides 32–43)
After insertingTree (worked out from the slide steps)
C, N, G, A[A C G N]
H
  • root [G]
  • leaves [A C] [H N]
E, K, Q
  • root [G]
  • leaves [A C E] [H K N Q]
M
  • root [G M]
  • leaves [A C E] [H K] [N Q]
F, W, L, T
  • root [G M]
  • leaves [A C E F] [H K L] [N Q T W]
Z
  • root [G M T]
  • leaves [A C E F] [H K L] [N Q] [W 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.)

In the order-5 example, the node (A, C, G, N) is full and H is inserted. What happens?

تفاصيل المصدرالفصل 3 · الشرائح ⁦32, 33, 34, 36, 37, 38, 40, 41, 42, 43, 44⁩
المصدر
الفصل 3 · الشرائح ⁦32, 33, 34, 36, 37, 38, 40, 41, 42, 43, 44⁩
ملاحظات حول المصدر (1)
  • رسوم الأشجار في الشرائح 33–43 صور بلا نص؛ محتوى الجدول محسوب من خطوات الشرائح المطبوعة (الانقسام وصعود القيمة الوسطى)، ويطابق رسوم الشريحتين 39 و43.

Hashing and static hashingالتجزئة الثابتة

Hashing, static hashing and bucket overflow

Hashing
A technique to quickly locate a data record irrespective of the size of the database.
Bucket
A unit of storage containing one or more records (typically a disk block); each hash-table location.
Hash file organization
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.
Static hashing
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.

Static hashing uses H(x) % 5 with x = employee_id. Which bucket does employee 112 go to?

تفاصيل المصدرالفصل 3 · الشرائح ⁦49–52⁩
المصدر
الفصل 3 · الشرائح ⁦49–52⁩

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.

Overflow buckets of a bucket are chained together in a linked list. What is this scheme called, and is it suitable for databases?

تفاصيل المصدرالفصل 3 · الشرائح ⁦54–58⁩
المصدر
الفصل 3 · الشرائح ⁦54–58⁩

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.
Dynamic (extendible) hashing
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).
Global depth
The number of bits in each directory id; the more records, the more bits (example: global depth = 2).

A database grows and shrinks in size frequently. Which hashing scheme suits it?

تفاصيل المصدرالفصل 3 · الشرائح ⁦59–61⁩
المصدر
الفصل 3 · الشرائح ⁦59–61⁩

Comparison of ordered indexing and hashingالفهرسة مقابل التجزئة

Dynamic hashing and comparison with ordered indexing

Ordered indexing vs hashing (slides 62–63)
IndexingHashing
Technique (slide 63)Quickly retrieve records from the database fileSearch the location of desired data on disk without using an index structure
Main purposeBasis for rapid random lookups and efficient access of ordered recordsOrganize data into easily searchable buckets
TypesOrdered, primary, secondary, clustered indexingStatic and dynamic hashing
Locating recordsData reference holds the address of the disk blockHash function calculates the direct location of records on disk
Better for (slide 62)Range queriesRecords 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.

Queries such as "salary between 50000 and 70000" are very common in an application. Which structure should be preferred?

تفاصيل المصدرالفصل 3 · الشرائح ⁦62–63⁩
المصدر
الفصل 3 · الشرائح ⁦62–63⁩

Bitmap indicesفهارس البت

Bitmap index
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)

  • 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.

What is 100110 AND 110011?

تفاصيل المصدرالفصل 3 · الشرائح ⁦64–67⁩
المصدر
الفصل 3 · الشرائح ⁦64–67⁩

Index definition in SQLالفهرس في SQL

create index
create index <index-name> on <relation-name> (<attribute-list>) — e.g., create index b-index on branch(branch_name).
create unique index
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
drop index <index-name>.
  • Most database systems allow specification of the type of index, and clustering.
تفاصيل المصدرالفصل 3 · الشريحة ⁦68⁩
المصدر
الفصل 3 · الشريحة ⁦68⁩

الخلاصة

تفاصيل المصدر
المصدر
الفصل 3

خريطة الفصل

    1. 1مقدمة الفصل
  1. Basic concepts and index evaluation metricsالمفاهيم الأساسية ومعايير تقييم الفهارس

    1. 2Basic conceptsالمفاهيم الأساسية
  2. Ordered indices: primary, secondary, dense and sparseالفهارس المرتبة: الأساسي والثانوي والكثيف والمتناثر

    1. 3Primary and secondary indicesالفهرس الأساسي والثانوي
    2. 4Dense and sparse index filesالفهرس الكثيف والمتناثر
  3. Multilevel indices and multiple-key accessالفهارس متعددة المستويات والوصول بمفاتيح متعددة

    1. 5Multilevel indexالفهرس متعدد المستويات
    2. 6Multiple-key access and composite indicesالمفاتيح المتعددة والمركبة
    1. 7Index update: deletion and insertionتحديث الفهرس
  4. B+-tree index filesملفات فهرس B+-Tree

    1. 8Why B+-trees?لماذا B+-Tree؟
    2. 9B+-tree properties and node structureخصائص B+-Tree
    3. 10Queries on B+-treesالبحث في B+-Tree
    1. 11B-tree insertion example (order M = 5)مثال الإدخال
  5. Hashing, static hashing and bucket overflowالتجزئة والتجزئة الثابتة وفيضان الدلاء

    1. 12Hashing and static hashingالتجزئة الثابتة
    2. 13Hash functions and bucket overflowدوال التجزئة والفيضان
  6. Dynamic hashing and comparison with ordered indexingالتجزئة الديناميكية والمقارنة بالفهرسة المرتبة

    1. 14Dynamic hashingالتجزئة الديناميكية
    2. 15Comparison of ordered indexing and hashingالفهرسة مقابل التجزئة
  7. Bitmap indices and index definition in SQLفهارس البت وتعريف الفهرس في SQL

    1. 16Bitmap indicesفهارس البت
    2. 17Index definition in SQLالفهرس في SQL
    1. 18الخلاصة