/*============================================================================
  File:     1_Examining_VLFs.sql

  Summary:  This script shows how to look at VLFs for a database.

  Date:     April 2015

  SQL Server Version: 2005/2008/2008R2/2012/2014
------------------------------------------------------------------------------
  Written by Erin Stellato, SQLskills.com

  For more scripts and other useful content, go to:
	http://www.SQLskills.com

  This script is intended only as a supplement to demos and lectures
  given by Erin Stellato.  
  
  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
============================================================================*/

/*
	Start with fresh copy of AW
*/

/*
	Verify the recovery model
*/ 
USE [master];
GO

SELECT DATABASEPROPERTYEX('AdventureWorks2014', 'Recovery')

SELECT [name], [recovery_model_desc]
FROM [sys].[databases]
WHERE [name] = 'AdventureWorks2014';

/*
	check log file configuration
*/
USE [AdventureWorks2014];
GO

SELECT 
	[f].[name], 
	[f].[file_id], 
	[f].[type_desc], 
	[f].[physical_name], 
	[f].[size]*8/1024 [SizeMB],
	[f].[growth]*8/1024 [GrowthMB]
FROM [sys].[database_files] [f]
LEFT OUTER JOIN [sys].[filegroups] [fg] ON [f].[data_space_id] = [fg].[data_space_id]
WHERE [f].[type_desc] = 'LOG';

/*
	set file growth low
*/
USE [master]
GO
ALTER DATABASE [AdventureWorks2014] MODIFY FILE ( NAME = N'AdventureWorks2014_Log', FILEGROWTH = 10%)
GO

/*
	Check VLFs
*/
DBCC LOGINFO('AdventureWorks2014');
GO
--note number of VLFs:

/*
	Modify some data
*/
USE [AdventureWorks2014];
GO

UPDATE [Person].[Person] SET [FirstName] = 'Tony';
GO


/*
	Check VLFs again
*/
DBCC LOGINFO('AdventureWorks2014');
GO
--note number of VLFs:


/*
	Issue a checkpoint
*/
CHECKPOINT;


/*
	Check VLFs again
*/
DBCC LOGINFO('AdventureWorks2014');
GO


/*
	Modify more data and then check VLFs
*/
USE [AdventureWorks2014];
GO

UPDATE [Person].[Person]
SET [LastName] = 'Stark'
WHERE [FirstName] = 'Tony';

DBCC LOGINFO('AdventureWorks2014');
GO

UPDATE [Person].[Person]
SET [LastName] = 'Stark'
WHERE [FirstName] like 'T%';

DBCC LOGINFO('AdventureWorks2014');
GO

UPDATE [Person].[Person]
SET [Title] = 'Mr.'
WHERE [LastName] IN ('Stark');

DBCC LOGINFO('AdventureWorks2014');
GO


/*
	checkpoint one more time
*/
CHECKPOINT;

DBCC LOGINFO('AdventureWorks2014');
GO

