Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Delete with TABLOCK Expand / Collapse
Author
Message
Posted Sunday, January 27, 2013 11:08 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 5:09 AM
Points: 415, Visits: 1,458
To delete rows in a heap and deallocate pages, use one of the following methods.

Specify the TABLOCK hint in the DELETE statement. Using the TABLOCK hint causes the delete operation to take a shared lock on the table instead of a row or page lock. This allows the pages to be deallocated. For more information about the TABLOCK hint, see Table Hint (Transact-SQL).
Use TRUNCATE TABLE if all rows are to be deleted from the table.
Create a clustered index on the heap before deleting the rows. You can drop the clustered index after the rows are deleted. This method is more time consuming than the previous methods and uses more temporary resources.


How can we prove this?

I have tried checking threw DBCC command, but it is showing the space is allocated.

Can anyone prove for me please.


Post #1412186
Posted Monday, January 28, 2013 4:02 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, March 26, 2013 8:41 AM
Points: 2,562, Visits: 3,451
SQL* (1/27/2013)
[b]How can we prove this?

I have tried checking threw DBCC command, but it is showing the space is allocated.

Can anyone prove for me please.
What has to be proved ?


-------Bhuvnesh----------
While 1 = 1 (Learning SQL....)
Click to get fast response of your post
Post #1412321
Posted Monday, January 28, 2013 4:20 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:48 AM
Points: 1,076, Visits: 1,922
SQL* (1/27/2013)
To delete rows in a heap and deallocate pages, use one of the following methods.

Specify the TABLOCK hint in the DELETE statement. Using the TABLOCK hint causes the delete operation to take a shared lock on the table instead of a row or page lock. This allows the pages to be deallocated. For more information about the TABLOCK hint, see Table Hint (Transact-SQL).
Use TRUNCATE TABLE if all rows are to be deleted from the table.
Create a clustered index on the heap before deleting the rows. You can drop the clustered index after the rows are deleted. This method is more time consuming than the previous methods and uses more temporary resources.


How can we prove this?

I have tried checking threw DBCC command, but it is showing the space is allocated.

Can anyone prove for me please.


Could you please make it more clear? What is the problem here anyway?


- Divine Flame
Post #1412335
Posted Monday, January 28, 2013 4:32 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 5:09 AM
Points: 415, Visits: 1,458
Divine Flame (1/28/2013)
SQL* (1/27/2013)
To delete rows in a heap and deallocate pages, use one of the following methods.

Specify the TABLOCK hint in the DELETE statement. Using the TABLOCK hint causes the delete operation to take a shared lock on the table instead of a row or page lock. This allows the pages to be deallocated. For more information about the TABLOCK hint, see Table Hint (Transact-SQL).
Use TRUNCATE TABLE if all rows are to be deleted from the table.
Create a clustered index on the heap before deleting the rows. You can drop the clustered index after the rows are deleted. This method is more time consuming than the previous methods and uses more temporary resources.


How can we prove this?

I have tried checking threw DBCC command, but it is showing the space is allocated.

Can anyone prove for me please.


Could you please make it more clear? What is the problem here anyway?


From the above statement from MSDN, if we use TABLOCK in the DELETE statement then the allocated pages will be deallocated.

I have checked whether there is any available space/allocated for the table, by using sp_Spaceused it is showing the allocated pages as it is. So the above statement is not true, right? But it is from MSDN so we can't tell this is wrong statement.

For the TRUNCATE TABLE statement, it is deallocating the pages.


Post #1412342
Posted Monday, January 28, 2013 5:10 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:51 AM
Points: 37,738, Visits: 30,010
SQL* (1/28/2013)
From the above statement from MSDN, if we use TABLOCK in the DELETE statement then the allocated pages will be deallocated.


If the table is a heap.

So the above statement is not true, right?


It's true.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1412354
Posted Monday, January 28, 2013 5:47 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:48 AM
Points: 1,076, Visits: 1,922
SQL* (1/28/2013)


From the above statement from MSDN, if we use TABLOCK in the DELETE statement then the allocated pages will be deallocated.

I have checked whether there is any available space/allocated for the table, by using sp_Spaceused it is showing the allocated pages as it is. So the above statement is not true, right? But it is from MSDN so we can't tell this is wrong statement.

For the TRUNCATE TABLE statement, it is deallocating the pages.


If the table is heap ( i.e. it does not have clustered index) & you use TABLOCK when deleting, the pages will be deallocated. It can be shown using undocumented command DBCC IND:

USE AdventureWorks
GO
IF OBJECT_ID('TheTable') IS NOT NULL
BEGIN
DROP TABLE TheTable;
END
GO
CREATE TABLE TheTable
(
ID INT IDENTITY(1,1),
Data VARCHAR(2000)
)
GO
INSERT INTO TheTable (Data) VALUES (REPLICATE('SomeData',150))
GO 1000
GO
--Check the number of pages allocated to the table
GO
DBCC IND ('AdventureWorks','TheTable',-1) -- 175 pages
GO
DELETE FROM TheTable WITH (TABLOCK)
GO
DBCC IND ('AdventureWorks','TheTable',-1) -- Only 1 page (IAM page) available & all other pages deallocated
--after deleting with TABLOCK


In the above example if you delete the data without using TABLOCK the pages will not be deallocated (that also can be checked using the same DBCC IND).


- Divine Flame
Post #1412373
Posted Monday, January 28, 2013 10:21 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Today @ 5:09 AM
Points: 415, Visits: 1,458
Thank you.

Post #1412758
Posted Tuesday, February 05, 2013 7:38 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:27 AM
Points: 350, Visits: 1,340
http://eohmicrosoft.blogspot.com/2012/12/heaps-of-trouble.html

http://thesqlguy.blogspot.com/
Post #1415839
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse