45
Database Systems An Overview of Storage and Indexing Chapter 8 of the textbook

1 Database Systems An Overview of Storage and Indexing Chapter 8 of the textbook

Embed Size (px)

Citation preview

Page 1: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

1

Database Systems

An Overview of Storage and Indexing

Chapter 8 of the textbook

Page 2: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

2

The Database Architecture

Plan Executor

Files & AccessMethods

Buffer Manager

Disk Space Manager

Parser

Operator EvaluatorOptimizer

LockManager

TransactionManager

RecoveryManager

Index Files

Data Files

System Catalog

Web Forms SQL interfaceApp Front ends

Page 3: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

3

Learning Objectives for Chapter 8

Identify the input, output and function of each component in the architecture diagram

Be able to describe the relationships between different classifications of indexes Alternatives, clustering, uniqueness, denseness, primary

For each cell in the table on the “cost of operations” slide , explain the underlying algorithm and justify the cost.

Definitions: Index only plans, composite keys

Given a workload, choose an optimal index configuration.

Page 4: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

4

Table of Contents: Chapter 8

8.1 Data on external storage Database architecture

8.2 File organizations and indexing Indexes Alternatives for k* data entries Index classification: clustered, dense, unique, primary

8.3 Index search data structures Hash-based indexes Tree indexes

8.4 Comparing File Organizations

8.5 Indexes and performance tuning

Page 5: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

5

Alternative File Organizations

Many alternatives exist, each ideal for some situations, and not so good in others: 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. Hashed files: Best for quick equality access.

RDBMSs typically use heap files plus indexes

A tree index using alternative 1 is similar to a sorted file, a hash index using alternative 1 is the same as a hash file.

Page 6: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

6

How the Records Fall

16

9

22

41

62

15

2

11

2

9

11

15

16

22

41

62

16

9

41

2

22

62

11

15

Heap Sorted HashedBucket

0

1

2

3

Page 7: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

7

Indexes An index on a file speeds up selections on the search

key fields for the index. Any subset of the fields, or a prefix of a field, of a relation

can be the search key for an index on the relation. Search key is not the same as key (minimal set of fields that

uniquely identify a record in a relation).

An index contains a collection of data entries, and supports efficient retrieval of all data entries k* with a given key value k. Given data entry k*, we can find record with key k in at most

one disk I/O. (Details soon …)

Page 8: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

8

Typical index architecture

Used by modern DBMSs

What are typical search structures in CS?

(Data file)

Data entries

Data Records

(Index File)k* k* k*

Search structure: given a key, it finds the data entry for that key

Page 9: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

9

Alternatives for Data Entry k* in Index

In a data entry k* we can store:1. Data record with key value k, or

• In this case the previous page is not accurate

2. <k, rid of data record with search key value k>, or

3. <k, list of rids of data records with search key k>

Choice of alternative for data entries is orthogonal to the indexing technique used to locate data entries with a given key value k. Examples of indexing techniques: B+ trees, hash-based

structures Typically, index contains auxiliary information that directs

searches to the desired data entries

Page 10: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

10

Alternative 1: k* holds the data

42 Red

24 Green

30 Blue

10 Blue

14 Red

21 Blue INDEX

.

.

.

Page 11: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

11

Alternative 2: One pointer per k*

10*

10*

21*

24*

30*

42*

10 Blue

42 Red

10 Green

30 Blue

21 Red

24 Blue

INDEX

Page 12: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

12

Alternative 3: 1 or more pointers per k*

10*

18*

21*

24*

30*

10 Blue

18 Red

10 Green

30 Blue

21 Red

24 Blue

INDEX

Page 13: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

13

Alternatives for Data Entries (Contd.)

Alternative 1: If this is used, index structure is a file organization for data

records (instead of a Heap file or sorted file). At most one single-attribute index on a given collection of data

records can use Alternative 1. (Otherwise, data records are duplicated, leading to redundant storage and potential inconsistency.)

Alternatives 2 and 3: Alternative 3 more compact than Alternative 2, but leads to

variable sized data entries even if search keys are of fixed length.

Page 14: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

14

Clustered vs. Un-clustered Index Suppose that Alternative (2) is used for data entries, and that

the data records are stored in a Heap file. To build clustered index, first sort the Heap file (with some free space

on each page for future inserts). Overflow pages may be needed for inserts. (Thus, order of data recs is

`close to’, but not identical to, the sort order.)

Index entries

Data entries

direct search for

(Index File)

Data Records

data entriesCLUSTERED

(Data file)

Data entries

Data Records

UNCLUSTERED

Page 15: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

15

Index Classification Primary vs. secondary: If search key contains primary key, then

called primary index. In practice, primary means alternative 1

Unique index: Search key contains a candidate key.

Clustered vs. un-clustered: If order of data records is the same as, or `close to’, order of data entries, then called clustered index. Alternative 1 implies clustered. A file can be clustered on at most one single-attributesearch key. Cost of retrieving data records through index varies greatly based on

whether index is clustered or not!

Page 16: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

16

Index Classification (Cont.)• Dense vs. Sparse: If

there is at least one data entry per search key value (in some data record), then it’s dense.

– Alternative 1 always leads to dense index.

– Every sparse index is clustered!

– Sparse indexes are smaller; however, some useful optimizations are based on dense indexes.

Ashby, 25, 3000

Smith, 44, 3000

Ashby

Cass

Smith

22

25

30

40

44

44

50

Sparse Indexon

Name Data File

Dense Indexon

Age

33

Bristow, 30, 2007

Basu, 33, 4003

Cass, 50, 5004

Tracy, 44, 5004

Daniels, 22, 6003

Jones, 40, 6003

Page 17: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

17

Index ClassificationClustered, sparse indexes are smaller; they work well for range searches

and sorting.

But…data blocks are difficult to keep sorted and some useful optimizations are based on dense indexes.

Note: one file can have at most one clustered index - all of the additional indices must be unclustered.

sparse dense

clustered YES YES

unclusteredNO! YES

Page 18: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

18

Hash-based Index Examples

hage

Smith,44,3000

Jones,40,6003

Tracy,44,5004

Ashby,25,3000

Basu,33,4003

Bristow,29,2007

Cass,50,5004

Daniels,22,6003

h(age)=00

h(age)=01

h(age)=10

3000

3000

5004

5004

4003

2007

6003

6003

h sal

h(sal)=00

h(sal)=11

Employees file hashed on ageIndex on salary

Page 19: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

19

Hash-Based Indexes

Good for equality selections.

Index is a collection of buckets. Bucket = primary page plus zero or more overflow pages.

Buckets contain data entries.

Hashing function h: h(r) = bucket in which (data entry for) record r belongs. h looks at the search key fields of r. No need for “index entries” in this scheme.

Page 20: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

20

B+ Tree Indexes

Leaf pages contain data entries, and are chained (prev & next) 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-leaf

Pages

Pages (Sorted by search key)

Leaf

Page 21: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

21

Example B+ Tree

Find 28*? 29*? All > 15* and < 30*

Insert/delete: Find data entry in leaf, then change it. Need to adjust parent sometimes. And change sometimes bubbles up the tree

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 22: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

22

Assume an employee file is organized by the search key <age, salary>.

We will consider various file organizations, and operations like scan, equality, range, insert, delete.

For each file organization, we will compute the cost of each operation.

Comparing File Organizations

Page 23: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

23

Comparing File Organizations

Heap files (random order; insert at eof)

Sorted files, sorted on <age, sal>

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

Heap file with unclustered B + tree index on search key <age, sal>

Heap file with unclustered hash index on search key <age, sal>

Page 24: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

24

Operations to Compare

Scan: Fetch all records from disk

Equality search

Range selection

Insert a record

Delete a record

Page 25: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

25

Cost Model for Our Analysis

We ignore CPU costs, for simplicity: B: The number of data pages R: Number of data records per page F: Fanout, number of index records per page D: (Average) time to read or write disk page Measuring number of page I/O’s ignores gains of pre-fetching

a sequence of pages; thus, even I/O cost is only approximated.

Average-case analysis; based on several simplistic assumptions.

Good enough to show the overall trends!

Page 26: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

26

Assumptions in Our Analysis

Heap Files: Equality selection on key; exactly one match.

Sorted Files: Files compacted after deletions.

Indexes: Alt (2), (3): data entry size = 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 size

Page 27: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

27

Assumptions (contd.)

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

indexes.

Range searches: We use tree indexes to restrict the set of data records

fetched, but ignore hash indexes.

Page 28: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

28

Cost of Operations (a) Scan (b) Equality (c ) Range (d) Insert (e) Delete

(1) Heap BD 0.5BD BD 2D Search +D

(2) Sorted BD Dlog 2B D(log 2 B + # pgs with match recs)

Search + BD

Search +BD

(3) Clustered

1.5BD Dlog F 1.5B D(log F 1.5B + # pgs w. match recs)

Search + D

Search +D

(4) Unclust. Tree index

BD(R+0.15) D(1 + log F 0.15B)

D(log F 0.15B + # match recs)

Search + 2D

Search + 2D

(5) Unclust. Hash index

BD(R+0.125) 2D BD Search + 2D

Search + 2D

Several assumptions underlie these (rough) estimates!

Page 29: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

29

Indexes and Performance Tuning

One of a DBA’s most important duties is to deal with performance problems

One of the most effective methods for improving performance is to choose the best indexes for the given workload

Why not index everything? What are the two costs of an index?

Part of a DBA’s job: choose indexes so the database will “run fast”. What information does the DBA need, to do this?

Page 30: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

30

Understanding the Workload

For each query in the workload: Which relations does it access? Which attributes are retrieved? Which attributes are involved in selection/join conditions? How

selective are these conditions likely to be?

For each update in the workload: Which attributes are involved in selection/join conditions? How

selective are these conditions likely to be? The type of update (INSERT/DELETE/UPDATE), and the attributes that

are affected.

Page 31: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

31

Choice of Indexes

What indexes should we create? Which relations should have indexes? What field(s) should be the

search key? Should we build several indexes?

What kinds of indexes should we create? Clustered? Hash/tree?

Before creating an index, must also consider the impact on updates in the workload! Trade-off: Indexes can make queries go faster, updates slower.

Require disk space, too.

Also consider dropping indexes

Page 32: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

32

Choice of Indexes (Contd.)

One approach: Consider the most important queries in turn. Consider the best plan using the current indexes, and see if a better plan is possible with an additional index. If so, create it. Obviously, this implies that we must understand how a DBMS evaluates

queries and creates query evaluation plans!

Second approach: Use an Index Tuning Wizard [163]. Inputs: A workload (it can also capture a workload) and an amount of

disk storage for indexes Output: an index configuration

Page 33: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

33

Index Selection Guidelines Attributes in WHERE clause are candidates for index keys.

Exact match condition suggests hash index. Range query suggests tree index.

• Clustering is especially useful for range queries; can also help on equality queries if there are many duplicates.

Try to choose indexes that benefit as many queries as possible. Since only one index can be clustered per relation, choose it based on important queries that would benefit the most from clustering.

Multi-attribute search keys should be considered when a WHERE clause contains several conditions. Order of attributes is important for range queries. Such indexes can sometimes enable index-only strategies for

important queries.

Page 34: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

34

Index-Only Plans

A number of queries can be answered without retrieving any tuples from one or more of the relations involved if a suitable index is available.

For index-only strategies, clustering is not important!

SELECT E.dno, COUNT(*)FROM Emp EGROUP BY E.dno

SELECT E.dno, MIN(E.sal)FROM Emp EGROUP BY E.dno

SELECT AVG(E.sal)FROM Emp EWHERE E.age=25 AND E.sal BETWEEN 3000 AND 5000

<E.dno>

<E.dno,E.sal>

Tree indexMay help

<E. age,E.sal> or<E.sal, E.age>

Tree index!

Page 35: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

35

Examples of Clustered Indexes

B+ tree index on E.age can be used to get qualifying tuples. How selective is the condition? Is the index clustered?

Consider the GROUP BY query. If many tuples have E.age > 10, using E.age

index and sorting the retrieved tuples may be costly.

Clustered E.dno index may be better!

Equality queries and duplicates: Clustering on E.hobby helps!

SELECT E.dnoFROM Emp EWHERE E.age>40

SELECT E.dno, COUNT (*)FROM Emp EWHERE E.age>10GROUP BY E.dno

SELECT E.dnoFROM Emp EWHERE E.hobby=Stamps

Page 36: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

36

Indexes with Composite Search Keys

Composite Search Keys: Search on a combination of fields. Equality query: Every field value is

equal to a constant value. E.g. wrt <sal,age> index:

• age=20 and sal =75 Range query: Some field value is

not a constant. E.g.:• age =20; or age=20 and sal > 10

Data entries in index sorted by search key to support range queries.

sue 13 75

bob

cal

joe 12

10

20

8011

12

name age sal

<sal, age>

<age, sal> <age>

<sal>

12,20

12,10

11,80

13,75

20,12

10,12

75,13

80,11

11

12

12

13

10

20

75

80

Data recordssorted by name

Data entries in indexsorted by <sal,age>

Data entriessorted by <sal>

Examples of composite keyindexes using lexicographic order.

Page 37: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

37

Composite Search Keys

To retrieve Emp records with age=30 AND sal=4000, an index on <age,sal> would be better than an index on age or an index on sal. Choice of index key orthogonal to clustering etc.

If condition is: 20<age<30 AND 3000<sal<5000: Clustered tree index on <age,sal> or <sal,age> is best.

If condition is: age=30 AND 3000<sal<5000: Clustered <age,sal> index much better than <sal,age> index!

Composite indexes are larger, updated more often.

Page 38: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

38

Indexes in the real world

Types of indexes supported Oracle, SQLServer and DB2 support only B+Tree indexes.

Postgres supports hash indexes but does not recommend using them.

MySQL? Everyone uses hash indexes for hash joins, but they are

constructed on the fly.

Page 39: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

39

Clustering in the real world Oracle, SQL Server:

Declares a clustered index on any primary key. It uses alternative 1.

DB2, Postgres The user can define an index to be clustered. The DBMS uses alternative 2. At first the index will be clustered. If you also specify a percentage of free space, it will place

subsequent inserts/updates (Postgres:updates only) in sorted order so the index should stay clustered for a while.

It’s up to you to recluster the index if overflow chains get to be long.

MySQL

Page 40: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

40

Slides From Chapter 9

Relevant to Buffer Management

9.3 Disk Space Management

9.4 Buffer Management

Page 41: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

41

Disk Space Management

The lowest layer of DBMS software manages space on disk.

Higher levels call upon this layer to: allocate/de-allocate a disk page read/write a disk page

A request for a sequence of disk pages must be satisfied by allocating the pages sequentially on disk! Higher levels don’t need to know how this is done, or how free space on the disk is managed.

Page 42: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

42

Buffer Management in a DBMS

Data must be in RAM for DBMS to operate on it!

Table of <frame#, pageid> pairs is maintained.

DB

MAIN MEMORY

DISK

disk page

free frame

Disk Page Requests from Higher Levels

BUFFER POOL

choice of frame dictatedby replacement policy

Page 43: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

43

When a Disk Page is Requested ...

If requested disk page is not in buffer pool: Choose a frame for replacement If frame is dirty, write it to disk Read requested page into chosen frame

Pin the page and return its address.

Page 44: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

44

More on Buffer Management

Requestor of page must unpin it, and indicate whether page has been modified: Dirty bit is used for this What if requestor does not unpin the page?

Page in pool may be requested many times, a pin count is used. A page is a candidate for replacement

iff pin count = 0.

Page 45: 1 Database Systems  An Overview of Storage and Indexing  Chapter 8 of the textbook

45

Buffer Replacement Policy

Frame is chosen for replacement by a replacement policy: Least-recently-used (LRU), Clock, MRU etc.

Policy can have big impact on # of I/O’s; depends on the access pattern.