/*============================================================================
  File:     5b_Baselines_CacheUsage_QueryData.sql

  Summary:  This script contains the statements to
			regularly query the captured cache usage data.

  Date:     April 2017

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

  (c) 2015, SQLskills.com. All rights reserved.

  For more scripts and sample code, check out 
    http://www.SQLskills.com

  You may alter this code for your own *non-commercial* purposes. You may
  republish altered code as long as you include this copyright and give due
  credit, but you must obtain prior permission before blogging this code.
  
  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.
============================================================================*/

USE [BaselineData];
GO


/*
	View all captured data
*/
SELECT *
FROM [dbo].[SQLskills_CacheUseByDB]
ORDER BY Rownum



/*
	View database consuming most cache
	space per collection
*/
SELECT  
	[w].[CaptureDate],
	[w].[DatabaseName],
	[w].[CachedSizeMB]
FROM    [dbo].[SQLskills_CacheUseByDB] [w]
        JOIN ( SELECT   MIN([RowNum]) AS [RowNumber] ,
                        [CaptureDate]
               FROM     [dbo].[SQLskills_CacheUseByDB]
               WHERE    [CaptureDate] IS NOT NULL
                        AND [CaptureDate] > GETDATE() - 30
               GROUP BY [CaptureDate]
             ) [m] ON [w].[RowNum] = [m].[RowNumber]
ORDER BY [w].[CaptureDate];

