Creating Indexes

advertisement
15
Creating Indexes
15Ć2
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
Objectives
If you want to improve the performance of some queries, you should consider
creating an index. You can also use indexes to enforce uniqueness on a column or
a collection of columns.
At the end of this lesson, you should be able to
D
Distinguish between the indexes that are created automatically and those that are
created 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.
Creating Indexes
15Ć3
15Ć4
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
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 table
scan 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 to
locate data quickly. The index is automatically used and maintained by the Oracle7
Server. 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 means
that 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 Server
automatically 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 can
create a FOREIGN KEY column index for a join in a query to improve retrieval
speed.
For more information, see
Oracle7 Server Concepts Manual, Release 7.3, “Schema Objects” section, “Indexes”
topic.
Creating Indexes
15Ć5
15Ć6
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
When Is the Index Used?
Once the index has been created, the Oracle7 Server will use it whenever possible to
speed up access to the data. Note that this use is automatic and usually requires no
action by the user. A brief guideline is provided below on how the Server determines
to use the index.
Optimization Techniques
When an index is used depends partly on the Oracle Optimizer being used at the time.
The Oracle7 Server uses both rule-based and cost-based optimization.
Rule-based optimization is when the Oracle7 Server decides when it is appropriate to
use an index based on its internal rules. The Server identifies the columns that are
indexed and the index types.
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.
Creating Indexes
15Ć7
15Ć8
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
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 the
row containing that value. The pointer directly points to the appropriate row in the
table, 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 efficient
method of ensuring that access to any specified value will take approximately the
same 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 and
pointers to pages (or branches) lower in the structure until eventually the key values
indicate the location of the data itself. The location identifier at the database level is
called a ROWID.
Creating Indexes
15Ć9
15Ć10
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
Index Structure
continued
Index Types
Type
Description
Unique
Ensures that values in specified columns are
unique.
Non-unique
Ensures fastest possible results when querying
data.
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 check
purposes. The columns need not be adjacent.
Index types are not mutually exclusive. For example, you can create a unique,
concatenated index.
Creating Indexes
15Ć11
15Ć12
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
Creating an Index
Create an index on one or more columns by issuing the CREATE INDEX command.
Abridged Syntax
CREATE INDEX index
ON
table (column[, column]...);
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
2 ON
s_emp_last_name_idx
s_emp(last_name);
Index created.
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “CREATE INDEX.”
Creating Indexes
15Ć13
15Ć14
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
Creating an Index
continued
More Is Not Always Better
More indexes on a table does not mean it will speed up queries. Each DML operation
that 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 must
make 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 join
condition.
D
The table is large and most queries are expected to retrieve less than 2–4% of the
rows.
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.
Creating Indexes
15Ć15
15Ć16
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
Confirming Indexes
Confirm the existence of indexes from the USER_INDEXES data dictionary view.
You can also check the columns involved in an index by querying the
USER_IND_COLUMNS view.
Example
Display all previously created indexes, affected column names, and uniqueness on the
S_EMP table.
SQL>
2
3
4
5
SELECT
FROM
WHERE
AND
ic.index_name, ic.column_name,
ic.column_position col_pos, ix.uniqueness
user_indexes ix, user_ind_columns ic
ic.index_name = ix.index_name
ic.table_name = ’S_EMP’;
INDEX_NAME
––––––––––––––––––––
S_EMP_ID_PK
S_EMP_LAST_NAME_IDX
S_EMP_USERID_UK
Creating Indexes
COLUMN_NAME
–––––––––––
ID
LAST_NAME
USERID
COL_POS
––––––––
1
1
1
UNIQUENESS
––––––––––
UNIQUE
NONUNIQUE
UNIQUE
15Ć17
15Ć18
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
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 INDEX
command. In order to drop an index, you must be the owner of the index or have the
DROP ANY INDEX privilege.
Syntax
DROP INDEX
where: index
Creating Indexes
index;
is the name of the index.
15Ć19
15Ć20
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
Summary
Indexes are used to improve the query retrieval speed. They are database objects and
therefore take up disk space. Indexes use the B*tree search method to retrieve the
pointer to the rows in the tables.
Unique indexes for the PRIMARY KEY and UNIQUE KEY constraints in a table
definition are created automatically.
Users can create non-unique indexes to speed up searches by using the CREATE
INDEX command.
Indexes are maintained automatically by the Oracle7 Server. Users can view the
definitions 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 INDEX
privilege by using the DROP INDEX command.
Creating Indexes
15Ć21
15Ć22
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
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
Creating Indexes
15Ć23
15Ć24
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
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
3 WHERE
b.
Unique index on ID and non-unique index on CUSTOMER_ID.
Yes/No
SQL> SELECT
2 FROM
3 WHERE
c.
*
s_emp
last_name = ’Biri’;
id, customer_id, total
s_ord
date_ordered = ’31-AUG-92’;
Unique index on S_DEPT.ID and non-unique index on S_EMP.DEPT_ID.
Yes/No
SQL> SELECT
2 FROM
3 WHERE
Creating Indexes
e.last_name, d.name
s_emp e, s_dept d
e.dept_id = d.id;
15Ć25
Practice 15
continued
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-unique
index 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.
15Ć26
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder
Download