Documente Academic
Documente Profesional
Documente Cultură
Understanding Indexes
Tim Gorman Principal - SageLogix, Inc.
Email: tim@sagelogix.com
www.SageLogix.Com
Agenda
B*Tree index architecture Issues with B*Tree indexes in Oracle
Real issues
Sparsely-populated indexes Contention on INSERT Uneven data distribution Low data cardinality (a.k.a. selectivity)
branch
leaf
leaf
leaf
leaf
leaf Table: X
leaf
leaf
Or:
sequentially ascending data values
7
Split:
10
Block contention when inserting sequential data Uneven distribution of data values Popular and unpopular data values Low data cardinality
Sparsely-populated indexes
Indexes never become unbalanced over time
If so, then why does performance sometimes deteriorate over time?
14
Sparsely-populated indexes
How is this condition detected?
ANALYZE INDEX VALIDATE STRUCTURE command Populate session-private view named INDEX_STATS with only one row Column PCT_USED is the average percentage of space utilized in the blocks belonging to the index Derived from the ratio of the value of the columns USED_SPACE and BTREE_SPACE Expect PCT_USED to be 90 by default Lesser values may indicate sparseness developing Best to watch values over time
15
Sparsely-populated indexes
How is this condition detected?
If PCT_USED is much less than 90 Non-zero values in the column DEL_LF_ROWS Cause of sparseness is probably row deletions, if the value in the column DEL_LF_ROWS is a large percentage of value in column LF_ROWS
16
Sparsely-populated indexes
To rebuild an index:
Use ALTER INDEX REBUILD Use ALTER INDEX COALESCE
17
Sparsely-populated indexes
ALTER INDEX REBUILD is the most commonlyused solution for sparsely-populated indexes
Unlike CREATE INDEX, uses the existing index as the source, which is faster because: Index is usually smaller than the table (less I/O) Index is already sorted (some sorting still needed) Other features of ALTER INDEX REBUILD include: Parallel execution (throw more resources at task) Direct-path operations (no rollback/undo generated)
18
Sparsely-populated indexes
ALTER INDEX COALESCE is another
alternative to rebuilding
Merges unused space within indexes to free blocks for reuse
19
Contention on INSERT
Multiple concurrently-executing INSERTs into
an index on column(s) with sequential data values can also bottle-neck on buffer busy waits (a.k.a. block-level contention)
Pseudo-randomizing such data with REVERSE key indexes relieves this performance problem Instead of many processes attempting to insert index entries into the right-most leaf block
Insertions are evenly scattered over all of the available leaf blocks
20
Contention on INSERT
[ CREATE | ALTER ] INDEX REVERSE
Pseudo-randomizes non-random data By flipping or reversing the physical order of the data value during storage Causes sequential data values to become more random by simply reversing data 123456 becomes 654321 123457 becomes 754321, etc... Index data values are transparently converted (flipped) and unconverted (unflipped) upon insert and retrieval, respectively
21
Contention on INSERT
Adverse impacts of using REVERSE indexes:
only equivalence operations will use the index =, !=, <>, IN, and NOT IN range-scans will not use the index >, >=, <, <=, LIKE, BETWEEN
No problem here
But timestamps are often range-scanned There is likely a problem with these situations
22
27
Table truncated.
SQL> begin 2 for i in 1..100000 loop 3 insert into t1 4 values(to_char(round(i/187,0)), i, 5 round(i/187,0)); 6 end loop; 7 end; 8 / PL/SQL procedure successfully completed.
29
30
32
ROWID list Distinct values list Bitmap ROWID list Distinct values list Bitmap
More distinct data values means less room for rows within each data value
34
For each change, entries within the bitmap segments must be:
Expanded/decoded Manipulated Re-encoded
Because of this, bitmap indexes are most feasible on partitioned tables in non-transactional applications
Data changes should be performed in bulk using some variation on the technique of EXCHANGE PARTITION
35
Function-based indexes
Indexes can be based on functions and
expressions
create index xxx_ix1 on xxx(upper(c1)); create index xxx_ix2 on xxx(upper(c1)||yadda_yadda(c2));
36
Function-based indexes
User-defined functions usable along with standard
built-in functions
37
Function-based indexes
Function-based indexes must have statistics
gathered before they can be used
System permissions
[ GLOBAL ] QUERY REWRITE needed to CREATE or ALTER REBUILD
38
Descending indexes
Implemented as a form of function-based
indexes
CREATE INDEX DESC data values in index leaf blocks are sorted in descending order must have statistics before it will be utilized not usable with rule-based optimizer system permission QUERY REWRITE or GLOBAL QUERY REWRITE needed to CREATE or ALTER REBUILD parameter QUERY_REWRITE_ENABLED not necessary to use
39
Index compression
Since Oracle8i, compression has been available on
B*Tree indexes:
Reduces repeated storage of prefix column data values Index compression [ COMPRESS [ #-prefix-cols ] | NOCOMPRESS ] UNIQUE: default #-prefix-cols is (#-cols)-1 NONUNIQUE: default #-prefix-cols is #-cols Same syntax for IOT in USING clause as for index compression DML is supported on compressed indexes, but it becomes much slower Index compression is best used for read-mostly or readonly situations Mixing of compressed and uncompressed index partitions is possible
40
41
Slides, paper, and scripts downloadable from http://www.SageLogix.com and http://www.EvDBT.com/papers.htm Email: tim@sagelogix.com
42