1. Trang chủ
  2. » Công Nghệ Thông Tin

creating indexes

26 189 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Creating indexes
Thể loại Chapter
Định dạng
Số trang 26
Dung lượng 155,6 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć2... Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć4... Creating Indexes 15Ć5Overview An Oracle7 Ser

Trang 1

Creating Indexes

15

Trang 2

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć2

Trang 3

At the end of this lesson, you should be able to

D Distinguish between the indexes that are created automatically and those that arecreated manually

D Identify the uses for indexes

D Explain the index structure and why it improves query speed

D Create a non-unique index

D Remove an index from the data dictionary

D Evaluate guidelines for creating and using indexes

Trang 4

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć4

Trang 5

Creating Indexes 15Ć5

Overview

An Oracle7 Server index is a database object that can speed up the retrieval of rows

by using a pointer Indexes can be created explicitly or automatically They are

transparent to the user If you do not have an index on the column, then a full tablescan will occur

What Is an Index?

An index is a database object that provides direct and fast access to rows in a table.Its purpose is to reduce the necessity of disk I/O by using a B*Tree indexed path tolocate data quickly The index is automatically used and maintained by the Oracle7Server Once an index is created, no direct activity is required by the user

Indexes are logically and physically independent of the table they index This meansthat they can be created or dropped at any time and have no effect on the base tables

or other indexes

How Are Indexes Created?

Two types of indexes can be created One type is a unique index The Oracle7 Serverautomatically creates this index when you define a column in a table to have a

PRIMARY KEY or a UNIQUE constraint The name of the index is the name given

to the constraint

The other type of index a user can create is a non-unique index For example, you cancreate a FOREIGN KEY column index for a join in a query to improve retrievalspeed

For more information, see

Oracle7 Server Concepts Manual, Release 7.3, “Schema Objects” section, “Indexes”

topic

Trang 6

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć6

Trang 7

Creating Indexes 15Ć7

When Is the Index Used?

Once the index has been created, the Oracle7 Server will use it whenever possible tospeed up access to the data Note that this use is automatic and usually requires noaction by the user A brief guideline is provided below on how the Server determines

to use the index

The cost-based optimization method uses statistics about tables along with

information about available indexes to select an execution plan for the SQL

statements

For more information, see

Tune Oracle7 Applications course description.

Trang 8

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć8

Trang 9

Creating Indexes 15Ć9

Index Structure

An index is an optional structure that is independent of the table structure Each index

is composed of column values that the index is on and pointers (or ROWIDs) to therow containing that value The pointer directly points to the appropriate row in thetable, therefore avoiding a full table scan

B*Tree

The Oracle7 Server uses a balanced B*tree index structure This is a binary,

self-balancing search structure to equalize access times to any row It is an efficientmethod of ensuring that access to any specified value will take approximately thesame time whether the row is at the beginning, middle, or end of the table

Each index that the Oracle7 Server builds consists of a number of pages (or branches)

of storage arranged in a tree Each page (or branch) holds a series of key values andpointers to pages (or branches) lower in the structure until eventually the key valuesindicate the location of the data itself The location identifier at the database level iscalled a ROWID

Trang 10

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć10

Trang 11

Single column Only one column exists in the index.

Concatenated or composite Can contain up to 16 columns in the index for

either performance or uniqueness checkpurposes The columns need not be adjacent.Index types are not mutually exclusive For example, you can create a unique,

concatenated index

Trang 12

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć12

Trang 13

Creating Indexes 15Ć13

Creating an Index

Create an index on one or more columns by issuing the CREATE INDEX command

Abridged Syntax

where: index is the name of the index

table is the name of the table

column is the name of the column in the table to be

indexed

Example

Create an index to improve the speed of query access on the LAST_NAME column

in the S_EMP table

SQL> CREATE INDEX s_emp_last_name_idx

2 ON s_emp(last_name);

Index created

For more information, see

Oracle7 Server SQL Reference, Release 7.3, “CREATE INDEX.”

Trang 14

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć14

Trang 15

Creating Indexes 15Ć15

More Is Not Always Better

More indexes on a table does not mean it will speed up queries Each DML operationthat is committed on a table with indexes means that the indexes must be updated.The more indexes you have associated with a table, the more effort the Server mustmake to update all the indexes after a DML

When to Create an Index

D The column is used frequently in the WHERE clause or in a join condition

D The column contains a wide range of values

D The column contains a large number of null values

D Two or more columns are frequently used together in a WHERE clause or joincondition

D The table is large and most queries are expected to retrieve less than 2–4% of therows

Remember that if you want to enforce uniqueness, you should define a unique

constraint in the table definition Then, a unique index is automatically created

When to Not Create an Index

D The table is small

D The columns are not often used as a condition in the query

D Most queries are expected to retrieve more than 2–4% of the rows

D The table is updated frequently

Trang 16

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć16

Trang 17

SQL> SELECT ic.index_name, ic.column_name,

2 ic.column_position col_pos, ix.uniqueness

3 FROM user_indexes ix, user_ind_columns ic

4 WHERE ic.index_name = ix.index_name

5 AND ic.table_name = ’S_EMP’;

Trang 18

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć18

Trang 19

Creating Indexes 15Ć19

Removing an Index

You cannot modify indexes To change an index, you must drop it and then re-create

it Remove an index definition from the data dictionary by issuing the DROP INDEXcommand In order to drop an index, you must be the owner of the index or have theDROP ANY INDEX privilege

Syntax

where: index is the name of the index

Trang 20

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć20

Trang 21

Creating Indexes 15Ć21

Summary

Indexes are used to improve the query retrieval speed They are database objects andtherefore take up disk space Indexes use the B*tree search method to retrieve thepointer to the rows in the tables

Unique indexes for the PRIMARY KEY and UNIQUE KEY constraints in a tabledefinition are created automatically

Users can create non-unique indexes to speed up searches by using the CREATEINDEX command

Indexes are maintained automatically by the Oracle7 Server Users can view thedefinitions of the indexes in the USER_INDEXES data dictionary view

An index can be dropped by the creator or a user with the DROP ANY INDEXprivilege by using the DROP INDEX command

Trang 22

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć22

Trang 23

Creating Indexes 15Ć23

Practice Overview

In this practice you will create an index on the WORKER table

Practice Contents

D Creating non-unique indexes

D Displaying data dictionary information about the index

D Dropping indexes

Trang 24

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć24

Trang 25

Creating Indexes 15Ć25

Practice 15

1. Would any indexes specified be used with the following queries and why?

a. Non-unique index on LAST_NAME

Yes/No

SQL> SELECT *

2 FROM s_emp

3 WHERE last_name = ’Biri’;

b. Unique index on ID and non-unique index on CUSTOMER_ID

Yes/No

SQL> SELECT id, customer_id, total

2 FROM s_ord

3 WHERE date_ordered = ’31-AUG-92’;

c. Unique index on S_DEPT.ID and non-unique index on S_EMP.DEPT_ID.Yes/No

SQL> SELECT e.last_name, d.name

2 FROM s_emp e, s_dept d

3 WHERE e.dept_id = d.id;

Trang 26

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 15Ć26

2. Create a non-unique index on the foreign key column in the WORKER table

3. Since users will frequently query on the employee last name, create a non-uniqueindex on that column in the WORKER table

4. Display the indexes and uniqueness that exist in the data dictionary for the

WORKER and DEPARTMENT tables Save the command into a script named

p15q4.sql.

5. Remove the primary key constraint on the WORKER table

6. Re-display the indexes and uniqueness that exist in the data dictionary for the

WORKER and DEPARTMENT tables by executing the p15q4.sql script What

changes do you observe and why?

If you have time, complete the following exercises:

7. Re-create the primary key constraint on the WORKER table Confirm the

constraint in the data dictionary by executing pl2q2.sql Confirm the unique index

in the data dictionary by executing pl5q4.

8. Remove the index on the employee last name from the WORKER table

Ngày đăng: 26/10/2013, 23:15

Xem thêm

w