| |||||
|
The DBA Script Thumb By: Rodney Landrum
On a recent slow Friday afternoon I was practicing my Origami skills with approved expense reports and consolidating hundreds of queries that I'd collected over the past 10 years as a DBA. I was suddenly struck with what I thought was a good idea: why not put my most-used DBA queries on one of my Red-Gate thumb drives so as to have them always available? Because of the imminent deadline of a presentation for an upcoming SQL Server Users group meeting, I figured I would then share these queries with other SQL souls. Excitedly, I set about creating the DBA script thumb for the presentation. I dubbed it the "Green Thumb", subtitled the "Top 1000 Most Used Queries for the DBA".
Listing 1 Set NoCount On --Check to see the temp table exists IF EXISTS ( SELECT Name FROM tempdb..sysobjects Where name like '#HoldforEachDB%' ) --If So Drop it DROP TABLE #HoldforEachDB_size --Recreate it CREATE TABLE #HoldforEachDB_size ( [DatabaseName] [nvarchar](75) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Size] [decimal] NOT NULL, [Name] [nvarchar](75) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [Filename] [nvarchar](90) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, ) ON [PRIMARY] IF EXISTS ( SELECT name FROM tempdb..sysobjects Where name like '#fixed_drives%' ) --If So Drop it DROP TABLE #fixed_drives --Recreate it CREATE TABLE #fixed_drives ( [Drive] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [MBFree] [decimal] NOT NULL ) ON [PRIMARY] --Insert rows from sp_MSForEachDB into temp table INSERT INTO #HoldforEachDB_size EXEC sp_MSforeachdb 'Select ''?'' as DatabaseName, Case When [?]..sysfiles.size * 8 / 1024 = 0 Then 1 Else [?]..sysfiles.size * 8 / 1024 End AS size,[?]..sysfiles.name, [?]..sysfiles.filename From [?]..sysfiles' --Select all rows from temp table (the temp table will auto delete when the connection is gone. INSERT INTO #fixed_drives EXEC xp_fixeddrives Select @@Servername print '' ; Select rtrim(Cast(DatabaseName as varchar(75))) as DatabaseName, Drive, Filename, Cast(Size as int) AS Size, Cast(MBFree as varchar(10)) as MB_Free from #HoldforEachDB_size INNER JOIN #fixed_drives ON LEFT(#HoldforEachDB_size.Filename, 1) = #fixed_drives.Drive GROUP BY DatabaseName, Drive, MBFree, Filename, Cast(Size as int) ORDER BY Drive, Size Desc print '' ; Select Drive as [Total Data Space Used |], Cast(Sum(Size) as varchar(10)) as [Total Size], Cast(MBFree as varchar(10)) as MB_Free from #HoldforEachDB_size INNER JOIN #fixed_drives ON LEFT(#HoldforEachDB_size.Filename, 1) = #fixed_drives.Drive Group by Drive, MBFree print '' ; Select count(Distinct rtrim(Cast(DatabaseName as varchar(75)))) as Database_Count from #HoldforEachDB_size
Figure 1 shows the output of the query, with the multiple result sets that provide an at-a-glance view of how much free space is available on the disk, and how much space is taken by all of the SQL database files on each drive:
Listing 2 SELECT sd.name, bs.TYPE, bs.database_name, max(bs.backup_start_date) as last_backup FROM master..sysdatabases sd Left outer join msdb..backupset bs on rtrim (bs.database_name) = rtrim(sd.name) left outer JOIN msdb..backupmediafamily bmf ON bs.media_set_id = bmf.media_set_id Group by sd.name, bs.TYPE, bs.database_name Order by sd.name,last_backup The output of the query from Listing 2 can be seen in Figure 2, where there are obviously several databases that have not been backed up, as indicated by a NULL value in the last_backup column. This is not something a DBA would want to see, especially when master and msdb are included in the list of databases not backed up:
Other Related Articles ... to read more DBA articles, visit http://dba.fyicenter.com/article/ |
||||