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:
removed the lock escalation option on the table http://msdn.microsoft.com/en-us/library/ms184286%28v=sql.105%29.aspx
Changing the locks on the index to be row based and not page based http://msdn.microsoft.com/en-us/library/ms189076%28v=sql.105%29.aspx
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:
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.