Docsity

Database Storage and Indexing: An Overview, Lecture notes of Database Management Systems (DBMS)

Partial preview of the text

Download Database Storage and Indexing: An Overview and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

7– 1 Overview of Storage and Indexing ( 1. 1. 1 )

Overview of Storage and Indexing

Oct 2017

Overview

✥ How does the DBMS storage data?

✥ Why is I/O is so important for database operations?

✥ Why are indices useful?

File Organization and Indexing

✥ A relation is typically stored as a file of records

✥ Rows are packed into records (pages)

✥ The simplest file structure is an unordered file or heap file ✦ Records are stored in random order ✦ The file manager is able to retrieve and store a particular record by its rid

✥ An index is a data structure that organizes data records to optimize certain kinds of retrieval operations ✦ They allow the efficient retrieval of records that satisfy a search condition on the search key ✦ Multiple indexes can be created on the same file (i.e table)

Data Entries

✥ A data entry is a record stored in an index file

✥ A data entry with search value k (denoted as k*) contains enough information to locate all records with search key value k

✥ There are 3 ways to store a data entry:

  1. A data entry k* is an actual data record (with search key value k)
  2. A data entry is a (k, rid) pair, where rid is the record id of the data record with search key k
  3. A data entry is a (k, rid − list) pair, where rid − list is a list of record ids with search key value k

✥ If we want multiple indexes on a table, we will need to use 1 and 2 or 3.

Primary and Secondary Indices

✥ An index on the primary key is a primary index

✥ Other indexes are secondary indexes

✥ Two data entries are duplicates if the have same search key

✥ A primary index does not have duplicates

✥ A secondary index could have no duplicates if is specified as UNIQUE

Index Data Structures

✥ Indexes are usually implemented in one of two ways:

- -

Hash-based Indexing Tree-based Indexing

Tree-based Indexing

✥ The tree organizes the keys in order

Non-leaf Pages

Leaf Pages

✥ The inside nodes contain only keys, while the leafs contains the data entries

B+Tree

Root 17 Entries <= 17 Entries > 17 (^5 13 27 )

2* 3*

✥ B+ trees are the most commonly used ✦ They are guaranteed to be balanced ✦ The height of the tree is the length of a path from the root to the leaf (guaranteed to the always the same) ✦ Each node can have up to n children ( fan-out of the tree), so a tree of fan-out n has in the order of nh

5* 7* 8* 14* 16* 22* 24* 27* 29* (^) 33* 34* 38* 39*

Example Clustered B+ Tree

  • Fan out: 2 , Height: 3 (length from root to leaf)

What do we compare?

For the sake of comparison we will use the following queries:

  1. Scan: fetch all records that satisfy a given condition: fetch pages from disk into the buffer pool.
  2. Search with equality: find all records that satisfy a given equality condition. Fetch pages from disk into buffer pool and retrieve matching records.
  3. Search with Range Selection: find all records within certain range: employees with age > 10 AND age < 100. Similar to previous.
  4. Insert Record: Insert a new record. Identify the page where it should be inserted, fetch it, modify it, and write it back. Depending on file organization other pages might need to be changed.
  5. Delete a record. Similar to previous.

What we compare

We will use a very simple cost model

✥ B is the number of data pages when pages are packed and there is no waste ✥ R is the number of records per page ✥ D is the avg. time to read or write a page ✥ C is the cost of comparing a key ✥ F is the fan out of the B+ trees ✥ H is the time to apply the hash function

D= ˜

15 ms, while C and H =˜

100ns, so we will concentrate on I/O

cost. We ignore other details (such as contiguity of pages)

Heap Files

✥ Scan : BD

✥ Search with Equality : BD

✥ Range Search : BD

✥ Insert : 2 D

✥ Delete : Search + D

2

Clustered B+ Tree

  • Fan out: F (F keys per inner node)
    • In leave nodes , on average only 67 % of the space is used, hence we need 1. 5 times the number of leaves we would need if each node was full ( 1. 5 B, as each node uses one page) - Scan : 1.5BD (scan all the leave nodes) - Search with Equality : DlogF 1.5B Use the tree, with depth proportional to logF (number of leave nodes) - Range Search :DlogF 1. 5 B+ cost of retrieving sequential matching rows (find first, scan sequential) - Insert : Search + D (search and insert) - Delete : Search + D (search and destroy)

7– 20 Overview of Storage and Indexing ( 1. 1. 1 ) CSC 370 [email protected]

Heap File with Unclustered Tree Index

  • Assume Index data is 1/10th of the size of the data record
    • So 10 times more index records < key, rid > than data records per leave page
    • Total number of leave pages 0. 15 B (assuming 67 % occupancy rate) - Scan : ∗ Cost of scanning all index leave pages 0.15BD + ∗ Per each key in the index, retrieve one data page (heap file) BRD. Total: BD(0.1 5 + R) - Search with Equality : Search in tree + 1 data page D(logF 0.15B + 1 )