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 ««123»»

An Un-indexed Foreign Key Gotcha Expand / Collapse
Author
Message
Posted Monday, November 9, 2009 3:52 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:15 PM
Points: 4,469, Visits: 6,398
sqlpro (11/9/2009)
thanks for the reply. probably i misunderstood but following is the link i got my understanding from.its here http://www.informit.com/library/content.aspx?b=STY_Sql_Server_7&seqNum=145



In general, nonclustered indexes (and the associated bookmark lookup to the base table) will be used only if the query is estimated to hit between 0 and perhaps as much as 2 percent of the total rows. This is a surprisingly low percentage to most people. Often, and certainly the case in my example where I explicitely stated there was exactly one FK child row per parent in a billion row table, FK children tables have VERY few rows per parent key and an index WILL be beneficial for lookups/joins.


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #816209
Posted Monday, November 9, 2009 3:56 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, July 29, 2013 8:34 PM
Points: 110, Visits: 120
I've been realizing that creating a non-clustered index on the foreign key is quite a very good idea. Nice article, very informative and interesting.
Post #816215
Posted Monday, November 9, 2009 3:57 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 9, 2014 9:55 PM
Points: 360, Visits: 103
that makes sense. thanks for correcting that TheSQLGuru


Post #816216
Posted Monday, November 9, 2009 4:05 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, November 12, 2014 7:25 PM
Points: 60, Visits: 80
I couldn't resist -- I wanted to query to find them. Didn't read all replies first. So here is my simple query to find a table without an index on a foreign key.

If FK exists and column is in NO index -- then it will list out. Could be refined more. (exact order for multiple column FK match exact order for indexes )

-----------------------------------------------------------------------
-- Find a list of Foreign Keys that are not indexed
--
-- Created 11-09-2009 Denise Crabtree
-----------------------------------------------------------------------
-----------------------------------------------------------------------
SELECT
object_name(parent_object_id) [FK table]
,object_name(referenced_object_id) [References this Table]
,object_name(constraint_object_id) [Foreign Key Name]
,sys.columns.name [ChildTable Column]

FROM sys.foreign_key_columns
INNER JOIN sys.columns
ON sys.foreign_key_columns.parent_object_id = sys.columns.object_id
AND sys.foreign_key_columns.Parent_column_id = sys.columns.column_id

LEFT OUTER JOIN sys.index_columns
ON sys.foreign_key_columns.parent_object_id = sys.index_columns.object_id
AND sys.foreign_key_columns.Parent_column_id = sys.index_columns.column_id

WHERE
sys.index_columns.object_id is null
Post #816226
Posted Monday, November 9, 2009 4:18 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:15 PM
Points: 4,469, Visits: 6,398
See the earlier reply by richardd. It has a link to a script by Paul Nielson that not only identifies FKs missing indexes but will actually generate CREATE INDEX scripts for them (IIRC).

Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #816235
Posted Monday, November 9, 2009 4:52 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 4:59 PM
Points: 18,060, Visits: 16,091
Good Article with useful info. Thanks



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #816242
Posted Tuesday, November 10, 2009 7:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 22, 2014 7:48 AM
Points: 8, Visits: 16
The indexed FKs have nothing to do with performance; increased performance MIGHT be a side-effect, but - as you point out - it might as well not be such an effect. The idea of indexing FK columns has to do with the locking mechanisms.

In order to be sure that you can delete a parent record you must also make sure that there are no children belonging to that parent. This can only be achieved by actually reading the child table(s). When you don't have any usable index on the child table, then you must read all records - and they get locked at the same time, just because they are part of an updating (read-write) transaction.

This is very common truth för many relational databases, including SQL Server, Oracle, Firebird and others, and it has so been since "ancient" times.


P.S. I thought it was common knowledge...
Post #816523
Posted Tuesday, November 10, 2009 8:15 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:15 PM
Points: 4,469, Visits: 6,398
The indexed FKs have nothing to do with performance;


I think that is a bit of a stretch. There is almost always at least some performance benefit (often dramatic), both in efficient join/seek and in improved concurrency from reduced locking.


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #816574
Posted Wednesday, November 11, 2009 7:58 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 20, 2014 1:11 PM
Points: 27, Visits: 475
Please read Thomas Kyte's books. He wrote for Oracle but the principles about "transaction processing" and "locking" and "index" are the same.

When FK records can be fastly located by index, the transaction processing will be fast, that reduces required lock-time, hence improves performance. This applies to any records.


Jason
http://dbace.us
Post #817172
Posted Wednesday, November 11, 2009 8:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 22, 2014 7:48 AM
Points: 8, Visits: 16
I agree it's a bit of a stretch.

My point was that indexing might have different purposes, optimizing search operations being just ONE of them.

Post #817186
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse