 |
|
Oracle Create Index tips
Oracle Tips by Burleson Consulting |
Creating Oracle Indexes
Once you have decided you need to create an index you
use the create index command. The command is pretty straightforward as seen in
this example:
CREATE INDEX
ix_emp_01
ON
emp (deptno)
TABLESPACE
index_tbs;
This statement creates an index called IX_EMP_01. This
index is built on the DEPTNO column of the EMP table. We also defined the
tablespace that the index should be created in using the tablespace keyword, in
this case we created it in the INDEX_TBS tablespace.
You can create indexes on multiple columns in a table.
Say, for example, we wanted an index on the EMP table columns EMPNO and DEPTNO.
This is known as a concatenated index, and it?s created this way:
CREATE INDEX ix_emp_01
ON emp (empno, deptno) TABLESPACE index_tbs;
Altering Oracle Indexes
If we create indexes, there may be times that we will
want to change some attribute of that index, such as where it is stored. Also,
sometimes an index needs to be rebuilt to help with performance. For cases like
these, the alter index command is what we want.
Let?s look at an example of the use of the alter index
command:
ALTER INDEX ix_emp_01
REBUILD TABLESPACE new_index;
In this example we use the alter index command to
rebuild an index. The rebuild keyword is what tells Oracle to rebuild the index.
When we use the tablespace keyword, followed by a tablespace name, we are
telling Oracle which tablespace to recreate the rebuilt index in. By default
Oracle will create the rebuilt index in the same tablespace.
The alter index command allows you to rename an index using the rename to keyword as seen in this example:
ALTER INDEX ix_emp_01
RENAME TO ix_emp_01_old;
In this case we have renamed the ix_emp_01 index to
ix_emp_01_old. All of the data dictionary entries will be changed by Oracle to
reflect the new name (we will discuss indexes and the data dictionary in a
moment)
Dropping Oracle Indexes
Sometimes what we create we must destroy. When it?s time
to remove an index, the drop index command is what is needed. The drop index
command is pretty straight forward as seen in this example:
DROP INDEX
ix_emp_01_old;
Generate create index syntax
You can punch create index DDL syntax is using the
dbms_metadata package,
something like this:
select
dbms_metadata.get_ddl('INDEX',index_name)
from
user_indexes
where
xxxx;
For more information on creating indexes see these articles:
This is an excerpt from the bestselling "Easy
Oracle Jumpstart" by Robert Freeman and Steve Karam (Oracle ACE and Oracle
Certified Master). It?s only $19.95 when you buy it directly from the
publisher
here.
 |
If you like Oracle tuning, you may enjoy the new book "Oracle
Tuning: The Definitive Reference", over 900 pages of BC's favorite tuning
tips & scripts.
You can buy it direct from the publisher for 30%-off and get instant access to
the code depot of Oracle tuning scripts. |
|