Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Why does TRUNCATE TABLE hang?


Why does TRUNCATE TABLE hang?

Author
Message
Allen Nugent
Allen Nugent
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 119
I'm trying to empty a staging table using TRUNCATE TABLE in a stored procedure, but the sp just hangs.

The table is only 400k rows, nobody else should be accessing it, and the sp worked previously. The only thing I think I've changed is to add indexes.

What should I try?
peterdru401
peterdru401
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 128
There must be an open transaction on table which means there is a lock on table

u can follow these steps

check which query is lock the object u r trying to truncate by following.

Select blocking_session, sid, serial#, wait_class,seconds_in_wait From v$session where blocking_session is not NULL order by seconds_in_wait

let me know if problem is still there :-)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)

Group: General Forum Members
Points: 55955 Visits: 44706
Something's blocking the truncate.

Check sys.dm_exec_requests, see what the wait type is, what the blocking session is. See what that blocking session is/was doing.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


GilaMonster
GilaMonster
SSC Guru
SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)

Group: General Forum Members
Points: 55955 Visits: 44706
peterdru401 (10/24/2012)
check which query is lock the object u r trying to truncate by following.

Select blocking_session, sid, serial#, wait_class,seconds_in_wait From v$session where blocking_session is not NULL order by seconds_in_wait


Is v$session some custom view that you have? It's not a standard SQL Server object.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


sanket kokane
sanket kokane
Old Hand
Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)

Group: General Forum Members
Points: 369 Visits: 1019


u can follow these steps

check which query is lock the object u r trying to truncate by following.

[b]Select blocking_session, sid, serial#, wait_class,seconds_in_wait From v$session where blocking_session is not NULL order by seconds_in_wait


let me know if problem is still there :-)




You seems a Oracle Guy !

-----------------------------------------------------------------------------
संकेत कोकणे
Allen Nugent
Allen Nugent
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 119
Thanks, all.

"Invalid object name 'v$session'."

I queried sys.dm_exec_requests as recommended by Gail, but I don't know what I'm looking at. Nothing in the output refers to the problem table by name.

(Nb. I have had training in BIDS but not in SQL Server, and there are massive gaps in my self-education.)
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6389 Visits: 7660
Allen,

Short form, Truncate Table requires an exclusive lock on the table schema (SCH_M) to activate, which means that it can be the only thing accessing the table when it tries to run.

What you're going to end up having to do is try to figure out where you've got a hung transaction that's got a SCH_S lock against that table or one of the indexes. It's going to be a bit of a dig to pull it off and it can be a pain in the arse.

It may seem like overkill, but 'bounce' the server. Just stop/start the SQLServer process. It'll go through a shutdown/recovery process (which you DON'T want to stop for any reason if you can help it) but that should clear any lingering transactions or unexpected issues.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
George M Parker
George M Parker
Right there with Babe
Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)Right there with Babe (728 reputation)

Group: General Forum Members
Points: 728 Visits: 1472
Something has to be blocking the truncate table statement. This is kind of an old school script as it uses code from sp_lock, but it should tell you exactly which connections have locks on the table and what type of locks they are.

declare @tablename sysname
select @tablename = 'Replace with your table name'

select convert (smallint, req_spid) As spid,
rsc_dbid As dbid,
rsc_objid As ObjId,
rsc_indid As IndId,
substring (v.name, 1, 4) As Type,
substring (rsc_text, 1, 32) as Resource,
substring (u.name, 1, 8) As Mode,
substring (x.name, 1, 5) As Status
from master.dbo.syslockinfo,
master.dbo.spt_values v,
master.dbo.spt_values x,
master.dbo.spt_values u
where master.dbo.syslockinfo.rsc_type = v.number
and v.type = 'LR'
and master.dbo.syslockinfo.req_status = x.number
and x.type = 'LS'
and master.dbo.syslockinfo.req_mode + 1 = u.number
and u.type = 'L'
and rsc_objid = object_id(@tablename)
Allen Nugent
Allen Nugent
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 119
Thanks, Craig.

Unfortunately, I'm developing on a production server (not my choice), and there's a lot of stuff on it. I might be able to get it restarted over the weekend, but for now I think I'll have to use a surgical approach.
Allen Nugent
Allen Nugent
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 119
Thanks, George.

Your script turned up the same 3 SPID's I became suspicious of after I ran EXEC sp_who2. Should I just kill them? (They are my processes.)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search