Your problem is the isolation level you are using. If you do not change it, SQL Server (and many other databases) work in the mode that it chooses, it will be locked during uncommitted reads. You want to change SQL Server to use MVCC instead (by default, Oracle, MySQL and SQL Server both have it), and your problem will go away.
From INSTALL BET ISOLATION LEVEL (Transact-SQL) :
READ THE COMMITTEE
Indicates that operators cannot read data that has been changed but not committed by other transactions. This prevents dirty reads. Data can be changed by other transactions between separate statements within the current transaction, resulting in irreproducible reads or phantom data. This parameter is a SQL Server standard.
The behavior of READ COMMITTED depends on the setting of the Database READ_COMMITTED_SNAPSHOT option:
- If READ_COMMITTED_SNAPSHOT is set to OFF (the default value), the Database Engine uses common locks to prevent other transactions from changing rows during the current transaction reading operations are performed. General locks also block instructions from reading lines changed by other transactions until another transaction is completed. The type of shared lock determines when it will be released. Lowercase locks released before the next line processed. Page locks are released when the next page is read and the table locks are released when approval finishes.
- If READ_COMMITTED_SNAPSHOT is set to ON, the Database Engine uses a version string to represent each statement with a sequence of transactions a snapshot of the data as it existed at the beginning of the statement. Locks are not used to protect data from updating on other transactions.
When the READ_COMMITTED_SNAPSHOT database is enabled, you can use the READCOMMITTEDLOCK table hint table to request a general lock instead of the version control string for individual statements in transactions running on the READ isolation level COMMITTED.
(in italics)
Change your database configuration to turn READ_COMMITTED_SNAPSHOT ON.
In addition, try to minimize the duration of the transaction and make sure that you complete the transaction in the background (this makes 10,000 inserts per hour), because if it is never completed, the choice will be blocked forever (the default setting).
cletus
source share