Index on every Foreign Key?

  • I've read here and in various articles that placing an index on a foreign key will boost performance, especially when joining tables together on the PK to FK reference.

    If that is true, would it be good practice to just place an index on every foreign key, regardless of it's usage or lack of, or should you add indexes only to boost performance for specific issues that a sql containing joins might bring up?

    lowell@stormrage.com

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • While that advice is a decent starting point; i.e. first look to foreign key fields used frequently in joins to place indexes on.

    However, beware putting indexes on any field (foreign key or otherwise) that is not particularly selective (a low rate of repetitive data). Having an index on any field that has very few distinct values will not help any query and will likely hurt update and insert performance on the same table, as inserts and updates to another index are now necessary.

    HTH a bit,

    Jay

  • Essentially, that is exactly what I was wondering;

    For fun, I wrote a script that would create an index for every foreign key that did not have an index yet; but I was afraid to add a lot of indexes (600+ new indexes) in bulk unless it seemed to be a common practice; I think searching all my sp's for JOIN statements, and indexing any FK keys found there would be a better optimization. In our case, every sql statement is an SP, so if there is no join, i can assume the FK is not relevant to performance issues.

    Thanks!

    lowell@stormrage.com

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • quote:


    ...I think searching all my sp's for JOIN statements, and indexing any FK keys found there would be a better optimization. In our case, every sql statement is an SP, so if there is no join, i can assume the FK is not relevant to performance issues...


    I think that would be a bad assumption. Even if there are no joins, an index will be helpful if the field is in a WHERE clause (SARG) and the data in the field is selective. I do think that there is no one-pass index optimization query that can be run; indexes should be carefully selected and placed on those fields which are used most often in WHERE clauses (JOIN clauses are a special type of WHERE condition...), and where the data in those fields contains fairly to highly distinct values. I wouldn't advise running any end-all-be-all script to identify fields on which to place indexes. That would be a recipe for disaster in my opinion. Spending the time to carefully look at the data in each prospective field is well worth the effort.

    🙂

    Jay

  • Profiler your database for quite period time and feed the result into Index Tuning Wizard to identify the columns that need to be indexed. And then inentify the long run queries and see changes in indexes will help the performance.

  • quote:


    ...Profiler your database for quite period time and feed the result into Index Tuning Wizard to identify the columns that need to be indexed...


    Allen,

    I agree with the opinion 🙂 , however, lowell stated all his scripts are in stored procedures. AFAIK, Index Tuning Wizard will not help in the slightest with a profiler trace that has only stored procedure executions. ITW needs true scripts in order to optimize, and lowell would have to do a lot of work taking the scripts out of his procedures and running against a test environment in order to get a profiler trace that ITW would use effectively.

    Just thought I'd save lowell a bit of a headache 🙂

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply