indexes: structure, splits and free space management internals

Post on 28-Nov-2014

922 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

2014 © Trivadis

BASEL BERN BRUGG LAUSANNE ZUERICH DUESSELDORF FRANKFURT A.M. FREIBURG I.BR. HAMBURG MUNICH STUTTGART VIENNA

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals

OakTable World, 29 September 2014

Christian Antognini

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

1

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals29 September 2014

2

@ChrisAntognini

Senior principal consultant, trainer and partnerat Trivadis in Zurich (CH) christian.antognini@trivadis.com http://antognini.ch

Focus: get the most out of Oracle Database Logical and physical database design Query optimizer Application performance management

Author of Troubleshooting Oracle Performance Apress, 2008/2014

OakTable Network, Oracle ACE Director

2014 © Trivadis

AGENDA

1. Concepts

2. Index Keys

3. Splits

4. Free Space

5. Reorganizations

6. Bitmap

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

3

2014 © Trivadis

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

Concepts

4

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals5

Structure of a B-Tree

29 September 2014

24 51

12 29 42 86

2 ROWID

8 ROWID

10 ROWID

11 ROWID

12 ROWID

18 ROWID

20 ROWID

24 ROWID

26 ROWID

29 ROWID

34 ROWID

38 ROWID

42 ROWID 51 ROWID

55 ROWID

73 ROWID

86 ROWID

99 ROWID

left<24right≥24

leaf

blo

cks

bra

nch

blo

cks

root block

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals6

Nulls

NULL values are stored in the index only when at least one of the indexed columns is not NULL Exception: bitmap indexes always store indexed data

For unique indexes the uniqueness is guaranteed only if at least one of the indexed columns is not NULL

NULL values are stored after not NULL values in the index

29 September 2014

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals7

Myths

Oracle B-tree indexes can become “unbalanced” over time and need to be rebuilt

Deleted space in an index is “deadwood” and over time requires the index to be rebuilt

If an index reaches “x” number of levels, it becomes inefficient and requires the index to be rebuilt

If an index has a poor clustering factor, the index needs to be rebuilt

To improve performance, indexes need to be regularly rebuilt

29 September 2014

Source: Oracle B-Tree Index Internals: Rebuilding The Truth, Richard Foote

2014 © Trivadis

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

Index Keys

8

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals9

Internal Key

Internally a unique key is needed to sort the indexed columns

For unique indexes the internal key is the same as the index key

For non-unique indexes the ROWID of the indexed row is added to the index key to make it unique For global indexes an extended ROWID is used, otherwise a restricted

ROWID is enough

Therefore The index entries are sorted in a consistent way Even for non-unique indexed the order of the index entries changes only

when the indexed table is reorganized (i.e. not if the index is rebuilt!)

29 September 2014

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals10

Branch Block Key

In branch blocks the internal key is truncated after the first byte that differs from the last key of the left leaf block

All keys are preceded by a length field 1 byte for data up to 127 bytes Otherwise 2 bytes

- The high bit of the first byte is set (0x80)- The remaining bits of the first byte and the second byte contain the length

Partially stored keys are terminated by 0xFE Nulls have a column length of 0xFF, trailing nulls are stored

29 September 2014

RDBALen

Key 1DataKey 1

LenKey X

DataKey X

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals11

Leaf Block Key

All keys are preceded by a length field 1 byte for data up to 127 bytes Otherwise 2 bytes

- The high bit of the first byte is set (0x80)- The remaining bits of the first byte and the second byte contain the length

Nulls have a column length of 0xFF, trailing nulls are stored

For global indexes an extended ROWID is used, otherwise a restricted ROWID is enough

29 September 2014

Flag LockLen

Key 1DataKey 1

LenROWID

ROWID(optional)

2014 © Trivadis

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

Splits

12

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals13

Splits

Two split methods are implemented

50:50 split The keys are evenly distributed over two blocks Based on size, not number of keys

99:1 split (a.k.a. 90:10) The new key is the right-most of the index A new leaf block containing only the new key is added Useful for increasing values like sequences and timestamps

A change in the structure of an index (like a split) is not rollbacked if the transaction that caused it is rollbacked

29 September 2014

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals14

Splits

29 September 2014

#1

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals15

Splits

29 September 2014

#1

#2 #3

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals16

Splits

29 September 2014

#3#2 #4

#1

#3#2 #4

#1

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals17

Splits

29 September 2014

#1

#3#2 #4 #5

#1

#3#2 #4 #5

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals18

Splits

29 September 2014

2014 © Trivadis

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

Free Space

19

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals20

Reminder

PCTFREE is only used when the index is created (or rebuilt) An update is implemented as a delete followed by an insert The free space in leaf blocks is strongly dependent on the type of split (50:50

or 99:1)

PCTUSED cannot be used for indexes

29 September 2014

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals21

Free Space

29 September 2014

#1

#4#2 #3

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals22

Free Space

29 September 2014

#1

#3#2 #4

2014 © Trivadis

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

Reorganizations

23

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals24

Reorganizations

If the free space is not automatically reused, it should be manually reclaimed This is unusual, therefore such an operation should only be performed if it is

really necessary

To manually reclaim free space two possibilities exists Rebuild: the index is recreated Coalesce: free space in contiguous blocks is merged and, therefore, some

blocks are unchained

Unnecessary rebuild/coalesce may lead to sub-optimal data density in leaf blocks Too much free space, i.e. too low density Too much block splits, i.e. too high density

29 September 2014

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals25

Rebuild vs. Coalesce

29 September 2014

Pro Cons

Rebuild

Doesn’t lock table (online rebuilds only)

Reclaim all the free space

Locks table during rebuild (offline rebuilds only)

Doubles space usage temporarily

The index is completely recreated (to do so large sorts may be performed)

Coalesce

Doesn’t lock table

Current index structure is reused

Doesn’t reclaim all the free space

Can generate lot of redo

2014 © Trivadis

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

Bitmap

26

2014 © Trivadis

Indexes: Structure, Splits and Free Space Management Internals27

Bitmap

A bitmap index is a regular B-tree, only the internal key differs from the non-bitmap index!

The key is composed by Indexed column(s) Start/End ROWID Bitmap representing the rows that have the key

- To save space it is “compressed”

29 September 2014

2014 © Trivadis

Text Text

Summary

B-tree indexes cannot become “unbalanced”

Deleted space in an index can be reused

Indexes do not need to be regularly rebuilt

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

28

2014 © Trivadis

Questions and answers ...

2014 © Trivadis

BASEL BERN BRUGG LAUSANNE ZUERICH DUESSELDORF FRANKFURT A.M. FREIBURG I.BR. HAMBURG MUNICH STUTTGART VIENNA

Christian Antognini

Senior Principal Consultant

christian.antognini@trivadis.com

29 September 2014Indexes: Structure, Splits and Free Space Management Internals

29

top related