Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 shrinkdatabase

Author  Topic 

frank.svs
Constraint Violating Yak Guru

368 Posts

Posted - 2011-06-16 : 13:21:25
Hi,

I have 2 queries/questions on DBCC SHRINKDATABASE and how it works internally.
I am trying to visualize what sql is trying to do but finding some difficulty.

Can anyone help me out on this regard?

--create a test database of 400 mb size with 2 data files in PRIMARY filegroup
USE [master]
GO
CREATE DATABASE [testdb] ON PRIMARY
( NAME = N'testdb',
FILENAME = N'C:\DATA\testdb.mdf' ,
SIZE = 307200KB , --300 mb
MAXSIZE = UNLIMITED,
FILEGROWTH = 1024KB ),
( NAME = N'testdb02',
FILENAME = N'C:\DATA\testdb02.ndf' ,
SIZE = 102400KB , --100 mb
MAXSIZE = UNLIMITED,
FILEGROWTH = 1024KB )
LOG ON
( NAME = N'testdb_log',
FILENAME = N'C:\DATA\testdb_log.LDF' ,
SIZE = 576KB ,
MAXSIZE = 2048GB ,
FILEGROWTH = 10%)
GO

--query to check the free space
use testdb
go
SELECT Name, Filename,
CONVERT(Decimal(15,2),ROUND(a.Size/128.000,2)) [Currently Allocated Space (MB)],
CONVERT(Decimal(15,2),ROUND(FILEPROPERTY(a.Name,'SpaceUsed')/128.000,2)) AS [Space Used (MB)],
CONVERT(Decimal(15,2),ROUND((a.Size-FILEPROPERTY(a.Name,'SpaceUsed'))/128.000,2)) AS [Available Space (MB)]
FROM dbo.sysfiles a (NOLOCK)
go

--output
Name Filename CurrentAllocatedSpace(MB) SpaceUsed(MB) AvailableSpace(MB)
testdb C:\DATA\testdb.mdf 300.00 1.31 298.69
testdb_log C:\DATA\testdb_log.LDF 0.56 0.29 0.27
testdb02 C:\DATA\testdb02.ndf 00.00 0.06 99.94


-- created table
use testdb
go
create table test
(c1 char(1024)
)

-- fills 200 mb data
insert into test
select 'A'
GO 204800


use testdb
go
SELECT Name, Filename,
CONVERT(Decimal(15,2),ROUND(a.Size/128.000,2)) [Currently Allocated Space (MB)],
CONVERT(Decimal(15,2),ROUND(FILEPROPERTY(a.Name,'SpaceUsed')/128.000,2)) AS [Space Used (MB)],
CONVERT(Decimal(15,2),ROUND((a.Size-FILEPROPERTY(a.Name,'SpaceUsed'))/128.000,2)) AS [Available Space (MB)]
FROM dbo.sysfiles a (NOLOCK)
go


Name Filename CurrentAllocatedSpace(MB) SpaceUsed(MB) AvailableSpace(MB)
testdb C:\DATA\testdb.mdf 300.00 153.75 146.25
testdb_log C:\DATA\testdb_log.LDF 0.56 0.22 0.34
testdb02 C:\DATA\testdb02.ndf 100.00 76.31 23.69



-- First question, why it dont insert data in the first data file "C:\DATA\testdb.mdf" and why it is storing some data in "C:\DATA\testdb02.ndf" ?


-- shrik database to 40mb
USE [testdb]
GO
DBCC SHRINKDATABASE(N'testdb',40)
GO

/*
DbId FileId CurrentSize MinimumSize UsedPages EstimatedPages
5 1 32792 288 19672 19672
*/

-- Secondly question, why it is shriking the space from only file id "1" i.e mdf and why not .ndf??
-- I can use, dbcc shrinfile and specify the target sizein MB. Basically,i want to understand the behaviour of
-- DBCC SHRINKDATABASE. Also, in msdn documentation read the below and want to know better on this
http://msdn.microsoft.com/en-us/library/ms190488.aspx

How DBCC SHRINKDATABASE Works

DBCC SHRINKDATABASE shrinks data files on a per-file basis, but shrinks log files as if all the log files existed in one contiguous log pool. Files are always shrunk from the end.
MSDN says per-file basis, but i dont observe shrinking is taking place per file. It has picked up fileid "1" only.


use testdb
go
SELECT Name, Filename,
CONVERT(Decimal(15,2),ROUND(a.Size/128.000,2)) [Currently Allocated Space (MB)],
CONVERT(Decimal(15,2),ROUND(FILEPROPERTY(a.Name,'SpaceUsed')/128.000,2)) AS [Space Used (MB)],
CONVERT(Decimal(15,2),ROUND((a.Size-FILEPROPERTY(a.Name,'SpaceUsed'))/128.000,2)) AS [Available Space (MB)]
FROM dbo.sysfiles a (NOLOCK)
go

--output
Name Filename CurrentAllocatedSpace(MB) SpaceUsed(MB) AvailableSpace(MB)
testdb C:\DATA\testdb.mdf 256.19 153.75 102.44
testdb_log C:\DATA\testdb_log.LDF 0.56 0.23 0.34
testdb02 C:\DATA\testdb02.ndf 100.00 76.31 23.69


Thanks in advance.

frank.svs
Constraint Violating Yak Guru

368 Posts

Posted - 2011-06-16 : 13:30:08
small correction target % is 40% and not 40M.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-16 : 13:40:39
There's some more information on how it works (and why you shouldn't use it) here:

http://blogs.msdn.com/b/sqlserverstorageengine/archive/2006/06/13/629059.aspx
http://www.sqlskills.com/BLOGS/PAUL/category/Shrink.aspx
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-06-17 : 17:21:16
Not about how it works, but more about why you shouldn't use it: [url]http://mirko-marovic-eng.blogspot.com/2011/05/to-be-and-not-to-shrink-that-is-answer.html[/url]

Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-17 : 17:43:11
No offense, but here's some better articles that cover this topic:

http://sqlinthewild.co.za/index.php/2007/09/08/shrinking-databases/
http://www.sqlskills.com/BLOGS/PAUL/post/Why-you-should-not-shrink-your-data-files.aspx
http://www.brentozar.com/archive/2009/08/stop-shrinking-your-database-files-seriously-now/

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -