Given the following indexes for an Oracle database:
CREATE INDEX subject_x1 ON subject (code);
CREATE INDEX subject_x2 ON subject (code, status);
Is it true that the first index is redundant, and can be removed. We need to get this right as this is a relatively large sized table that is going to be constantly hammered.
Any oracle documentation explaining or confirming this would be very helpful.
As with most database questions, it depends 🙂
In fact, it shouldn’t be removed in all circumstances. If you have a query that uses
codealone in thewhereclause, it’s possible that the use ofsubject_x1will still outperformsubject_x2just by virtue of the fact that less index data will need to be read in.An extreme example, let’s say that
codeis achar(2)andstatusis a char(4094)`. If you have the query:That’s most likely to result in an index-only scan since there’s absolutely no reason to go to the table. If you use
subject_x1and you read in 4K blocks from your index, you can bring in a couple of thousand codes with every read.By using
subject_x2, each read only gives you one code (and possibly a wasted status). That’s a huge difference in performance.However, and I cannot stress this enough, measure, don’t guess!
Profile your queries (and updates) using both indexes to see which performs better, and do it with representative data. If you find that
subject_x1gives you better performance for some queries, leave it in. It will affect the update/insert speed and storage requirements but you’ll find that won’t generally matter since: