Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to create a new SQL Server Index?

Tags:

sql

database

tsql

Obviously (methinks), creating an index on a BIT column is unnecessary. However, if you had a column that you need to search in which every value is likely unique, like BlogPost or StreetAddress or something, then an index seems appropriate (again, methinks).

But what's the cutoff? What if you expect 10,000 rows and you'll have about 20 unique values among them. Should an index be created?

Thanks in advance.

like image 792
witttness Avatar asked Oct 30 '08 14:10

witttness


2 Answers

The best answer to this is to profile your queries and see if the index improves your queries. The difficulty in answering this is that it is nearly impossible to generalize the behavior of the query optimizer.

That said, a rule-of-thumb is if your selectivity is 10% or less on a given query on a table, then you will most likely benefit from an index. So in your example, you might benefit from an index if your values are evenly distributed. However, considering that your table is small, so your performance boost might be negligible.

This is not a hard and fast rule as there are a lot of factors that can change the 10% number, including the use of a clustered or other index types, size of the rows, if some columns not inline, query structure, etc.

Also keep in mind there is a significant performance penalty for inserting into a table with an index. If this table is frequently updated or appended, the speed boost from the index may be negated by the slower inserts and updates.

See the MSDN article on Tablescan vs Index access.

Edit: As others have pointed out, your query may benefit from an index if you are performing aggregation queries, such as counting the number of times a particular value appears. You may also benefit if you frequently sort on a particular column.

like image 194
James Schek Avatar answered Oct 05 '22 07:10

James Schek


James hit the nail on the head. I'll just add that even a bit column might benefit from an index depending on how you are using the table. For example, if you need to count the number of rows that have a 1 many times throughout the day, an index there could be useful. Indexes aren't always about finding a single record - they can also be used for aggregations.

like image 27
Tom H Avatar answered Oct 05 '22 07:10

Tom H