views:

61

answers:

7

If I have a field in a table of some date type and I know that I will always be searching it using comparisons like between, > or < and never = could there be a good reason not to add an index for it?

+1  A: 

Don't index it IF you want to scan the entire table every time. I would want the database to try and do a range scan, so I'd add the index, but I use SQL Server and it will use the index in most cases. However different databases many not use the index.

KM
Could you explain why I would want to scan the entire table every time? Do you mean if I have other parameters that already require a scan like `Active = 1`?
Spencer Ruport
grab a phone book and do a range scan on "Jones", now pretend that the phone book is in random order and do a complete table scan (cover to cover) looking for all "Jones" names. Which would be faster?
KM
Range scan of course. I'm asking about "Don't index it IF you want to scan the entire table every time." is there some reason I would want to scan the entire table every time?
Spencer Ruport
not me, I say `I'd add the index`. @gbn raises valid points about covering other columns, but the question does not have enough info to do a full index analysis.
KM
My bad. Misunderstood the tone there. +1 for your help.
Spencer Ruport
+1  A: 

Depending on the data, I'd go further than that, and suggest it could be a clustered index if you're going to be doing BETWEEN queries, to avoid the table scan.

Rowland Shaw
+3  A: 

There are far more bad reasons.

However, an index on the search column may not be enough if the index is nonclustered and non-covering. Queries like this are often good candidates for clustered indexes, however a covering index is just as good.

gbn
+1 for mentioning covering indices - something too many folks still overlook...
marc_s
+1 from me as well. I hadn't heard of this concept before.
Spencer Ruport
+1  A: 

While an index helps for querying the table, it will also slow down inserts, updates and deletes somewhat. If you have a lot more changes in the table than queries, an index can hurt the overall performance.

Guffa
+3  A: 

The only reason not to add an index on a field you are going to search on is that the cost of maintaining the index overweights its benefits.

This may happen if:

  • You have a really tough DML on your table
  • The existence of the index makes it intolerably slow, and
  • It's more important to have fast DML than the fast queries.

If it's not the case, then just create the index. The optimizer just won't use it if it thinks it's not needed.

Quassnoi
Dumb question. What's DML?
Spencer Ruport
Data Manipulation Language (insert, update, delete, select) as opposed to DDL (Data Definition Language) like create, drop, etc.
MJB
Ah thanks. Hadn't seen that before.
Spencer Ruport
+2  A: 

This is a great example of why this is as much art as science. Some considerations:

  • How often is data added to this table? If there is far more reading/searching than adding/changing (the whole point of some tables to dump data into for reporting), then you want to go crazy with indexes. You clustered index might be needed more for the ID field, but you can have plenty of multi-column indexes (where the date fields comes later, with columns listed earlier in the index do a good job of reducing the result set), and covered indexes (where all returned values are in the index, so it's very fast, like you're searching on the clustered index to begin with).

  • If the table is edited/added to often, or you have limited storage space and hence can't have tons of indexes, then you have to be more careful with your indexes. If your date criteria typically gives a wide range of data, and you don't search often on other fields, then you could give a clustered index over to this date field, but think several times before you do that. You clustered index being on a simple autonumber field is a bonus for all you indexes. Non-covered indexes use the clustered index to zip to the records for the result set. Don't move the clustered index to a date field unless the vast majority of your searching is on that date field. It's the nuclear option.

  • If you can't have a lot of covered indexes (data changes a lot on the table, there's limited space, your result sets are large and varied), and/or you really need the clustered index for another column, and the typical date criteria gives a wide range of records, and you have to search a lot, you've got problems. If you can dump data to a reporting table, do that. If you can't, then you'll have to balance all these competing factors carefully. Maybe for the top 2-3 searches you minimize the result-set columns as much as you can configure covered indexes, and you let the rest make due with a simple non -clustered index

You can see why good db people should be paid well. I know a lot of the factors, but I envy people to can balance all these things quickly and correctly without having to do a lot of profiling.

Patrick Karcher
A: 

If the table is small it might never use the indexes therefore adding them may just be wasting resources.

There are datatypes (like image in SQL Server) and data distributions where indexes are unlikely to be used or can't be used. For instance in SQL Server, it is pointless to index a bit field as there is not enough variability in the data for an index to do any good.

If you usually query with a like clause and a wildcard as the first character, no index will be used, so creating one is another waste of reseources.

HLGEM