overview of storage and indexing · 5 file organizations § heap (random order) files:suitable when...

24
1 Overview of Storage and Indexing Chapter 8 (part 1)

Upload: others

Post on 29-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

1

Overview of Storage and Indexing

Chapter 8(part 1)

Page 2: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

2

Motivationv DBMS stores vast quantities of data v Data is stored on external storage devices and fetched

into main memory as needed for processingv Page is unit of information read from or written to

disk. (in DBMS, a page may have size 8KB or more).v Data on external storage devices :

§ Disks: Can retrieve random page at fixed costBut reading several consecutive pages is much cheaper than reading them in random order

§ Tapes: Can only read pages in sequenceCheaper than disks; used for archival storage

v Cost of page I/O dominates cost of typical database operations

Page 3: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

3

Structure of a DBMS:Layered Architecture

Query Optimizationand Execution

Relational Operators

Files and Access Methods

Buffer Management

Disk Space Management

DB

These layersmust considerconcurrencycontrol andrecovery

v external storage access§Disk space manager manages persistent data

§Buffer manager stages pages from externalstorage to main memorybuffer pool.

§File and index layers make calls to buffer manager.

Page 4: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

4

Files versus Indices

v File organization :§ Method of arranging a file of records on external storage.§ Record id (rid) is sufficient to physically locate record

v Indexes :§ Indexes are data structures that allow to find record ids

of records with given values in index search key fields

Page 5: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

5

File Organizations

§ Heap (random order) files: Suitable when typical access is a file scan retrieving all records.

§ Sorted Files: Best if records must be retrieved in some order, or only a `range’ of records is needed.

§ Indexes: Data structures to organize records to optimize certain kinds of retrieval operations.

• Speed up searches for a subset of records, based on values in certain (“search key”) fields

• Updates are much faster than in sorted files.

Page 6: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

6

Alternatives for Data Entry k* in Indexv Data Entry : Records stored in index file

§ Given search key value k, provide for efficient retrieval of all data entries k* with value k.

v In a data entry k* , alternatives include that we can store:§ alternative 1: Full data record with key value k, or§ alternative 2: <k, rid of data record with search key value k>, or§ alternative 3: <k, list of rids of data records with search key k>

v Choice of above 3 alternative data entries is orthogonal to indexing technique used to locate data entries.§ Example indexing techniques: B+ trees, hash-based structures, etc.

Page 7: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

7

Alternatives for Data Entriesv Alternative 1: Full data record with key value k

§ Index structure is file organization for data records (instead of a Heap file or sorted file).

§ At most one index on a given collection of data records can use Alternative 1. Otherwise, data records are duplicated, leading to redundant storage and potential inconsistency.

§ If data records are very large, this implies size of auxiliary information in index is also large.

Page 8: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

8

Alternatives for Data Entriesv Alternatives 2 (<k, rid>) and 3 (<k, list-of-rids>):

§ Data entries typically much smaller than data records.

v Comparison:§ Both better than Alternative 1 with large data records,

especially if search keys are small.

§ Alternative 3 more compact than Alternative 2, but leads to variable sized data entries even if search keys are of fixed length.

Page 9: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

9

Index Classification

v Primary vs. secondary index: § If search key contains primary key, then called primary index.

v Clustered vs. unclustered index : § If order of data records is the same as, or `close to’, order of data

entries, then called clustered index.

Page 10: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

10

Index Clustered vs Unclustered

v Observation 1: § Alternative 1 implies clustered. True ?

v Observation 2: § In practice, clustered also implies Alternative 1 (since

sorted files are rare).v Observation 3:

§ A file can be clustered on at most one search key.v Observation 4:

§ Cost of retrieving data records through index varies greatly based on whether index is clustered or not !!

Page 11: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

11

Index Clustered vs Unclustered

v Observation 1: § Alternative 1 implies clustered. True ?

v Observation 2: § In practice, clustered also implies Alternative 1 (since

sorted files are rare).v Observation 3:

§ A file can be clustered on at most one search key.v Observation 4:

§ Cost of retrieving data records through index varies greatly based on whether index is clustered or not !!

Page 12: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

12

Clustered vs. Unclustered Index

Index entries

Data entries

direct search for

(Index File)(Data file)

Data Records

data entries

Data entries

Data Records

CLUSTERED UNCLUSTERED

Suppose Alternative (2) is used for data entries.

Page 13: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

13

Clustered vs. Unclustered Indexv Use Alternative (2) for data entriesv Data records are stored in Heap file.

§ To build clustered index, first sort the Heap file § Overflow pages may be needed for inserts. § Thus, order of data recs is close to (not identical to) sort order.

Index entries

Data entries

direct search for

(Index File)(Data file)

Data Records

data entries

Data entries

Data Records

CLUSTERED UNCLUSTERED

Page 14: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

14

B+ Tree Indexes

v Index leaf pages contain data entries, and are chained (prev & next)v Index non-leaf pages have index entries; only used to direct searches:

P0 K 1 P 1 K 2 P 2 K m P m

index entry

Non-leafPages

Pages (Sorted by search key)

Leaf

Page 15: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

15

Example B+ Tree

v Find: 29*? 28*? All > 15* and < 30*v Insert/delete: Find data entry in leaf, then change it.

2* 3*

Root

17

30

14* 16* 33* 34* 38* 39*

135

7*5* 8* 22* 24*

27

27* 29*

Entries < 17 Entries >= 17

Note how data entriesin leaf level are sorted

Page 16: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

16

Hash-Based Indexes

v Index is a collection of buckets. § Bucket = primary page plus zero or more overflow pages. § Buckets contain data entries.

v Hashing function h: § h(r) = bucket in which data entry for record r belongs. § h looks at search key fields of r.§ No need for “index entries” due to one-level index file

v Good for equality selections.

Page 17: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

17

Cost Model for Our Analysis

v Notes:§ We ignore CPU costs, for simplicity.§ Measuring number of page I/Os ignores gains of pre-

fetching a sequence of pages§ Thus even I/O cost is only approximated. § Average-case analysis; based on simplistic assumptions.

☛ Good enough to show overall trends!

Page 18: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

18

Cost Model for Our Analysis

Variables :§ B: The number of data pages§ R: Number of records per page§ D: (Average) time to read or write disk page

Page 19: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

19

Comparing File Organizations

v Heap files (random order; insert at eof)

v Sorted files, sorted on <age, sal>

v Clustered B+ tree file, Alternative (1), search key <age, sal>

v Heap file with unclustered B + tree index on search key <age, sal> (Alt 2)

v Heap file with unclustered hash index on search key <age, sal> (Alt 2)

Page 20: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

20

Operations to Compare

v Scan: Fetch all records from diskv Equality searchv Range selectionv Insert a recordv Delete a record

Page 21: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

21

Assumptions in Our Analysisv Heap Files:

§ Equality selection on key; exactly one match.v Sorted Files:

§ Files compacted after deletions.v Indexes:

§ Alt (2), (3): data entry size/pointers = 10% size of record § Hash: No overflow buckets.

• 80% page occupancy => File size = 1.25 data size§ Tree: 67% occupancy (this is typical).

• Implies file size = 1.5 data sizev Scans:

§ Leaf levels of a tree-index are chained.§ Index data-entries plus actual file scanned for unclustered indexes.

v Range searches:§ We use tree indexes to restrict set of data records fetched, but

ignore hash indexes.

Page 22: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

22

Cost of Operations (a) Scan (b)

Equality (c ) Range (d) Insert (e) Delete

(1) Heap (2) Sorted (3) Clustered (4) Unclustered Tree index

(5) Unclustered Hash index

☛ Several assumptions underlie these (rough) estimates!

Page 23: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

23

Summary

v Many alternative file organizations exist, each appropriate in some situation.

v If selection queries are frequent, sorting the file or building an index is important.§ Hash-based indexes only good for equality search.§ Sorted files and tree-based indexes best for range search;

also good for equality search. § Files rarely kept sorted in practice; B+ tree index is better.

v Index is a collection of data entries plus a way to quickly find entries with given key values.

Page 24: Overview of Storage and Indexing · 5 File Organizations § Heap (random order) files:Suitable when typical access is a file scan retrieving all records. § Sorted Files:Best if records

24

Summary

v Data entries can be :§ actual data records,§ <key, rid> pairs, or § <key, rid-list> pairs.

v Can have several indexes on a given file of data records, each with a different search key.

v Indexes can be classified as clustered vs. unclustered, v Differences have important consequences for

utility/performance of query processing