Alex Rivera | Logout

How do you know what a good index is?

Asked 2008-09-17T02:22:41.527
13

When working with tables in Oracle, how do you know when you are setting up a good index versus a bad index?

Edit
Report

4 Answers

9

Fields that are diverse, highly specific, or unique make good indexes. Such as dates and timestamps, unique incrementing numbers (commonly used as primary keys), person's names, license plate numbers, etc...

A counterexample would be gender - there are only two common values, so the index doesn't really help reduce the number of rows that must be scanned.

Full-length descriptive free-form strings make poor indexes, as whoever is performing the query rarely knows the exact value of the string.

Linearly-ordered data (such as timestamps or dates) are commonly used as a clustered index, which forces the rows to be stored in index order, and allows in-order access, greatly speeding range queries (e.g. 'give me all the sales orders between October and December'). In such a case the DB engine can simply seek to the first record specified by the range and start reading sequentially until it hits the last one.

answered 2008-09-17T02:39:32.827
2

Here's a great SQL Server article: http://www.sql-server-performance.com/tips/optimizing_indexes_general_p1.aspx

Although the mechanics won't work on Oracle, the tips are very apropos (minus the thing on clustered indexes, which don't quite work the same way in Oracle).

answered 2008-09-17T02:25:02.847
0

Some rules of thumb if you are trying to improve a particular query.

For a particular table (where you think Oracle should start) try indexing each of the columns used in the WHERE clause. Put columns with equality first, followed by columns with a range or like.

For example:

WHERE CompanyCode = ? AND Amount BETWEEN 100 AND 200

If columns are very large in size (e.g. you are storing some XML or something) you may be better off leaving them out of the index. This will make the index smaller to scan, assuming you have to go to the table row to satisfy the select list anyway.

Alternatively, if all the values in the SELECT and WHERE clauses are in the index Oracle will not need to access the table row. So sometimes it is a good idea to put the selected values last in the index and avoid a table access all together.

You could write a book about the best ways to index - look for author Jonathan Lewis.

answered 2008-10-03T12:53:19.513
-2

A good index is something that you can rely on to be unique for a specific table row.

One commonly used index scheme is the use of numbers which increment by 1 for each row in the table. Every row will end up having a different number index.

answered 2008-09-17T02:32:38.133

Your Answer