Solved

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

Posted on 2010-11-21
8
753 Views
Last Modified: 2012-05-10
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.

0
Comment
Question by:stebennettsjb
8 Comments
 
LVL 7

Expert Comment

by:rashmi_vaghela
Comment Utility
0
 
LVL 5

Expert Comment

by:SinghAmandeep
Comment Utility
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
0
 

Author Comment

by:stebennettsjb
Comment Utility
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
0
 
LVL 6

Accepted Solution

by:
subhashpunia earned 500 total points
Comment Utility
In SSMS right click Database > Reports > Standard Reports > Disk Usage > here you will see the fata and log file growth status.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:stebennettsjb
Comment Utility
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
0
 
LVL 9

Expert Comment

by:sarabhai
Comment Utility
Pls check sys.master_files view
0
 

Author Comment

by:stebennettsjb
Comment Utility
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
0
 

Author Comment

by:stebennettsjb
Comment Utility
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
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now