Professional Documents
Culture Documents
Tuning
Tuning
Rewrite SQL (Leccotech) Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views, Denormalization Export/Import (drop indexes): defragment Check Locks Separate data by category in proper tablespaces Partition Database Redundant Arrays of Inexpensive Disks (RAID) Redefining Client-Server Architecture Buy Hardware
When to Index
Large tables A field that you query by frequently Field with high cardinality (not sex where card. Is only 2) Smaller Fields and Fixed length are preferred. (Obs:Most DBMSs automatically index PK)
Indexes (Defaults)
Anytime a PK is created, an index is automatically created. Anytime when the type of index is not specificied, the type of index created is a B-Trees.
B-Trees (continued)
One node corresponds to one block/page (minimum disk I-O). Non-Leaf nodes(n keys, n+1 pointers) Leaf-Nodes (contain n entries, where each entry has an index and a pointer to a data block). Also, each node has a pointer to next node. All leaves are at the same height.
Bitmap Index
Bitmap indexes contain the key value and a bitmap listing the value of 0 or 1 (yes/no) for each row indicating whether the row contains that value or not. May be a good option for indexing fields that have low cardinality (opposite of Btrees).
Hash Indexing
B-trees and Bitmap index keys are used to find rows requiring I/O to process index Hash gets rows with a key based algorithm Rows are stored based on a hashed value Index size should be known at index creation Example:
create index cidx on orders (cid) hashed;
Index-Organized Tables
Table data is incorporated into the B-Tree using the PK as the index. Table data is always in order of PK. Many sorts can be avoided. Especially useful for lookup type tables Index works best when there are few (and small) columns in your table other than the PK.
Conclusions on Indexes
For high-cardinality key values, B-Tree indexes are usually best. B-Trees work with all types of comparisons and gracefully shrink and grow as table changes. For low cardinality read-only environments, Bitmaps may be a good option.
Query Optimizer
A query optimizer parsers your SQL/Query into a sequence of relational algebra operations, determining an execution plan. The query optimizer figures out the best execution plan based on rules of thumb and information provided in the Data Dictionary (System catalog).
Query Optimizer
To view how the query plan you must use either set autotrace on or explain plan. Set autotrace on is much simpler. Explain plan is a little bit more efficient, but more complicated.
TABLE ACCESS FULL (full table scan): Oracle will look at every row in the table to find the requested information. This is usually the slowest way to access a table.
TABLE ACCESS BY ROWID Oracle will use the ROWID method to find a row in the table. ROWID is a special column detailing an exact Oracle block where the row can be found. This is the fastest way to access a table (faster than any index. Less flexible than any index).
INDEX RANGE SCAN Oracle will search an index for a range of values. Usually, this even occurs when a range or between operation is specified by the query or when only the leading columns in a composite index are specified by the where clause. Can perform well or poorly, based on the size of the range and the fragmentation of the index.).
INDEX UNIQUE SCAN Oracle will perform this operation when the tables primary key or a unique key is part of the where clause. This is the most efficient way to search an index.
NESTED LOOPS Indicates that a join operation is occurring. Can perform well or poorly, depending on performance on the index and table operations of the individual tables being joined.
ROWID
SELECT ROWID, INTO :EMP_ROWID, FROM EMP WHERE EMP.EMP_NO = 56722 FOR UPDATE; UPDATE EMP SET EMP.NAME = WHERE ROWID = :EMP_ROWID;
ROWID (cont.)
Fastest Less Flexible Are very useful for removing duplicates of rows
SELECT STATEMENT
Not exists in place of NOT IN Joins in place of Exists Avoid sub-selects Exists in place of distinct UNION in place of OR on an index column WHERE instead of ORDER BY