When should I use index on table?
EDIT: Now I am working with firebird 2.5, but most of the time I am using SQL Server 2005/2008.
We should look at the column(s) that are used very frequently in the query's WHERE clause as filter conditions. If the WHERE clause uses a single column for filtering the rows then a single-column index is good otherwise if multiple columns are used for filtering then a composite index is suitable.
Index the Correct Tables and Columns Create an index if you frequently want to retrieve less than about 15% of the rows in a large table. This threshold percentage varies greatly, however, according to the relative speed of a table scan and how clustered the row data is about the index key.
An index is good for picking a fraction of the rows from a table. Querying by a primary key value is the best utilization of an index. The worst scenario is accessing all rows from a table via an index, because it has to read index pages and referenced data pages.
Indexing makes columns faster to query by creating pointers to where data is stored within a database. Imagine you want to find a piece of information that is within a large database. To get this information out of the database the computer will look through every row until it finds it.
In general, my indexing strategy would be something like this (I'm using SQL Server exclusively for now - adapt to your own database system as needed):
pick a good clustering key - not a GUID, not a VARCHAR(250)
or something - a good clustering key is narrow, unique, stable, ever-increasing - something like a INT IDENTITY
is perfect. Makes this your clustered primary key -> gives you your first index on the table
for any column that is being used as a foreign key into another table - add an index. It can either be a single column index - or it might be a compound index - whatever works best for your case. It's important that the foreign key column be the first column in that index (if you're using a compound index) - otherwise, the benefits for the JOIN
's or for checking referential integrity won't be available to your system
And that's it for now.
Then: run your system - observe, and measure - establish a baseline. Is the app fast enough? If yes -> you're done - go home and enjoy your spare time.
If not: then start collecting data and indications as to why the app isn't fast enough. Look at e.g. things like the DMV's in SQL Server that tell you about the worst performing queries, or the missing index DMV. Analyze those. See what you could improve. Add one index at a time and again: observe, measure, compare to your baseline.
If you have improvement -> leave that index in place and this measurement is your new baseline. Rinse and repeat until you (and your users) are happy with the app's performance (and then go home and enjoy your time off).
Over-indexing in SQL Server can be worse than not having any indexes. Don't start out with too many indices to begin with! Only establish good clustered PK and foreign key nonclustered indices - that's all - then observe, measure, optimize & repeat that cycle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With