Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good practice to use reverse indexes on surrogate keys? (Oracle)

Say I have a table with an auto-incrementing surrogate key.

Would this be a good case for using a reverse index?

Am I correct in stating:

Insertions (into index) would be faster .. since new values would be inserted randomly, instead of always going to the right most leaf (constantly forcing rebalances).

Index look ups would be marginally slower .. since the db would have to spend (a little) of time reversing the index.

Since it's a surrogate key .. I wouldn't really need range scan ability (which you can't do with reverse indexes).

(Note, I'm not using Oracle RAC)

like image 554
vicsz Avatar asked Mar 31 '12 23:03

vicsz


People also ask

What is a reverse key index in Oracle 8?

Reverse key indexes were introduced in Oracle 8 to help reduce this contention. A reverse key index is created by including the REVERSE keyword in the index creation. The contents of the table column is unchanged, only how the key is represented in the index block.

What is the reverse key index of 005789?

With a reverse key index, Oracle will logically index : 1 005789, 2 015789, 3 205789, 4 and so on.

Do reverse key indexes void the ability of index range scan?

"reverse key indexes void the ability of an index range scan. They only work with exact equality.' Does it mean if my query needs to do range scan on cols with reverse key index, does oracle ignore using the reverse key indexes on those columns and go for full table scan instead?

Does RBO make use of reverse key index?

but in general, NO range scanning with reverse keys. Does RBO make use of Reverse Key Index ? I have a table in which the primary key is populated by sequence and I see lots of Buffer Busy waits on the primary key Index and hence I suggested to drop and recreate it as a revarse key index.


1 Answers

In general, if you're not using RAC, there would be no reason to use a reverse-key index.

From a performance standpoint, you're much better off having one or two hot blocks at any given point in time that are subject to inserts because that essentially guarantees that the hot blocks will be in the buffer cache and the INSERT won't have to incur the cost of reading the block from disk. If you've got inserts going into random blocks in an index, there is a much greater probability that the block you want would have aged out of the cache and would incur the cost of a physical I/O.

The cost of keeping an index balanced is pretty minimal but even that favors a standard index. If you've got a sequence generated primary key with a normal index, Oracle will do a 90/10 block split on the right-most block when that block fills up. In contrast, if you've got a reverse key index, Oracle has to do 50/50 block splits whenever a given block fills up. A 50/50 block split copies half the data from the old block to the new block, a 90/10 block split only copies the right-most data value to the new block. The 90/10 block split, therefore, is much cheaper than a 50/50 block split and you'd need to do roughly the same number of block splits regardless of the type of index you pick. So the cost of maintaining a regular index is less than the cost of maintaining a reverse key index even ignoring the effect of cache.

The reason you'd consider using a reverse key index would be that you're using RAC and you want to avoid the cost of having many RAC nodes all fighting over the same hot block. If you constantly have to ship the hot block from one node to another in order to do the next insert, it may be worthwhile to use a reverse key index instead to reduce that contention. If you have licensed the partitioning option, it would be better still to use a hash partitioned index instead (this can be done whether or not the tables is partitioned). If you haven't licensed the partitioning option, a reverse key index may be good enough at resolving contention on the hot block to not require that you license partitioning.

like image 157
Justin Cave Avatar answered Sep 28 '22 06:09

Justin Cave