Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Index on every Foreign Key? Expand / Collapse
Author
Message
Posted Tuesday, May 6, 2003 7:01 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:03 PM
Points: 12,895, Visits: 32,091
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #11946
Posted Tuesday, May 6, 2003 8:40 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, October 21, 2003 12:00 AM
Points: 728, Visits: 1
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




Post #61435
Posted Tuesday, May 6, 2003 8:52 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:03 PM
Points: 12,895, Visits: 32,091
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #61436
Posted Tuesday, May 6, 2003 9:00 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, October 21, 2003 12:00 AM
Points: 728, Visits: 1
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




Post #61437
Posted Tuesday, May 6, 2003 12:23 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 2, 2005 11:49 AM
Points: 2,611, Visits: 1
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.




Post #61438
Posted Tuesday, May 6, 2003 12:29 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, October 21, 2003 12:00 AM
Points: 728, Visits: 1
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 :)





Post #61439
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse