Skip to content

Commit 3f0cbb8

Browse files
committed
Last set of standardised headings
1 parent 936a10a commit 3f0cbb8

9 files changed

+255
-150
lines changed

Statistics/Drop all statistics.sql

+68-52
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
-- Generate DROP statements for all statistics objects in all databases
1+
-- Drop all statistics
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script generates DROP statements for all statistics objects in all databases
24
-- From https://blogs.msdn.microsoft.com/mvpawardprogram/2013/09/09/sql-server-auto-statistics-cleanup/
35

46
USE [master];
57
GO
6-
8+
79
SET NOCOUNT ON;
8-
10+
911
-- Table to hold all auto stats and their DROP statements
1012
CREATE TABLE #commands
11-
(
12-
Database_Name sysname ,
13-
Table_Name sysname ,
14-
Stats_Name sysname ,
15-
cmd NVARCHAR(4000) ,
16-
CONSTRAINT PK_#commands PRIMARY KEY CLUSTERED ( Database_Name, Table_Name, Stats_Name )
17-
);
18-
13+
(
14+
Database_Name sysname NOT NULL,
15+
Table_Name sysname NOT NULL,
16+
Stats_Name sysname NOT NULL,
17+
cmd NVARCHAR(4000) NOT NULL,
18+
CONSTRAINT PK_#commands
19+
PRIMARY KEY CLUSTERED (
20+
Database_Name,
21+
Table_Name,
22+
Stats_Name
23+
)
24+
);
25+
1926
-- A cursor to browse all user databases
20-
DECLARE Databases CURSOR
21-
FOR
22-
SELECT [name]
23-
FROM sys.databases
24-
WHERE database_id > 4;
25-
26-
DECLARE @Database_Name sysname ,
27-
@cmd NVARCHAR(4000);
28-
27+
DECLARE Databases CURSOR LOCAL FAST_FORWARD FOR
28+
SELECT [name]
29+
FROM sys.databases
30+
WHERE database_id > 4;
31+
32+
DECLARE @Database_Name sysname,
33+
@cmd NVARCHAR(4000);
34+
2935
OPEN Databases;
30-
31-
FETCH NEXT FROM Databases INTO @Database_Name;
32-
36+
37+
FETCH NEXT FROM Databases
38+
INTO @Database_Name;
39+
3340
WHILE @@FETCH_STATUS = 0
34-
BEGIN
35-
-- Create all DROP statements for the database
36-
SET @cmd = 'SELECT N''' + @Database_Name + ''',
41+
BEGIN
42+
-- Create all DROP statements for the database
43+
SET @cmd
44+
= N'SELECT N''' + @Database_Name
45+
+ N''',
3746
so.name,
3847
ss.name,
3948
N''DROP STATISTICS [''
@@ -45,40 +54,47 @@ WHILE @@FETCH_STATUS = 0
4554
+ ''.[''
4655
+ ss.name
4756
+ ''];''
48-
FROM [' + @Database_Name + '].sys.stats AS ss
49-
INNER JOIN [' + @Database_Name + '].sys.objects AS so
57+
FROM [' + @Database_Name + N'].sys.stats AS ss
58+
INNER JOIN [' + @Database_Name + N'].sys.objects AS so
5059
ON ss.[object_id] = so.[object_id]
51-
INNER JOIN [' + @Database_Name + '].sys.schemas AS ssc
60+
INNER JOIN [' + @Database_Name
61+
+ N'].sys.schemas AS ssc
5262
ON so.schema_id = ssc.schema_id
5363
WHERE ss.auto_created = 1
5464
AND
5565
so.is_ms_shipped = 0';
56-
--SELECT @cmd; -- DEBUG
57-
-- Execute and store in temp table
58-
INSERT INTO #commands
59-
EXECUTE ( @cmd
60-
);
61-
-- Next Database
62-
FETCH NEXT FROM Databases INTO @Database_Name;
63-
END;
66+
--SELECT @cmd; -- DEBUG
67+
-- Execute and store in temp table
68+
INSERT INTO #commands
69+
EXECUTE (@cmd);
70+
-- Next Database
71+
FETCH NEXT FROM Databases
72+
INTO @Database_Name;
73+
END;
6474

65-
WITH Ordered_Cmd
66-
AS -- Add an ordering column to the rows to mark database context
75+
WITH Ordered_Cmd
76+
AS -- Add an ordering column to the rows to mark database context
6777

68-
( SELECT ROW_NUMBER() OVER ( PARTITION BY Database_Name ORDER BY Database_Name, Table_Name, Stats_Name ) AS Row_Num ,
69-
*
70-
FROM #commands
71-
)
72-
SELECT CASE WHEN Row_Num = 1
78+
(SELECT ROW_NUMBER() OVER (PARTITION BY Database_Name
79+
ORDER BY Database_Name,
80+
Table_Name,
81+
Stats_Name
82+
) AS Row_Num,
83+
*
84+
FROM #commands)
85+
SELECT CASE
86+
WHEN Row_Num = 1
7387

74-
-- Add the USE statement before the first row for the database
75-
THEN REPLICATE(N'-', 50) + NCHAR(10) + NCHAR(13) + N'USE [' + Database_Name + '];' + NCHAR(10) + NCHAR(13)
76-
ELSE ''
77-
END + cmd
78-
FROM Ordered_Cmd
79-
ORDER BY Database_Name ,
80-
Table_Name ,
81-
Stats_Name;
88+
-- Add the USE statement before the first row for the database
89+
THEN
90+
REPLICATE(N'-', 50) + NCHAR(10) + NCHAR(13) + N'USE [' + Database_Name + '];' + NCHAR(10) + NCHAR(13)
91+
ELSE
92+
''
93+
END + cmd
94+
FROM Ordered_Cmd
95+
ORDER BY Database_Name,
96+
Table_Name,
97+
Stats_Name;
8298

8399
-- CLEANUP
84100
CLOSE Databases;

Statistics/Find auto-created statistics objects that overlap with index statistics.sql

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
-- Find auto-created statistics objects that overlap with index statistics
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script generates DROP STATISTICS statements for all auto-generaed statistics on all databases that were not auto-generated
4+
-- (eg. were generated as a result of a created index).
5+
-- It only makes sense to drop this statistic if it's the leading column of the index since that's the only column that has statistics
6+
-- generated on it, and this script doesn't take that into account so it needs to be manually checked.
7+
18
EXEC dbo.sp_ineachdb @command = '
29
WITH autostats ( object_id, stats_id, name, column_id )
310
AS ( SELECT sys.stats.object_id ,

Statistics/Generate DROP STATISTICS statements for all user-created statistics.sql

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
-- Generate DROP STATISTICS statements for all user-created statistics
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script returns information on each user-created statistics object, along with the DROP STATISTICS object to run if appropriate.
4+
15
CREATE TABLE tempdb.dbo.UserCreatedStats
26
(
37
[Database] sysname NOT NULL,
@@ -7,7 +11,13 @@ CREATE TABLE tempdb.dbo.UserCreatedStats
711
DropStatement VARCHAR(255) NOT NULL
812
);
913

10-
EXEC dbo.sp_ineachdb @command = 'INSERT INTO tempdb.dbo.UserCreatedStats SELECT ''?'' AS [Database], schema_name(T.schema_id) AS SchemaName, object_name(S.object_id) AS TableName, S.name AS StatisticsName, ''USE '' + ''?'' + ''; DROP STATISTICS ['' + schema_name(T.schema_id) + ''].['' + object_name(S.object_id) + ''].['' + S.name + '']'' AS DropStatement FROM sys.stats AS S INNER JOIN sys.tables AS T ON T.object_id = S.object_id WHERE user_created = 1',
14+
EXEC dbo.sp_ineachdb @command = '
15+
INSERT INTO tempdb.dbo.UserCreatedStats
16+
SELECT DB_NAME() AS [Database], schema_name(T.schema_id) AS SchemaName, object_name(S.object_id) AS TableName, S.name AS StatisticsName,
17+
''USE ['' + DB_NAME() + '']; DROP STATISTICS ['' + schema_name(T.schema_id) + ''].['' + object_name(S.object_id) + ''].['' + S.name + '']'' AS DropStatement
18+
FROM sys.stats AS S
19+
INNER JOIN sys.tables AS T ON T.object_id = S.object_id
20+
WHERE S.user_created = 1',
1121
@user_only = 1;
1222

1323
SELECT [Database],

TempDB/Find tempdb data files with differing sizes.sql

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
-- Find tempdb data files with differing sizes
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script checks whether the sizes of all data files in the tempdb database, and produces an appropriate message accordingly.
4+
-- Note that if the script detects differing file sizes and you execute the generated ALTER DATABASE statement to make them the same,
5+
-- it's necessary to run the script below a second time to get it to generate another ALTER DATABASE statement with the files having the
6+
-- same sizes, in order for these new file sizes to still apply after the next instance restart.
7+
18
DECLARE @sql VARCHAR(MAX) = '', @maxsize BIGINT, @name VARCHAR(100)
29

310
SELECT files.name, stats.size_on_disk_bytes
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
-- Find tempdbs with uneven initial size or growth
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script generates an explanatory sentence if there are multiple size, growth or is_percent_growth values on tempdb data files.
4+
15
SELECT 'The tempdb database has multiple data files in one filegroup, but they are not all set up with the same initial size or to grow in identical amounts. This can lead to uneven file activity inside the filegroup.'
2-
FROM tempdb.sys.database_files
3-
WHERE type_desc = 'ROWS'
4-
GROUP BY data_space_id
5-
HAVING COUNT(DISTINCT size) > 1 OR COUNT(DISTINCT growth) > 1 OR COUNT(DISTINCT is_percent_growth) > 1
6+
FROM tempdb.sys.database_files
7+
WHERE type_desc = 'ROWS'
8+
GROUP BY data_space_id
9+
HAVING COUNT(DISTINCT size) > 1
10+
OR COUNT(DISTINCT growth) > 1
11+
OR COUNT(DISTINCT is_percent_growth) > 1;

TempDB/Who owns that #temp table.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- Who owns that #temp table
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script queries the Default Trace to determine which login created # tables in TempDB.
14
-- From http://sqlperformance.com/2014/05/t-sql-queries/dude-who-owns-that-temp-table
25

36
DECLARE @filename VARCHAR(MAX);
@@ -22,6 +25,4 @@ FROM sys.fn_trace_gettable(@filename, DEFAULT) AS gt
2225
WHERE gt.DatabaseID = 2
2326
AND gt.EventClass = 46 -- (Object:Created Event from sys.trace_events)
2427
AND gt.EventSubClass = 1 -- Commit
25-
AND o.name LIKE N'#%'
26-
AND o.create_date >= DATEADD(MILLISECOND, -100, gt.StartTime)
27-
AND o.create_date <= DATEADD(MILLISECOND, 100, gt.StartTime);
28+
AND o.name LIKE N'#%';

VLFs/Detect too many VLFs.sql

+77-58
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,83 @@
1+
-- Detect too many VLFs
2+
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
3+
-- This script lists all databases that have more than 1000 VLFs in their transaction log.
14
-- From: http://adventuresinsql.com/2009/12/a-busyaccidental-dbas-guide-to-managing-vlfs/
25

3-
DECLARE @query VARCHAR(1000) ,
4-
@dbname VARCHAR(1000) ,
5-
@count INT
6+
DECLARE @query VARCHAR(1000),
7+
@dbname VARCHAR(1000),
8+
@count INT;
69

7-
SET NOCOUNT ON
10+
SET NOCOUNT ON;
811

9-
DECLARE csr CURSOR FAST_FORWARD READ_ONLY
10-
FOR
11-
SELECT name
12-
FROM sys.databases
13-
WHERE state_desc = 'ONLINE'
12+
DECLARE csr CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
13+
SELECT name
14+
FROM sys.databases
15+
WHERE state_desc = 'ONLINE';
1416

1517
CREATE TABLE ##loginfo
16-
(
17-
dbname VARCHAR(100) ,
18-
num_of_rows INT
19-
)
20-
21-
OPEN csr
22-
23-
FETCH NEXT FROM csr INTO @dbname
24-
25-
WHILE ( @@fetch_status <> -1 )
26-
BEGIN
27-
28-
CREATE TABLE #log_info
29-
(
30-
RecoveryUnitId TINYINT ,
31-
fileid TINYINT ,
32-
file_size BIGINT ,
33-
start_offset BIGINT ,
34-
FSeqNo INT ,
35-
[status] TINYINT ,
36-
parity TINYINT ,
37-
create_lsn NUMERIC(25, 0)
38-
)
39-
40-
SET @query = 'DBCC loginfo (' + '''' + @dbname + ''') '
41-
42-
INSERT INTO #log_info
43-
EXEC ( @query )
44-
45-
SET @count = @@rowcount
46-
47-
DROP TABLE #log_info
48-
49-
INSERT ##loginfo
50-
VALUES ( @dbname, @count )
51-
52-
FETCH NEXT FROM csr INTO @dbname
53-
54-
END
55-
56-
CLOSE csr
57-
DEALLOCATE csr
58-
59-
SELECT dbname, num_of_rows
60-
FROM ##loginfo
61-
WHERE num_of_rows >= 1000
62-
ORDER BY num_of_rows DESC
63-
64-
DROP TABLE ##loginfo
18+
(
19+
dbname VARCHAR(100) NOT NULL,
20+
num_of_rows INT NOT NULL
21+
);
22+
23+
OPEN csr;
24+
25+
FETCH NEXT FROM csr
26+
INTO @dbname;
27+
28+
WHILE (@@fetch_status <> -1)
29+
BEGIN
30+
31+
CREATE TABLE #log_info
32+
(
33+
RecoveryUnitId TINYINT NOT NULL,
34+
fileid TINYINT NOT NULL,
35+
file_size BIGINT NOT NULL,
36+
start_offset BIGINT NOT NULL,
37+
FSeqNo INT NOT NULL,
38+
[status] TINYINT NOT NULL,
39+
parity TINYINT NOT NULL,
40+
create_lsn NUMERIC(25, 0) NOT NULL
41+
);
42+
43+
SET @query = 'DBCC loginfo (' + '''' + @dbname + ''') ';
44+
45+
INSERT INTO #log_info
46+
(
47+
RecoveryUnitId,
48+
fileid,
49+
file_size,
50+
start_offset,
51+
FSeqNo,
52+
status,
53+
parity,
54+
create_lsn
55+
)
56+
EXEC (@query);
57+
58+
SET @count = @@rowcount;
59+
60+
DROP TABLE #log_info;
61+
62+
INSERT ##loginfo
63+
(
64+
dbname,
65+
num_of_rows
66+
)
67+
VALUES
68+
(@dbname, @count);
69+
70+
FETCH NEXT FROM csr
71+
INTO @dbname;
72+
END;
73+
74+
CLOSE csr;
75+
DEALLOCATE csr;
76+
77+
SELECT dbname,
78+
num_of_rows
79+
FROM ##loginfo
80+
WHERE num_of_rows >= 1000
81+
ORDER BY num_of_rows DESC;
82+
83+
DROP TABLE ##loginfo;

0 commit comments

Comments
 (0)