Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file...

26
Access Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples can be inserted, retrieved, updated and deleted two simple file organizations “heap” files: any record can be placed on any page fast, simple insertion supports scan operations - retrieval of all tuples in a table no efficient way to retrieve specific tuples sorted files: records are sorted according to the value of one (or more) attributes can support table scan in sorted order log time retrieval of specific tuples (binary search) insertions may be expensive Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Transcript of Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file...

Page 1: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

File Organization• file organization is about which tuples to place on

each page, and how specific tuples can be inserted,retrieved, updated and deleted

• two simple file organizations“heap” files: any record can be placed on any page

• fast, simple insertion• supports scan operations - retrieval of all

tuples in a table• no efficient way to retrieve specific tuples

sorted files: records are sorted according to the valueof one (or more) attributes

• can support table scan in sorted order• log time retrieval of specific tuples (binary

search)• insertions may be expensive

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 2: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Indexes

• an index is a data structure used for file organization• indexes may be used to support

• efficient retrieval of specific tuples, e.g., the record ofan employee with a specified employee ID

• efficient retrieval of ranges of tuples, e.g., the recordsof employees with start dates in 2010

• ordered tuple scans, e.g., the records of allemployees, ordered by surname

• other operations, e.g., insertion, deletion, enforcementof integrity constraints

• there are many types of indexes, e.g., tree-structured,hash-based

• there may be more than one index on a single table

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 3: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Example: A Clustered B+-tree10 Davis

14 Smith

17

21

27

Taylor

Garner

Dawson

31

39

Jones

Weddell

44

46

Hoff

Ryan

57

66

73

Ashton

Truman

McNair

77

83

Salem

Walsh

84

90

Parker

Strong

Green95

14

39

46

83

73

27

root node

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 4: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Clustering

• A relation whose tuples are grouped into blocksbased on the value of attribute (field) A is said to beclustered on A. An index on attribute A is called aclustered index.

• Indexes that do not have this property are calledunclustered indexes

• A relation that is sorted on A is clustered on A.However, a relation that is clustered on A need not bestrictly sorted on A.

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 5: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Example: An Unclustered B+-tree Index

Data PagesIndex Pages

Page 1

Page 2

Page 3

Page 4

Page 5

Page 6

Page 010 Davis

14 Smith

17

21

27

Taylor

Garner

Dawson

31

39

Jones

Weddell

44

46

Hoff

Ryan

57

66

73

Ashton

Truman

McNair

77

83

Salem

Walsh

84

90

Parker

Strong

Green95

Ashton

Davis

Dawson

Garner

McNair

Parker

Ryan

Smith

Strong

Taylor

Truman

Walsh

Weddell

Jones

Hoff

GreenDawson

Jones

Strong

Ryan

Salem

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 6: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

B-tree blocks

• Non-Leaf blocks• each block stores at most m values and m + 1 pointers• each block stores at least bm/2c values and bm/2c+ 1

pointers

P0 | V1 | P1 | V2 | · · · | Vm | Pm

• Leaf blocks• may contain the tuples themselves (called Type 1 in

the textbook)• illustrated on Slide 3

• may contain key values plus tuple identifiers (calledType 2 and Type 3in the textbook)

• illustrated on Slide 5• Type 2 and Type 3 are distinguished by the way they

handle duplicate search keys

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 7: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Terminology• Type 1 Indexes

• are clustered by definition (why?)• are sometimes called index-organized files

• alternative to heap files and sorted files

• Type 2 and Type 3 Indexes• often unclustered, but may be clustered (how?)

• There can be at most one clustered index per relation(why?), and that index is sometimes called theprimary index.

• A table can have multiple unclustered indexes, andthey are sometimes called secondary indexes

• A dense index includes all search key values in its leafnodes. A sparse index only includes one search keyper data block.

• a sparse index only makes sense if it is built on therelation’s clustering attribute

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 8: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

B-tree Insertions

1. Determine leaf block where new tuple belongs.2. If there is room in the block, place the tuple in it.3. If there is no room, find an empty block, and move

half of the records into the new block. This is calledsplitting.

4. Add an entry for the new block in the parent indexblock.

5. If the index block is full, it may split. In this case, themiddle pointer is promoted to the next higher indexlevel.

6. Splitting may continue all the way to the root of theb-tree.

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 9: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Insertion Example

Page 5

Page 7

Page 0

Page 1

Page 2

Page 3

Page 4

Page 6

(new page)

77

83

Salem

Walsh

new tuple

10 Davis

14 Smith

17

21

27

Taylor

Garner

Dawson

31

39

Jones

Weddell

44

46

Hoff

Ryan

57 Ashton

14

39

46

83

73

27

root node

84

90

95 Green

Strong

Parker

73

66 Truman

McNair

59 Thomas

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 10: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Insertion Example (cont.)

77

83

Salem

Walsh

new tuple

10 Davis

14 Smith

17

21

27

Taylor

Garner

Dawson

31

39

Jones

Weddell

44

46

Hoff

Ryan

57 Ashton

14

83

73

27

root node

84

90

95 Green

Strong

Parker

73

66 Truman

McNair

59 Thomas

59

39

46

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 11: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Insertion Example (cont.)

77

83

Salem

Walsh

new tuple

10 Davis

14 Smith

17

21

27

Taylor

Garner

Dawson

31

39

Jones

Weddell

44

46

Hoff

Ryan

57 Ashton

14

83

84

90

95 Green

Strong

Parker

73

66 Truman

McNair

59 Thomas

59

3927

46

73root node

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 12: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

B-tree Deletions

1. Determine leaf block where tuple is located.2. Remove the tuple from the leaf block.3. If the block is less than half full, either:

• distribute remaining tuples to the block’s sibling,remove the block from the B-tree, and delete theblock’s pointer from the parent index node, or

• steal some tuples from the block’s siblings, and placethem in the block

4. If a leaf block is removed, its pointer must be deletedfrom its parent’s index node. Deletion of pointers maycascade all the way to the root.

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 13: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Deletion Example10 Davis

14 Smith

17

21

27

Taylor

Garner

Dawson

31

39

Jones

Weddell

44

46

Hoff

Ryan

57

66

73

Ashton

Truman

McNair

77

83

Salem

Walsh

84

90

Parker

Strong

Green95

14

39

46

83

73

27

root node

delete this

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 14: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Deletion Example (cont’d)10 Davis

31

39

Jones

Weddell

44

46

Hoff

Ryan

57

66

73

Ashton

Truman

McNair

77

83

Salem

Walsh

84

90

Parker

Strong

Green95

39

46

83

73

27

root node

17 Taylor

17

21

27

Garner

Dawson

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 15: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Deletion Example (cont’d)10 Davis

31

39

Jones

Weddell

44

46

Hoff

Ryan

57

66

73

Ashton

Truman

McNair

77

83

Salem

Walsh

84

90

Parker

Strong

Green95

39

46

83

73

27

root node

17 Taylor

17

21

27

Garner

Dawson

delete this

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 16: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Deletion Example (cont’d)10 Davis

31

39

Jones

Weddell

44

46

Hoff

Ryan

57

66

73

Ashton

Truman

McNair

77

83

Salem

Walsh

84

90

Parker

Strong

Green95

39

46

83

73

27

root node

17 Taylor

1727 Dawson

fix this

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 17: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Deletion Example (cont’d)10 Davis

31

39

Jones

Weddell

44

46

Hoff

Ryan

57

66

73

Ashton

Truman

McNair

77

83

Salem

Walsh

84

90

Parker

Strong

Green95

83

73

root node

17 Taylor

27 Dawson27

39 46

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 18: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Hash-based Indexes

• basic idea: use hashing to map attribute values topage numbers

• fast access to specific tuples, but no support for rangeretrievals or sorted scans

• retrieval performance of static hashing candeteriorate over time due to bucket overflows causedby tuple insertions

• extensible hashing tries to avoid this problem

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 19: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Static Hashing

44 Hoff

0

1

2

3

h(x)

10 Davis

17 Taylor

14 Smith

21 Garner

27 Dawson

Jones31

39 Weddell

83 Walsh

Assume:x h(x)10 214 217 121 227 031 039 144 283 3

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 20: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Extensible Hashing

0

1

2

3

h(x)

directory

10 Davis

17 Taylor

14 Smith

21 Garner

27 Dawson

Jones31

39 Weddell

83 Walsh

Assume:x h4(x)10 214 217 121 227 031 039 183 3

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 21: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Extensible Hashing Directory Extension

44 Hoff

0

1

2

3

h(x)

directory

14 Smith

44 Hoff

4

5

6

7

10 Davis

17 Taylor

14 Smith

21 Garner

27 Dawson

Jones31

39 Weddell

83 Walsh

Assume:x h4(x) h8(x)10 2 214 2 617 1 121 2 227 0 031 0 439 1 144 2 683 3 7

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 22: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Extensible Hashing Block Allocation

46 Ryan

Brinks61

0

1

2

3

h(x)

directory

14 Smith

44 Hoff

4

5

6

7

10 Davis

17 Taylor

14 Smith

21 Garner

27 Dawson

Jones31

39 Weddell

83 Walsh

Assume:x h8(x)10 214 617 121 227 031 439 144 646 061 483 7

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 23: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Exploiting Access Methods

• DBMS may have multiple access methods for a singlerelation

• For each query, the DBMS query optimizer must selectan access method for each relation involved in aquery.

• The task of choosing which access methods areavailable falls to the database administrator (DBA),and is called physical design.

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 24: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

The Physical Schema

create index LastnameIndexon Employee(Lastname);

drop index LastnameIndex

Effects of LastnameIndex:• May speed up processing of queries involvingLastname

• May increase execution time forinsertions/deletions/updates of tuples from Employee

• Increases the size of the database

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 25: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Relevant Access Methods

Song(SongID,Title,ReleaseID,Duration,Format,Genre)Release(ReleaseID,Title,ArtistID,ReleaseDate)

select S.Genre, count(*), sum(S.Duration)from Song S, Release Rwhere S.ReleaseID = R.ReleaseID

and S.Format = ’MP3’and R.ReleaseDate > ’1/1/2005’

group by S.Genreorder by S.Genre

Which access methods might be useful for this query?

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)

Page 26: Access Methods and Indexing File OrganizationAccess Methods and Indexing File Organization file organization is about which tuples to place on each page, and how specific tuples

Access Methods and Indexing

Physical Design Advisors

% db2advis -d sample -s "select empno,lastnamefrom employee where workdept = ’xxxx’"Found maximum set of [1] recommended indexestotal disk space needed for initial set [ 0.005] MB[ 50.5219] timerons (without indexes)[ 25.1521] timerons (with current solution)[%50.22] improvement

-- ===========================-- index[1], 0.005MB

CREATE INDEX WIZ1517 ON "KMSALEM "."EMPLOYEE"("WORKDEPT" ASC, "LASTNAME" ASC, "EMPNO" ASC) ;

-- ===========================

Design advisors can assist DBAs with physical design.

Cheriton School of Computer Science CS448 Database Systems Implementation (Winter 2012)