Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-21
8
Medium Priority
?
765 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 5

Expert Comment

by:SinghAmandeep
ID: 34186527
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
ID: 34186552
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 6

Accepted Solution

by:
subhashpunia earned 2000 total points
ID: 34187174
In SSMS right click Database > Reports > Standard Reports > Disk Usage > here you will see the fata and log file growth status.
0
 

Author Comment

by:stebennettsjb
ID: 34187455
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
ID: 34187456
Pls check sys.master_files view
0
 

Author Comment

by:stebennettsjb
ID: 34187499
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
ID: 34204279
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

916 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