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

Holy Foreign Keys and Indexes Expand / Collapse
Author
Message
Posted Monday, October 10, 2011 11:36 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 3:09 PM
Points: 869, Visits: 2,396
"I dropped all the foreign key constraints to this table on the handful of tables referencing it. I dropped the index, and then I added the foreign keys references back."

I would like to point out that if you didn't use WITH CHECK CHECK to fully rescan those tables and revalidate the constraint, those FK's are untrusted by SQL Server, and do not provide their full benefits.
Post #1188048
Posted Monday, October 10, 2011 11:54 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 1:20 PM
Points: 133, Visits: 472
Nadrek (10/10/2011)
"I dropped all the foreign key constraints to this table on the handful of tables referencing it. I dropped the index, and then I added the foreign keys references back."

I would like to point out that if you didn't use WITH CHECK CHECK to fully rescan those tables and revalidate the constraint, those FK's are untrusted by SQL Server, and do not provide their full benefits.


I will keep this in mind for the future. Do you have any documentation on this. Luckily for me, the data only lives in the tables for a short while so its all gone by now. Its put in an archive db\table by a rather large archive routine. Hey i didn't design it, i just support it. :)


Jimmy

"I'm still learning the things i thought i knew!"
Post #1188056
Posted Tuesday, October 11, 2011 2:31 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 11:49 PM
Points: 833, Visits: 1,365
This does indeed happen on both SQL Server 2008 R2 (RTM) and SQL Server 2008 SP2:

use tempdb;
go

create table t1 (
id int identity primary key,
val varchar(10)
);

create table t2 (
id int identity primary key,
val varchar(10)
);
go

create unique nonclustered index ix_t1 on t1 (id);
go

alter table t2
add constraint fk_t2_t1 foreign key (id) references t1 (id);
go

select
f.name as ConstraintName,
i.name as IndexName,
object_name(i.object_id) as TableName,
i.is_unique,
i.is_primary_key,
i.type_desc,
f.key_index_id
from
sys.foreign_keys f
inner join
sys.indexes i on
i.object_id = f.referenced_object_id and
i.index_id = f.key_index_id
go

drop table t2;
drop table t1;

Actually, Kimberly Tripp has even a Post on it. As far as I can see, this is a very nice feature optimizing the performance of foreign key constraint validation, and not even close to a bug.




Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

Concatenating Row Values in Transact-SQL
Post #1188270
Posted Tuesday, October 11, 2011 6:08 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 1:20 PM
Points: 133, Visits: 472
okbangas (10/11/2011)
This does indeed happen on both SQL Server 2008 R2 (RTM) and SQL Server 2008 SP2:

use tempdb;
go

create table t1 (
id int identity primary key,
val varchar(10)
);

create table t2 (
id int identity primary key,
val varchar(10)
);
go

create unique nonclustered index ix_t1 on t1 (id);
go

alter table t2
add constraint fk_t2_t1 foreign key (id) references t1 (id);
go

select
f.name as ConstraintName,
i.name as IndexName,
object_name(i.object_id) as TableName,
i.is_unique,
i.is_primary_key,
i.type_desc,
f.key_index_id
from
sys.foreign_keys f
inner join
sys.indexes i on
i.object_id = f.referenced_object_id and
i.index_id = f.key_index_id
go

drop table t2;
drop table t1;

Actually, Kimberly Tripp has even a Post on it. As far as I can see, this is a very nice feature optimizing the performance of foreign key constraint validation, and not even close to a bug.



Can you run my script that is attached on your environment? I would like to see if you get the error that i did on 2005 in your 2008 environment. A guess is that perhaps the association still happens, but you don't get the error when dropped due to some new code. Out of curiosity I will look into it that.

As far as being a feature, I don't like features that cause errors. Perhaps the implementation could be different. Like it should auto-switch to the best index available, and if no index create whatever object is needed to hold everything together. Based on my guess above, maybe that's why there is no error in 2008.


Jimmy

"I'm still learning the things i thought i knew!"
Post #1188372
Posted Tuesday, October 11, 2011 6:14 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 11:49 PM
Points: 833, Visits: 1,365
I do get the same error message, yes:
Msg 3723, Level 16, State 6, Line 2
An explicit DROP INDEX is not allowed on index 't1.ix_t1'. It is being used for FOREIGN KEY constraint enforcement.




Ole Kristian Velstadbråten Bangås - Virinco - Facebook - Twitter

Concatenating Row Values in Transact-SQL
Post #1188376
Posted Tuesday, October 11, 2011 6:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:29 PM
Points: 11,286, Visits: 13,072
I don't think I'd call this a bug. SQL Server can use the Primary Key or a Unique Constraint/index to as the reference column(s) for a foreign key. In this case (or these cases for those who have experienced it), SQL Server is probably using a unique clustered index to enforce a foreign key because it believes that it will improve performance. I'd have to assume that the lead column in the unique clustered index is the same column as the primary key. I'd be checking to see if both indexes are needed and perhaps make the unique clustered index the PK.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1188397
Posted Tuesday, October 11, 2011 12:36 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 9:53 AM
Points: 8, Visits: 60
Hi,

I hope you guys can help me get something straightened out here. I ran this query on the Master DB:

select f.name
, i.name
, object_name(i.object_id) as tablename
, i.is_unique,i.is_primary_key
, i.type_desc
, f.key_index_id
from sys.foreign_keys f
join sys.indexes i
on i.object_id = f.referenced_object_id
and i.index_id = f.key_index_id

With these results.

name name tablename is_unique is_primary_key type_desc key_index_id
FK_ProjectWorkOrders_Projects PK_Projects Projects 1 1 CLUSTERED 1
FK_ProjectFacilities_Projects PK_Projects Projects 1 1 CLUSTERED 1
FK_WorkOrderTypeEmployees_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1
FK_ProjectWorkOrders_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1
FK_ProjectWorkOrderEmployees_ProjectWorkOrders PK_ProjectWorkOrders ProjectWorkOrders 1 1 CLUSTERED 1

I also ran this query on the DB where these tables are and got 156 results which is pretty much what I expected.

Why are only some of the indexes showing up in the Master DB? These are the most recent table additions and foreign keys.

Very sorry if this is too far off topic. If so I will repost as a new thread.

Thank you,
Richard
Post #1188692
Posted Tuesday, October 11, 2011 2:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:29 PM
Points: 11,286, Visits: 13,072
rstelma (10/11/2011)
Hi,

I hope you guys can help me get something straightened out here. I ran this query on the Master DB:

select f.name
, i.name
, object_name(i.object_id) as tablename
, i.is_unique,i.is_primary_key
, i.type_desc
, f.key_index_id
from sys.foreign_keys f
join sys.indexes i
on i.object_id = f.referenced_object_id
and i.index_id = f.key_index_id

With these results.

name name tablename is_unique is_primary_key type_desc key_index_id
FK_ProjectWorkOrders_Projects PK_Projects Projects 1 1 CLUSTERED 1
FK_ProjectFacilities_Projects PK_Projects Projects 1 1 CLUSTERED 1
FK_WorkOrderTypeEmployees_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1
FK_ProjectWorkOrders_WorkOrderTypes PK_WorkOrderTypes WorkOrderTypes 1 1 CLUSTERED 1
FK_ProjectWorkOrderEmployees_ProjectWorkOrders PK_ProjectWorkOrders ProjectWorkOrders 1 1 CLUSTERED 1

I also ran this query on the DB where these tables are and got 156 results which is pretty much what I expected.

Why are only some of the indexes showing up in the Master DB? These are the most recent table additions and foreign keys.

Very sorry if this is too far off topic. If so I will repost as a new thread.

Thank you,
Richard


I wouldn't expect to see any results for user tables when running that query. Those DMV's are scoped to the database that you are in. If there are FK's showing in master that means the tables, indexes, and foreign keys were created in master instead of the user database or in addition to the user database.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #1188769
Posted Tuesday, October 11, 2011 4:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 9:53 AM
Points: 8, Visits: 60
Hi Jack,

Damn database gremlins.

Now when I run the query on the Master I get no results and 156 on the DB where the tables exist and where I expect to see the Foreign Keys. I swear I ran that on the Master and kept switching back and forth.

Anyway... thank you. Very much appreciate your reply.

Richard
Post #1188825
Posted Tuesday, October 11, 2011 7:30 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Sunday, September 7, 2014 5:17 PM
Points: 536, Visits: 761
Thank you for sharing this obscure issue. I hope it saves me time someday.


Post #1188840
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse