Semantec Ltd. Oracle Performance Tuning Boyan Pavlov Indexes Indexes
Semantec Ltd. Agenda Overview Types Storage Selecting an index strategy
Semantec Ltd. Overview Create Indexes when selecting a small percentage of rows from a table (less than 2-4%) Indexes are logically and physically independent of the data in the associate table. Required CREATE ANY INDEX system privilege for creating index.
Semantec Ltd. Indexing schemes B-tree indexes B-tree cluster indexes Reverse key indexes Bitmap indexes Hash cluster indexes
Semantec Ltd. Types Unique and non unique indexes Composite indexes Function-based indexes Application domain indexes
Semantec Ltd. Unique and nonunique indexes Oracle recommends that you do not explicitly define unique indexes on tables. Oracle automatically creates index to enforce UNIQUE and PRIMARY KEY integrity constraints.
Semantec Ltd. Composite index Example Vend_idPart_NoUnit_cost CREATE INDEX Vend_idx1 ON Vendor (Vend_id,Part_No); CREATE INDEX Vend_idx2 ON Vendor (Part_No,Vend_id);
Semantec Ltd. Composite index Guidelines for creating Create on keys that are frequently used together in WHERE clause conditions combined with AND operators. If several queries select the same set of keys based on one or more key values, then create a composite index containing all of these keys. Consider the guidelines associated with the general performance advantages and trade- offs of all type of indexes.
Semantec Ltd. Composite index Guidelines for ordering Create the index so the keys used in WHERE clauses make up a leading portion. Create the index so that the more frequently selected keys make up a leading portion. If all keys are used in WHERE clauses equally often, then ordering these keys from most selective to least selective. If all keys are used in the WHERE clauses equally often but the data is physically ordered on one of the keys, then place that key first in the composite index.
Semantec Ltd. Internal structure of indexes B-tree indexes B-tree cluster indexes Reverse key indexes Bitmap indexes Index-organized tables
Semantec Ltd. B-tree indexes Storage
Semantec Ltd. B-tree indexes Advantages All leaf blocks of the tree are at the same depth. B-tree indexes automatically stay balanced. All blocks of the B-tree are three-quarters full on the average. B-trees provide excellent retrieval performance for a wide range of queries, including exact match and range searches. Inserts, updates, and deletes are efficient, maintaining key order for fast retrieval. B-tree performance is good for both small and large tables, and does not degrade as the size of a table grows.
Semantec Ltd. Reverse key indexes Creating a reverse key index, compared to a standard index, reverses the bytes of each column indexed (except the rowid) while keeping the column order. Example: CREATE INDEX i ON t (a,b,c) REVERSE; ALTER INDEX i REBUILD NOREVERSE;
Semantec Ltd. Bitmap indexes Example CUSTO MER# MARITAL_ STATUS REGIONGENDERINCOME_ LEVEL 101SingleEastMale1 102MarriedCentralFemale2 103MarriedWestFemale3 104DivorcedCentralFemale1 105MarriedWestMale2 106SingleCentralMale2 Table Customer_data
Semantec Ltd. Bitmap indexes Example CREATE BITMAP INDEX ON Customer_data(MARITAL_STATUS, REGION, GENDER); SELECT COUNT(*) FROM Customer_data WHERE MARITAL_STATUS = ’Married’ AND REGION IN (’Central’,’West’);
Semantec Ltd. Bitmap indexes Example REGION = ‘East’ REGION = ‘Central’ REGION = ‘West’
Semantec Ltd. REGION = ‘Central’ REGION = ‘West’ Bitmap indexes Example Status = ‘married’ AND OR = = AND
Semantec Ltd. Bitmap indexes Benefits Reduced response time for large classes of queries Asubstantial reduction of space usage compared to other indexing techniques Dramatic performance gains even on very low end hardware Very efficient parallel DML and loads
Semantec Ltd. Function-based indexes Advantages Increase the number of situations where the optimizer can perform a range scan instead of a full table scan. Precompute the value of a computationally intensive function and store it in the index. Create indexes on object columns and REF columns. Create more powerful sorts.
Semantec Ltd. Function-based indexes Restrictions Used only by cost-based optimization. A PL/SQL function, used in the index expression must be declared as DETERMINISTIC. Function-based indexes cannot be built on LOB columns, nested tables, or varrays. Expressions used in a function-based index should reference only columns in a row in the table. Hence, these expressions cannot contain any aggregate functions. You must have the initialization parameters COMPATIBLE set to or higher, QUERY_REWRITE_ENABLED=TRUE, and QUERY_REWRITE_INTEGRITY=TRUSTED.
Semantec Ltd. Function-based indexes Restrictions You must analyze the table or index before the index is used. Bitmap optimizations cannot used descending indexes. Function-based indexes are not used when OR- expansion is done. The index function cannot be marked NOT NULL. To avoid a full table scan, you must ensure that the query cannot fetch null values. Function-based indexes that return VARCHAR2 or RAW data types from a PL/SQL function are not permitted due to length restrictions.
Semantec Ltd. Function-based indexes Example CREATE INDEX Idx ON Emp_tab(UPPER(Ename)); SELECT * FROM Emp_tab WHERE UPPER(Ename) like :KEYCOL;
Semantec Ltd. Function-based indexes Example CREATE INDEX Idx ON Fbi_tab (A+B*(C-1),A,B); SELECT a FROM Fbi_tab WHERE A+B*(C-1) < 100;
Semantec Ltd. Index-organized tables Primary key uniquely identifies a row; primary key must be specified Primary key based access Logical rowid in ROWID pseudocolumn allows building secondary indexes UNIQUE constraint not allowed but triggers are allowed Cannot be stored in a cluster Can contain LOB columns but not LONG columns Distribution and replication not supported
Semantec Ltd. Application domain indexes Indextype schema object Domain index (an application-specific index) Application specific operators
Semantec Ltd. Selecting an index strategy Creating indexes Alter indexes Dropping indexes
Semantec Ltd. Determine whether an index is good Create index, Analyze index, Use EXPLAIN PLAN on your query to see if the optimizer uses it. Is it expensive to maintain? Compare optimizer costs with and without index.
Semantec Ltd. Re-creating Indexes Use the ALTER INDEX... REBUILD statement to reorganize or compact an existing index or to change its storage characteristics. The REBUILD statement uses the existing index as the basis for the new one. ALTER INDEX... REBUILD is usually faster than dropping and re-creating an index. It reads all the index blocks using multiblock I/O then discards the branch blocks. A further advantage of this approach is that the old index is still available for queries while the rebuild is in progress.
Semantec Ltd. Writing Statements that Avoid Using Indexes You can use the NO_INDEX hint to give the CBO maximum flexibility while disallowing the use of a certain index. You can use the FULL hint to force the optimizer to choose a full table scan instead of an index scan. You can use the INDEX, INDEX_COMBINE, or AND_EQUAL hints to force the optimizer to use one index or a set of listed indexes instead of another.
2001, Semantec Ltd. Creating tables with constraints and indexes 1. Create the tables with the constraints. NOT NULL constraints may be unnamed and should be created enabled and validated. All other constraints should be named and should be "created disabled". 2. Load old data into the tables. 3. Create all indexes. 4. Enable novalidate all constraints. Do this to primary keys before foreign keys. 5. Allow users to query and modify data. 6. With a separate ALTER TABLE statement for each constraint, validate all constraints.