Link to home
Start Free TrialLog in
Avatar of stebennettsjb
stebennettsjb

asked on

SQL 2005/2008 Find how .mdf and .ldf files have grown - shrunk

Hi,

Is there a way to find out how much the files of a database both log and data have been growing? (ie a table that shows dates/times of when a log or data file had to expend and if a autoshrink was run how much space that cleared up)

We have very little space on our log file volume and it gets dangerously near full on a regular bases. When we are down to to 5-20mb our database manager then runs a shrink on all log files and we get a good 5-6gb of space back.

From what i understand its best to try and keep the files (.ldf and .mdf) at a static size to improve performance, so my theory was if i can find the ldf that is growing a lot i can A resize it to a larger size (and know the size to extend to) and if needed move it to a different location while we wait for more storage.

Hope this makes sense and it is possible.

Any help or suggestions gratefully received.

Avatar of rashmi_vaghela
rashmi_vaghela
Flag of India image

Avatar of SinghAmandeep
SinghAmandeep

Try this

--INF: How to Shrink the SQL Server 7.0 Transaction Log
-- SQL7  http://support.microsoft.com/support/kb/articles/q256/6/50.asp?id=256650&SD
-- SQL7  http://www.support.microsoft.com/kb/256650  
-- SQL2000 http://support.microsoft.com/kb/272318/en-us
-- SQL2005 http://support.microsoft.com/kb/907511/en-us
-- select db_name()
-- select * from sysfiles

-- THIS SCRIPT IS NOT INTENDED FOR SCHEDULED USAGE !! READ BOL and the urls !!

SET NOCOUNT ON
DECLARE @LogicalFileName sysname,
        @MaxMinutes INT,
        @NewSize INT


-- *** MAKE SURE TO CHANGE THE NEXT 3 LINES WITH YOUR CRITERIA. ***

SELECT  @LogicalFileName = 'Airtel_Log',  -- Use sp_helpfile to identify the logical file name that you want to shrink.
        @MaxMinutes = 10,               -- Limit on time allowed to wrap log.
        @NewSize = 305                  -- in MB

-- Setup / initialize
DECLARE @OriginalSize int
SELECT @OriginalSize = size -- in 8K pages
  FROM sysfiles
  WHERE name = @LogicalFileName
SELECT 'Original Size of ' + db_name() + ' LOG is ' +
        CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' +
        CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'
  FROM sysfiles
  WHERE name = @LogicalFileName
CREATE TABLE DummyTrans
  (DummyColumn char (8000) not null)


-- Wrap log and truncate it.
DECLARE @Counter   INT,
        @StartTime DATETIME,
        @TruncLog  VARCHAR(255)
SELECT  @StartTime = GETDATE(),
        @TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY'
-- Try an initial shrink.
DBCC SHRINKFILE (@LogicalFileName, @NewSize)
EXEC (@TruncLog)
-- Wrap the log if necessary.
WHILE     @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired
      AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)  -- the log has not shrunk    
      AND (@OriginalSize * 8 /1024) > @NewSize  -- The value passed in for new size is smaller than the current size.
  BEGIN -- Outer loop.
    SELECT @Counter = 0
    WHILE  ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))
      BEGIN -- update
        INSERT DummyTrans VALUES ('Fill Log')  -- Because it is a char field it inserts 8000 bytes.
        DELETE DummyTrans
        SELECT @Counter = @Counter + 1
      END   -- update
    EXEC (@TruncLog)  -- See if a trunc of the log shrinks it.
  END   -- outer loop
SELECT 'Final Size of ' + db_name() + ' LOG is ' +
        CONVERT(VARCHAR(30),size) + ' 8K pages or ' +
        CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'
  FROM sysfiles
  WHERE name = @LogicalFileName
DROP TABLE DummyTrans
PRINT '*** Perform a full database backup ***'
SET NOCOUNT OFF
Avatar of stebennettsjb

ASKER

Hi guys thanks for the replies.

Im trying to find how much the files are growing ie, run a script to show a table with all the ldf files and what size they are and the date that they autogrow.

Its basically monitoring that im looking to do not actually shrink the files, im comfortable with how to do that.

Thanks and hope that makes sense

s
ASKER CERTIFIED SOLUTION
Avatar of subhashpunia
subhashpunia
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

this sounds good but when i go to reports i get Custom Reports as the only option, no standard.

also would this give me access to T-SQL for the report. I was hoping to hook up the data into SSRS.

Thanks for all the help

S
Avatar of Sara bhai
Pls check sys.master_files view
sorry me being simple..

right clicked on the database folder instead of the server. I can now see Standard Reports but there is no Disk Usage report.

Thanks for the help

s
no one know how to get this report via TSQL and for all the databases instead of doing them one by one?

Thanks for the help so far :)

S