Index scan on foreign key reference

  • 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

  • the index scan is normal, from what i read, since the constraint for the foreign key needs to be validated; to do that, it has to check the existing values with that scan.

    i think the deadlock is coming from someplace other than that index scan.

    can you post the execution plan?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Also can you post the deadlock graph?

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply