Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Index on every Foreign Key?


Index on every Foreign Key?

Author
Message
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14956 Visits: 38953
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!

jpipes
jpipes
Right there with Babe
Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)

Group: General Forum Members
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



Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14956 Visits: 38953
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!

jpipes
jpipes
Right there with Babe
Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)

Group: General Forum Members
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.

Smile

Jay



Allen Cui-55137
Allen Cui-55137
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2611 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.



jpipes
jpipes
Right there with Babe
Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)

Group: General Forum Members
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 Smile , 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 Smile



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search