开发者

Should searchable date fields in a database table always be indexed?

开发者 https://www.devze.com 2022-12-24 09:24 出处:网络
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

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?


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.


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.


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消