Indexes are used in Oracle to provide quick access to rows in a table. Indexes provide faster access to data for operations that return a small portion of a table's rows.
Although Oracle allows an unlimited number of indexes on a table, the indexes only help if they are used to speed up queries. Otherwise, they just take up space and add overhead when the indexed columns are updated. You should use the EXPLAIN PLAN feature to determine how the indexes are being used in your queries. Sometimes, if an index is not being used by default, you can use a query hint so that the index is used.
In this post I will sharing few guidelines to be considered while creating indexes and managing indexes.
Create Indexes After Inserting Table Data
Typically, you insert or load data into a table (using SQL*Loader or Import) before creating indexes. Otherwise, the overhead of updating the index slows down the insert or load operation. The exception to this rule is that you must create an index for a cluster before you insert any data into the cluster.
Creating Large Index: Switch Your Temporary Tablespace to Avoid Space Problems Creating Indexes
When you create an index on a table that already has data, Oracle must use sort space to create the index. Oracle uses the sort space in memory allocated for the creator of the index (the amount for each user is determined by the initialization parameter
SORT_AREA_SIZE
), but must also swap sort information to and from temporary segments allocated on behalf of the index creation. If the index is extremely large, it might be beneficial to complete the following steps:- Create a new temporary tablespace using the
CREATE TABLESPACE
command. - Use the
TEMPORARY TABLESPACE
option of theALTER USER
command to make this your new temporary tablespace. - Create the index using the
CREATE INDEX
command. - Drop this tablespace using the
DROP TABLESPACE
command. Then use theALTER USER
command to reset your temporary tablespace to your original temporary tablespace.
Under certain conditions, you can load data into a table with the SQL*Loader "direct path load", and an index can be created as data is loaded.
Index the Correct Tables and Columns
Use the following guidelines for determining when to create an index:
- Create an index if you frequently want to retrieve less than 15% of the rows in a large table. The percentage varies greatly according to the relative speed of a table scan and how clustered the row data is about the index key. The faster the table scan, the lower the percentage; the more clustered the row data, the higher the percentage.
- Index columns used for joins to improve performance on joins of multiple tables.
- Primary and unique keys automatically have indexes, but you might want to create an index on a foreign key.
- Small tables do not require indexes; if a query is taking too long, then the table might have grown from small to large.
Some columns are strong candidates for indexing. Columns with one or more of the following characteristics are candidates for indexing:
- Values are relatively unique in the column.
- There is a wide range of values (good for regular indexes).
- There is a small range of values (good for bitmap indexes).
- The column contains many nulls, but queries often select all rows having a value. In this case, a comparison that matches all the non-null values, such as:WHERE COL_X > -9.99 *power(10,125)is preferable toWHERE COL_X IS NOT NULThis is because the first uses an index on
COL_X
(assuming thatCOL_X
is a numeric column).
Columns with the following characteristics are less suitable for indexing:
LONG
and LONG
RAW
columns cannot be indexed.
The size of a single index entry cannot exceed roughly one-half (minus some overhead) of the available space in the data block. Consult with the database administrator for assistance in determining the space required by an index.
Order of Index Columns in Composite Index for Performance
Although you can specify columns in any order in the
CREATE
INDEX
command, the order of columns in the CREATE
INDEX
statement can affect query performance. In general, you should put the column expected to be used most often first in the index. You can create a composite index (using several columns), and the same index can be used for queries that reference all of these columns, or just some of them.
For example, assume the columns of the
VENDOR_PARTS
table are as shown in Figure below.
Assume that there are five vendors, and each vendor has about 1000 parts.
Suppose that the
VENDOR_PARTS
table is commonly queried by SQL statements such as the following:SELECT * FROM vendor_parts WHERE part_no = 457 AND vendor_id = 1012;
To increase the performance of such queries, you might create a composite index putting the most selective column first; that is, the column with the most values:
CREATE INDEX ind_vendor_id ON vendor_parts (part_no, vendor_id);
Composite indexes speed up queries that use the leading portion of the index. So in the above example, queries with
WHERE
clauses using only the PART_NO
column also note a performance gain. Because there are only five distinct values, placing a separate index on VENDOR_ID
would serve no purpose.Limit the Number of Indexes for Each Table
The more indexes, the more overhead is incurred as the table is altered. When rows are inserted or deleted, all indexes on the table must be updated. When a column is updated, all indexes on the column must be updated.
You must weigh the performance benefit of indexes for queries against the performance overhead of updates. For example, if a table is primarily read-only, you might use more indexes; but, if a table is heavily updated, you might use fewer indexes.
Gather Statistics to Make Index Usage More Accurate
COMPUTE STATISTICS
in the CREATE INDEX
statement. As data is updated and the distribution of values changes, you or the DBA can periodically refresh the statistics by calling procedures likeDBMS_STATS.GATHER_TABLE_STATISTICS
and DBMS_STATS.GATHER_SCHEMA_STATISTICS
.Drop Indexes That Are No Longer Required
You might drop an index if:
- It does not speed up queries. The table might be very small, or there might be many rows in the table but very few index entries.
- The queries in your applications do not use the index.
- The index must be dropped before being rebuilt.
When you drop an index, all extents of the index's segment are returned to the containing tablespace and become available for other objects in the tablespace.
Use the SQL command
DROP
INDEX
to drop an index. For example, the following statement drops a specific named index:
DROP INDEX Emp_ename;
If you drop a table, then all associated indexes are dropped. To drop an index, the index must be contained in your schema or you must have theDROP
ANY
INDEX
system privilege.
Specify the Tablespace for Each Index
Indexes can be created in any tablespace. An index can be created in the same or different tablespace as the table it indexes. If you use the same tablespace for a table and its index, it can be more convenient to perform database maintenance (such as tablespace or file backup) or to ensure application availability. All the related data is always online together. Using different tablespaces (on different disks) for a table and its index produces better performance than storing the table and index in the same tablespace. Disk contention is reduced. But, if you use different tablespaces for a table and its index and one tablespace is offline (containing either data or index), then the statements referencing that table are not guaranteed to workConsider Parallelizing Index Creation
You can parallelize index creation, much the same as you can parallelize table creation. Because multiple processes work together to create the index, the database can create the index more quickly than if a single server process created the index sequentially. When creating an index in parallel, storage parameters are used separately by each query server process. Therefore, an index created with an INITIAL value of 5M and a parallel degree of 12 consumes at least 60M of storage during index creation.Consider Creating Indexes with NOLOGGING
You can create an index and generate minimal redo log records by specifying NOLOGGING in the CREATE INDEX statement. Note: Because indexes created using NOLOGGING are not archived, perform a backup after you create the index. Creating an index with NOLOGGING has the following benefits:
-> Space is saved in the redo log files. -> The time it takes to create the index is decreased. -> Performance improves for parallel creation of large indexes. In general, the relative performance improvement is greater for larger indexes created without LOGGING than for smaller ones. Creating small indexes withoutLOGGING has little effect on the time it takes to create an index. However, for larger indexes the performance improvement can be significant, especially when you are also parallelizing the index creation.Viewing Index Information
The following views display information about indexes:
View | Description |
---|---|
DBA_INDEXES ALL_INDEXES USER_INDEXES | DBA view describes indexes on all tables in the database. ALL view describes indexes on all tables accessible to the user. USER view is restricted to indexes owned by the user. Some columns in these views contain statistics that are generated by the DBMS_STATS package or ANALYZE statement. |
DBA_IND_COLUMNS ALL_IND_COLUMNS USER_IND_COLUMNS | These views describe the columns of indexes on tables. Some columns in these views contain statistics that are generated by theDBMS_STATS package or ANALYZE statement. |
DBA_IND_EXPRESSIONS ALL_IND_EXPRESSIONS USER_IND_EXPRESSIONS | These views describe the expressions of function-based indexes on tables. |
DBA_IND_STATISTICS ALL_IND_STATISTICS USER_IND_STATISTICS | These views contain optimizer statistics for indexes. |
INDEX_STATS | Stores information from the last ANALYZE INDEX...VALIDATE STRUCTURE statement. |
INDEX_HISTOGRAM | Stores information from the last ANALYZE INDEX...VALIDATE STRUCTURE statement. |
V$OBJECT_USAGE | Contains index usage information produced by the ALTER INDEX...MONITORING USAGE functionality. |
1 comments:
QUANTUM BINARY SIGNALS
Get professional trading signals sent to your cell phone every day.
Start following our trades right now and make up to 270% a day.
Post a Comment