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 scan on foreign key reference Expand / Collapse
Author
Message
Posted Wednesday, November 14, 2012 2:26 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, July 7, 2014 8:17 AM
Points: 295, Visits: 402
I have a table TabA with a column that is a foreign key to the primary key of table TabB.
I have also an index on this column from TabA.
I have a stored procedure that selects some data from a table TabC and insert into TabA:

CREATE TABLE TabA
(
ColA1 int,
ColA2 int,
.............
);

ALTER TABLE TabA ADD CONSTRAINT PK_TabA PRIMARY KEY CLUSTERED (ColA1);
ALTER TABLE TabA ADD CONSTRAINT FK_TabA_TabB FOREIGN KEY (ColA2) REFERENCES TabB(ColB1);
CREATE NONCLUSTERED INDEX IX_ColA2 ON TabA(ColA2);

CREATE TABLE TabB
(
ColB1 int,
ColB2 int,
.............
);
ALTER TABLE TabB ADD CONSTRAINT PK_TabB PRIMARY KEY CLUSTERED(ColB1);

CREATE TABLE TabC
(
ColC1 int,
ColC2 int,
.............
);


INSERT INTO TabA (ColA1, ColA2,.....)
SELECT ColC1, ColC2......
FROM TabC
WHERE .....

This query causes deadlocks in application. The deadlocks are on the primary key of table TabB.
Looking inside the query plan I see that an index scan is made on primary key of TabB PK_TabB (although TabB does not take part in the query!) .
How can I remove the index scan on the primary key of table TabB from the query plan?
How can I force the an index scan/seek on index IX_ColA2 from TabA instead of index scan on primary key of TabB?

Thanks
Post #1384481
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse