SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Index scan on foreign key reference


Index scan on foreign key reference

Author
Message
ioani
ioani
SSC-Addicted
SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)

Group: General Forum Members
Points: 405 Visits: 414
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
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