Free the pages after deletion of data
-
Monday, April 30, 2012 12:07 PM
Hi expert
I have one confusion, I have one table in that I insert some data and I checked SP_SPACEUSED its showing 16kb data, But in next step when I delete all data from table and check it once again using SP_SPACEUSED still its showing 16 kb data . It means sql server engine not releasing
the pages after deletion of data.So I want to know that is there any option we have to release these blank pages for another use? or if no then when sql server engine will release it automatically?
Regards Vikas Pathak
All Replies
-
Monday, April 30, 2012 12:14 PM
Run DBCC UPDATEUSAGE first then run sp_spaceused. see BOL for more info on dbcc updateusage
vt
Please mark answered if I've answered your question and vote for it as helpful to help other user's find a solution quicker
- Proposed As Answer by Seth Lynch Monday, April 30, 2012 2:47 PM
-
Monday, April 30, 2012 12:26 PM
If you deleted data from the table, it Freeing space for deleted records, sql server marks pages containing deleted records as free pages in it,
It notifies that they belong to the singleObject(ex- table). When new data are inserted against on the same object, they are put into those pages first. Once those pages are filled up as per the fillfactor, SQL Server will allocate new pages.
Rama Udaya.K ramaudaya.blogspot.com ---------------------------------------- Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Monday, April 30, 2012 1:29 PM
Hope above responses answer your query & additionally to reduce the size the file size you can look at following t-sql:
USE <Database_Name>;
GO
DBCC SHRINKFILE (<Database_File_Name>, <Size_To_Reduce_To>); -
Monday, April 30, 2012 1:34 PM
@v. vt
Thanks for reply but I checked with DBCC UPDATEUSAGE , there is no effect still its showing 16kb data.
Regards Vikas Pathak
-
Monday, April 30, 2012 1:39 PM
@Udaya_Sequel
Thanks for giving reply. If you are right then suppose I have a table with huge data ,now its ocupies more than 50 pages, and due to some change in bussiness rule I delete 90% of data. see In this situation according to you, how much memory sql server will west.....
Regards Vikas Pathak
- Edited by vikas kumar pathak Monday, April 30, 2012 1:42 PM
-
Monday, April 30, 2012 1:58 PM
Vikas - has you know that group of 8Pages is equl to 1 extents,so I hope if you use the below link it explians clearly-
http://blogs.msdn.com/b/psssql/archive/2008/04/08/how-it-works-sql-server-page-allocations.aspx
also how much memory sql server will west.....
Didn't get this one.
Rama Udaya.K ramaudaya.blogspot.com ---------------------------------------- Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Monday, April 30, 2012 2:52 PM
Hi
Deleting data will not reduce the file size - it will alter the ratio between Used Space and Free Space. Ie you will have more free space in your data files.
this script will show you what the Free space to used space is for each file. Then you can run anuragsh's script to shirnk the files (although you'd do better to leave it with the free space)
SELECT name AS 'File Name', physical_name AS 'Physical Name', size/128 AS 'Total Size in MB', size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS 'Available Space In MB' FROM sys.database_files;
Seth
http://lqqsql.wordpress.com
- Edited by Seth Lynch Monday, April 30, 2012 2:52 PM
- Proposed As Answer by jgardner04 Monday, April 30, 2012 5:58 PM
- Marked As Answer by vikas kumar pathak Sunday, June 10, 2012 7:12 AM
-
Wednesday, May 02, 2012 8:15 PMModerator
Vikas,
When you delete data in a table, the data is NOT *physically* removed from the data pages instead it is just marked as Ghosted. There is a separate background process "Ghost Cleanup" wakes up and removes "n" number of pages per interval and I think its different in SQL Server 2005 vs 2008 and later. I am adding few references below and go thru them. Depending on the volume of data that's deleted, sometimes Ghost Cleanup process can't keep up and you may want to rebuild the index.
http://www.sqlskills.com/BLOGS/PAUL/post/Inside-the-Storage-Engine-Ghost-cleanup-in-depth.aspx
http://www.sqlskills.com/BLOGS/PAUL/post/Ghost-cleanup-redux.aspx
http://www.sqlskills.com/blogs/jonathan/post/An-XEvent-a-Day-(15-of-31)-Tracking-Ghost-Cleanup.aspx
http://dba.stackexchange.com/questions/14615/sql-server-empty-table-is-slow-after-deleting-all-12-million-records
Sankar Reddy
Blog: http://SankarReddy.com/
Twitter: http://twitter.com/SankarReddy13/- Marked As Answer by vikas kumar pathak Friday, May 04, 2012 10:16 AM

