join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 



Free SQL Server monitoring resources

Monitoring SQL Server database transaction log space

Written By: Greg Robidoux -- 2/13/2007 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
Keeping track of your transaction log usage is key to understanding how your database transaction logs are being used as well as keeping track of how much space the transaction logs are using.  Depending on the recovery model that is set within your database and also the backup jobs that you run, the size of your transaction log can vary quite a bit.  So what is the best way to keep track of the transaction log usage?

Solution
SQL Server has a command that you can run to see the current size of the transaction logs and how much space is currently being utilized. The command is DBCC SQLPERF(logspace).  This is a very simple command to run and interpret and this is key to getting an understanding of how your transaction logs are being used.

Here is sample output after running the command.

As you can see there are only four columns. Below is a description of each of these.

  • Database Name - Name of the database for the log statistics displayed.
  • Log Size (MB) - Actual amount of space available for the log.
  • Log Space Used (%) - Percentage of the log file currently occupied with transaction log information.
  • Status - Status of the log file. Always 0.

If you run this command at different times you will see the Log Space Used (%) increase and decrease depending on the activity in your database, the recovery model that you have set and also whether you are running transaction log backups.  Also, if your transaction logs are set to auto grow and/or auto shrink you will see the size of the file either increase or decrease again depending on activity.

To gain some further insight it would be helpful to capture this data at a set time and then go back and analyze the data at a later time.  This can be done by sending the output of this command to a table.

Below is a simple way of beginning this process.  There are three pieces of code here:

  1. Stored Procedure spSQLPerf - the only purpose of this command is to be able to send the output from the DBCC command into a temporary table
  2. Table logSpaceStats - this will store the long term stats data
  3. Stored Procedure spGetSQLPerfStats - this calls spSQLPerf and inserts the data into table logSpaceStats
CREATE PROC dbo.spSQLPerf
AS
DBCC 
SQLPERF(logspace)
GO
 


CREATE TABLE dbo.logSpaceStats
(
id INT IDENTITY (1,1),
logDate datetime DEFAULT GETDATE(),
databaseName sysname,
logSize decimal(18,5),
logUsed decimal(18,5)
)
GO
 


CREATE PROC dbo.spGetSQLPerfStats
AS
SET 
NOCOUNT ON

CREATE TABLE 
#tFileList
(
databaseName sysname,
logSize decimal(18,5),
logUsed decimal(18,5),
status INT
)

INSERT INTO #tFileList
       
EXEC spSQLPerf

INSERT INTO logSpaceStats (databaseNamelogSizelogUsed)
SELECT databasenamelogSizelogUsed
FROM #tFileList

DROP TABLE #tFileList
GO

After you have created the above components you need to call the procedure as follows to capture the information.  This can be done manually or you can setup a scheduled job to have this run every hour or some other set schedule.

sp_getSQLPerfStats

To take a look at the results just query the logSpaceStats table.  This can be done across the board or for an individual database.

SELECT FROM dbo.logSpaceStats

Next Steps

  • This is just one component of monitoring your server and your transaction log usage.
  • Review this data to see how space usage is being used as well as a guideline to size your transaction logs appropriately
  • After large transactions run these commands to see how much transaction log space is being used.
Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Free SQL Server monitoring resources

  1. Get your free "Dynamic Management Views Starter Pack", including a SQL Server DMV eBooklet and more than 30 sample scripts.
  2. Download a free trial of SQL Response to monitor your SQL Servers and be alerted to problems in an intuitive interface and by email.
SQL Server DMV Starter Pack
SQL Response logo Red Gate Software - ingeniously simple tools

Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.



Red Gate Software - SQL Compare

Quickly and accurately deploy database changes with Red Gate’s SQL Compare. 2/3 of people who use a SQL comparison tool use Red Gate’s SQL Compare – the industry standard database comparison and deployment tool. “We rely on SQL Compare for every deployment.” Paul Tebbut, Technical Lead, Universal Music Group

Download now!

More SQL Server Tools
SQL Prompt

SQL Compare

SQL Backup

SQL diagnostic manager

SQL Data Generator


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try SQL Object Level Recovery Native from Red Gate to save time and disk space. Download a free trial.

SQL Server Issues? Not sure where to turn for answers? Innovative SQL DBA consultants

Become a member of the MSSQLTips community

Free whitepaper - Top 10 Things You Should Know About Optimizing SQL Server Performance



Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com