简体   繁体   中英

Using foreign key in SQL server and DB locks

I have been working with SQL Server for a long time I always use FKs and indexes when a logic connection between tables exist

Example:

MyTable1
{
    ID      BIGINT IDENTITY (1, 1)  NOT NULL,
    SomeData    NVARCHAR(255)       NOT NULL,
    MyFK        BIGINT          NULL -- this is a FK to MyTable2.ID
}

MyTable2
{
    ID      BIGINT IDENTITY (1, 1)  NOT NULL,
    SomeData    NVARCHAR(255)       NOT NULL
}

Now to the problem, When I execute some bulk update operations on MyTable1 that update MyFK, and in the same time execute an insert statements to MyTable2, we hang till a timeout occur or the update is done and the locks are released.

As far as I know when inserting to a table that has FKs, the DB engine needs to get a lock on the relevant table to validate the FK, and that is the source for the problem.

Things I tries to resolve the problem:

Both solutions lead me to deadlocks and bad performance.

When I removed the FK, it all worked well, but there is the risk of data corruption.

Questions:

  1. Is there any recommended set of rules on where to use a FK and where not to?
  2. Can you offer me any other solution but removing the FK to overcome my problem?

Just in case the page asasfrob found goes down, the answer it gave was:

Define the primary key on the parent table (TableA) as a non-clustered index. Once you do that, the issue won't occur since the look-up will occur against the non-clustered index and that won't be in a locked state since the PK column is not being modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM