Re: foreign key references only part of composite PK - table scan issue
- From: Dave <djohannsen2@xxxxxxxxx>
- Date: Sun, 9 Aug 2009 14:38:17 -0700 (PDT)
On Aug 9, 1:19 pm, Erland Sommarskog <esq...@xxxxxxxxxxxxx> wrote:
Dave (djohanns...@xxxxxxxxx) writes:
yeah you're right, you cannot have a foreign key that references only
part of a composite key. i should have left that part out. the join
DOES however only reference 1 of the 2 fields in the composite PK in
table ABC, causing the table scan.
So there are two join queries where this happen? Well, the first course
of action must be to determine what is the correct ways to write
these queries. Then you can start to add columns if there are any
missing.
You say joins, and if they are true joins it sounds fishy to join on
only part of a key, because that tend to bring in duplicates. But if
the SELECT includes a DISTINCT, but no column from table ABC, it could
be the case of a join that should have been written with EXISTS. And
in that case, at least the join from DEF to the first column in PK could
be OK.
As for the table scan, there is nothing that says that you would get
a table scan because you join only against the first column in the PK.
You could just as well get a table scan if you add a second column as
well, if the optimizer finds that a merge or a hash join is the best
way to do the join.
It's another matter with the join from MNO where you join only on the
second column in the key; here you can at best get an index scan if
the key is nonclustered. So here you would need to add an index on
this second column in ABC. All provided that the query is correctly
written, that is.
--
Erland Sommarskog, SQL Server MVP, esq...@xxxxxxxxxxxxx
Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
thanks for your efforts, you may have a miscommunication though.
there is only 1 join with 3 tables at play, where an index scan
(essentially a table scan) is at play. i won't explain the table
layouts again though - so no worries. thanks again.
dave
.
- Follow-Ups:
- Re: foreign key references only part of composite PK - table scan issue
- From: Erland Sommarskog
- Re: foreign key references only part of composite PK - table scan issue
- References:
- foreign key references only part of composite PK - table scan issue
- From: Dave
- Re: foreign key references only part of composite PK - table scan issue
- From: Erland Sommarskog
- Re: foreign key references only part of composite PK - table scan issue
- From: Dave
- Re: foreign key references only part of composite PK - table scan issue
- From: Erland Sommarskog
- foreign key references only part of composite PK - table scan issue
- Prev by Date: Re: foreign key references only part of composite PK - table scan issue
- Next by Date: Re: foreign key references only part of composite PK - table scan issue
- Previous by thread: Re: foreign key references only part of composite PK - table scan issue
- Next by thread: Re: foreign key references only part of composite PK - table scan issue
- Index(es):
Relevant Pages
|