clase 12 manejo indices modificada

20
Managing Indexes

Upload: titiushko-jazz

Post on 22-Jan-2018

71 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Clase 12 manejo indices   modificada

Managing Indexes

Page 2: Clase 12 manejo indices   modificada

Objectives

After completing this lesson, you should be able todo the following:• List the different types of indexes and their uses• Create B-tree and bitmap indexes• Reorganize indexes• Drop indexes• Get index information from the data dictionary

Page 3: Clase 12 manejo indices   modificada

Classification of Indexes

• Logical– Single column or concatenated– Unique or nonunique– Function-based

• Physical– Partitioned or nonpartitioned– B-tree– Normal or reverse key– Bitmap

Page 4: Clase 12 manejo indices   modificada

B-Tree Index

Index entry header

Key column length

Key column value

ROWID

Root

Branch

Leaf

Index entry

Page 5: Clase 12 manejo indices   modificada

KEY ROWID

ID (BLOCK# ROW# FILE#)

----- -------------------

1257 0000000F.0002.0001

2877 0000000F.0006.0001

4567 0000000F.0004.0001

6657 0000000F.0003.0001

8967 0000000F.0005.0001

9637 0000000F.0001.0001

9947 0000000F.0000.0001

... ...

... ...

Reverse Key Index

Index on EMPLOYEE (ID) EMPLOYEE table

ID FIRST_NAME JOB

----- ---------- --------

7499 ALLEN SALESMAN

7369 SMITH CLERK

7521 WARD SALESMAN

7566 JONES MANAGER

7654 MARTIN SALESMAN

7698 BLAKE MANAGER

7782 CLARK MANAGER

… ... ... ...

... ... ... ...

Page 6: Clase 12 manejo indices   modificada

Creating Function-Based Indexes

• Dramatically improves query performance

• Queries using expressions can use the index

CREATE INDEX summit.item_quantity_to_deliver_idx

ON summit.item(quantity - quantity_shipped);

SELECT ord_id, item_id

FROM ITEM

WHERE (quantity - quantity_shipped) > 0;

Page 7: Clase 12 manejo indices   modificada

Bitmap Index

<Blue, 10.0.3, 12.8.3, 1000100100010010100>

<Green, 10.0.3, 12.8.3, 0001010000100100000>

<Red, 10.0.3, 12.8.3, 0100000011000001001>

<Yellow, 10.0.3, 12.8.3, 0010001000001000010>

key

start

ROWID

end

ROWID bitmap

Table

Index

Block 10

Block 11

Block 12

File 3

Page 8: Clase 12 manejo indices   modificada

Comparing B-Tree and Bitmap Indexes

B-tree

Suitable for high-cardinality

columns

Updates on keys relatively

inexpensive

Inefficient for queries

using OR predicates

Useful for OLTP

Bitmap

Suitable for low-cardinality

columns

Updates to key columns very

expensive

Efficient for queries

using OR predicates

Useful for data warehousing

Page 9: Clase 12 manejo indices   modificada

Creating Normal B-Tree Indexes

CREATE INDEX summit.employee_last_name_idx

ON summit.employee(last_name)

PCTFREE 30

STORAGE(INITIAL 200K NEXT 200K

PCTINCREASE 0 MAXEXTENTS 50)

TABLESPACE indx;

Page 10: Clase 12 manejo indices   modificada

Creating Indexes: Guidelines

• Balance query and DML needs• Place in separate tablespace• Use uniform extent sizes: Multiples of five blocks

or MINIMUM EXTENT size for tablespace• Consider NOLOGGING for large indexes• Set high PCTFREE if new key values are likely to

be within the current range

Page 11: Clase 12 manejo indices   modificada

Creating Reverse Key Indexes

CREATE UNIQUE INDEX summit.orders_id_idx

ON summit.orders(id) REVERSE

PCTFREE 30

STORAGE(INITIAL 200K NEXT 200K

PCTINCREASE 0 MAXEXTENTS 50)

TABLESPACE indx;

Page 12: Clase 12 manejo indices   modificada

Creating Bitmap Indexes

Use the parameter CREATE_BITMAP_AREA_SIZE to specify the amount of memory allocated for bitmap creation.

CREATE BITMAP INDEX orders_region_id_idx

ON summit.orders(region_id)

PCTFREE 30

STORAGE(INITIAL 200K NEXT 200K

PCTINCREASE 0 MAXEXTENTS 50)

TABLESPACE indx;

Page 13: Clase 12 manejo indices   modificada

Changing Storage Parameters for Indexes

ALTER INDEX summit.employee_last_name_idx

STORAGE(NEXT 400K

MAXEXTENTS 100);

Page 14: Clase 12 manejo indices   modificada

Allocating and Deallocating Index Space

ALTER INDEX summit.orders_region_id_idx

ALLOCATE EXTENT (SIZE 200K

DATAFILE ‘/DISK6/indx01.dbf’);

ALTER INDEX summit.orders_id_idx

DEALLOCATE UNUSED;

Page 15: Clase 12 manejo indices   modificada

Rebuilding Indexes

Use the ALTER INDEX command to:• Move an index to a different tablespace• Improve space utilization by removing deleted

entries• Change a reverse key index to a normal B-tree

index and vice versa

ALTER INDEX summit.orders_region_id_idx REBUILD

TABLESPACE indx02;

Page 16: Clase 12 manejo indices   modificada

Online Rebuild of Indexes

Rebuilding indexes can be done with minimal table locking.

ALTER INDEX summit.orders_id_idx REBUILD ONLINE;

Page 17: Clase 12 manejo indices   modificada

Coalescing Indexes

Before coalescing After coalescing

ALTER INDEX summit.orders_id_idx COALESCE;

Page 18: Clase 12 manejo indices   modificada

Dropping Indexes

• Drop and re-create an index before bulk loads.

• Drop indexes that are infrequently needed and buildthem when necessary.

• Drop and re-create invalid indexes.

DROP INDEX summit.deptartment_name_idx;

Page 19: Clase 12 manejo indices   modificada

Obtaining Index Information

DBA_INDEXESOWNER

INDEX_NAME

INDEX_TYPE

TABLE_OWNER

TABLE_NAME

UNIQUENESS

TABLESPACE_NAME

LOGGING

STATUS

DBA_IND_COLUMNSINDEX_OWNER

INDEX_NAME

TABLE_OWNER

TABLE_NAME

COLUMN_NAME

COLUMN_POSITION

COLUMN_LENGTH

Page 20: Clase 12 manejo indices   modificada

Summary

In this lesson, you should have learned how to:• Create different types of indexes• Reorganize indexes• Drop indexes• Get index information from the data dictionary