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

Viewing 0 posts

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