1

I have a complex Query (Without any locking hints) that takes data from many tables say Table1,Table2,Table3

Below is the code the code to retrieve data (there are no transactions)

  IDbCommand sqlCmd = dbHelper.CreateCommand(Helper.MyConnString, sbSQL.ToString(), CommandType.Text, arParms);
  sqlCmd.CommandTimeout = 300;
  ds = dbHelper.ExecuteDataset(sqlCmd);

In an application this query runs every 2 minutes

When i fire a simple update query say

Update Table1 set Col1='abc' where ID=100  

(where ID is int and primary key + clustered index)

the update query gets delayed and many times it is timeout Below is the log enter image description here

How can i fix this.

2 Answers 2

1

You could execute your query inside a transaction that has an isolation level of SNAPSHOT. That way, your query won't acquire any (shared) locks and your UPDATE doesn't have to wait for the exclusive lock (given that the source of the locks on the table that block your UPDATE is really your query that the application runs every two minutes...)

For reference, have a look at Working with Snapshot Isolation and SET TRANSACTION ISOLATION LEVEL on MSDN.

EDIT due to comment:

First, turn on ALLOW_SNAPSHOT_ISOLATION for your database:

ALTER DATABASE YourDB SET ALLOW_SNAPSHOT_ISOLATION ON

Then, write your query as follows:

SET TRANSACTION ISOLATION LEVEL SNAPSHOT;

SELECT      Column1, Columns2, ...
FROM        Table1
LEFT JOIN   Table2
ON          Table1.Columns45 = Table2.Column3
[... your complex query ...]

Is that enough for an example?

0
1

If you don't want wait reading queries while modifing data, it would be best to use READ_COMMITTED_SNAPSHOT.

It can be transparently on, and don't affect you app code and has no side effect.

SNAPSHOT has many side effects, for example, while you don't have locks on data modifications, you can have conflicting data problems on commiting, this problems very dificult to deal with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.