all groups > sql server programming > october 2005 >
You're in the

sql server programming

group:

How to avoid blocking when doing insert and range delete


How to avoid blocking when doing insert and range delete Morris
10/5/2005 9:16:44 PM
sql server programming: I have a table required to do housekeeping (range delete). It takes
several minutes. I found that the other program is unable to insert
into the table during the delete. The insert statement is blocked and
finally timed out. The new record does not fall into the delete range.

Besides of increasing the time-out value, is it possible to set
somethig (isolation level, table hint) to make the insert statement
unblocked?

P.S. Is this problem related to the index? I have both cluster and
non-cluster indexes in the table.

-Morris
RE: How to avoid blocking when doing insert and range delete R.D
10/5/2005 9:47:02 PM
Morris
You can use TRUNCATE TABLE to finish delete fast. But it is minimally
logged. However we can't say exact problem unless we know the size of
deletion( how many rows are being deleted in that time)
[quoted text, click to view]
Index on column used in the where clause of delete definitely boosts the
performance. We cant say unless we the script.
[quoted text, click to view]

you can try to find out what is blocking what and where. use sp_lockinfo and
find out root block. I think you got to concentrate on delete statement than
insert

Regards
R.D


--
Regards
R.D
--Knowledge gets doubled when shared


[quoted text, click to view]
Re: How to avoid blocking when doing insert and range delete R.D
10/5/2005 10:04:02 PM
Yes I have seen that range very late . Thanks for that
--
Regards
R.D
--Knowledge gets doubled when shared


[quoted text, click to view]
Re: How to avoid blocking when doing insert and range delete R.D
10/5/2005 10:06:02 PM
oops
TRUNCATE WILL NOT WORK FOR RANGE
--
Regards
R.D
--Knowledge gets doubled when shared


[quoted text, click to view]
Re: How to avoid blocking when doing insert and range delete Morris
10/5/2005 10:25:36 PM
The housekeeping statement is
delete from event where eventtime >= '2005-07-01' and eventtime <
'2005-07-02'
There is around 4x,xxx records per day. It takes around 5 minutes to
remove.

The blocked statement is
insert into event values ( ......, '2005-10-06')
When delete is in progress. starting this statement will result as
blocking until the delete finishs.

I can simulate the problem in Query Analyser. I am trying to set
isolation level and table hint. But it is still not working. I want to
know if SQL Server 2000 can handle both statements simultaneously?

Thanks.

-Morris
Re: How to avoid blocking when doing insert and range delete R.D
10/5/2005 11:17:02 PM
Morris
In this situation you can set lock time out value .This should help you
BOL Has this:
Customizing the Lock Time-out
When Microsoft® SQL Server™ 2000 cannot grant a lock to a transaction on a
resource because another transaction already owns a conflicting lock on that
resource, the first transaction becomes blocked waiting on that resource. If
this causes a deadlock, SQL Server terminates one of the participating
transactions (with no time-out involved). If there is no deadlock, the
transaction requesting the lock is blocked until the other transaction
releases the lock. By default, there is no mandatory time-out period, and no
way to test if a resource is locked before locking it, except to attempt to
access the data (and potentially get blocked indefinitely).



Note The sp_who system stored procedure can be used to determine if a
process is being blocked, and who is blocking it.


The LOCK_TIMEOUT setting allows an application to set a maximum time that a
statement waits on a blocked resource. When a statement has waited longer
than the LOCK_TIMEOUT setting, the blocked statement is canceled
automatically, and error message 1222 "Lock request time-out period exceeded"
is returned to the application.

However, any transaction containing the statement is not rolled back or
canceled by SQL Server. Therefore, the application must have an error handler
that can trap error message 1222. If an application does not trap the error,
it can proceed unaware that an individual statement within a transaction has
been canceled, and errors can occur because statements later in the
transaction may depend on the statement that was never executed.

Implementing an error handler that traps error message 1222 allows an
application to handle the time-out situation and take remedial action for
example, automatically resubmitting the statement that was blocked, or
rolling back the entire transaction.

To determine the current LOCK_TIMEOUT setting, execute the @@LOCK_TIMEOUT
function, for example:

DECLARE @Timeout int
SELECT @Timeout = @@lock_timeout
SELECT @Timeout
GO


See Also

@@LOCK_TIMEOUT

SET LOCK_TIMEOUT

sp_who



--
Regards
R.D
--Knowledge gets doubled when shared


[quoted text, click to view]
Re: How to avoid blocking when doing insert and range delete Jonathan Chong
10/6/2005 12:00:00 AM
I had similar problem sometime ago and what I did was to breakdown to
smaller range for purging.

In your case, I would suggest to breakdown up to hours instead.

[quoted text, click to view]

Re: How to avoid blocking when doing insert and range delete Jonathan Chong
10/6/2005 12:00:00 AM
I don't think TRUNCATE TABLE will do the job as TRUNCATE TABLE removes all
rows from a table.
As Morris stated, he needs to delete a specific range of records, not entire
table.

[quoted text, click to view]

Re: How to avoid blocking when doing insert and range delete Morris
10/6/2005 12:42:48 AM
Hi Uri Dimant

Yeah ... there is a cluster index on eventtime column.

I am trying to remove all the index to test again.

Is it true that there is no way for SQL Server 2000 to insert a new
record during a long delete statement even the record does not fall
into the delete range?

Thank you.

-Morris
Re: How to avoid blocking when doing insert and range delete Morris
10/6/2005 1:40:27 AM
Okay ..... so i have to breakdown the delete range.

Thanks to R.D, Jonathan Chong, Uri Dimant.

-Morris
RE: How to avoid blocking when doing insert and range delete Srihari
10/6/2005 7:21:13 AM
HI,

You can do one thing before deleting you can disable writing to log using
command which can be found in BOL.
After deleting you can enable the log. This will reduces time.

With Regards,
AHARI

[quoted text, click to view]
Re: How to avoid blocking when doing insert and range delete Uri Dimant
10/6/2005 10:02:01 AM
Morris
Do you have an index define on eventtime column?

http://www.sql-server-performance.com/blocking.asp



\

[quoted text, click to view]

Re: How to avoid blocking when doing insert and range delete Morris
10/6/2005 8:01:41 PM
I cannot find the command. Are you talking about transaction log? How
can I disable writing to log?

Thank you.

-Morris
Re: How to avoid blocking when doing insert and range delete R.D
10/6/2005 11:27:03 PM
Morris
Have you tried this solution. This is my own guess. I think this should work.
1) Add your table to more than one filegroup/files
2) use ROWLOCK hint in the delete command.

--
Regards
R.D
--Knowledge gets doubled when shared


[quoted text, click to view]
Re: How to avoid blocking when doing insert and range delete Srihari
10/8/2005 1:40:04 AM
Hi Morris and R.D

I have tried this there is command to stop writing to log.



With Regards,
AHARI

[quoted text, click to view]
Re: How to avoid blocking when doing insert and range delete Morris
10/9/2005 8:38:10 PM
Srihari

How can you do it? What is the command?

Morris
AddThis Social Bookmark Button