forked from MichelleUfford/sql-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdba_missingIndexStoredProc_sp.sql
More file actions
248 lines (198 loc) · 9.01 KB
/
dba_missingIndexStoredProc_sp.sql
File metadata and controls
248 lines (198 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
Use dbaTools;
Go
/* Create a stored procedure skeleton */
If ObjectProperty(Object_ID('dbo.dba_missingIndexStoredProc_sp'), N'IsProcedure') Is Null
Begin
Execute ('Create Procedure dbo.dba_missingIndexStoredProc_sp As Print ''Hello World!''')
RaisError('Procedure dba_missingIndexStoredProc_sp created.', 10, 1);
End;
Go
/* Drop our table if it already exists */
If Exists(Select Object_ID From sys.tables Where [name] = N'dba_missingIndexStoredProc')
Begin
Drop Table dbo.dba_missingIndexStoredProc
Print 'dba_missingIndexStoredProc table dropped!';
End
/* Create our table */
Create Table dbo.dba_missingIndexStoredProc
(
missingIndexSP_id int Identity(1,1) Not Null
, databaseName varchar(128) Not Null
, databaseID int Not Null
, objectName varchar(128) Not Null
, objectID int Not Null
, query_plan xml Not Null
, executionDate smalldatetime Not Null
, statementExecutions int Not Null
Constraint PK_missingIndexStoredProc
Primary Key Clustered(missingIndexSP_id)
);
Print 'dba_missingIndexStoredProc Table Created';
/* Configure our settings */
Set ANSI_Nulls On;
Set Quoted_Identifier On;
Go
Alter Procedure dbo.dba_missingIndexStoredProc_sp
/* Declare Parameters */
@lastExecuted_inDays int = 7
, @minExecutionCount int = 1
, @logResults bit = 1
, @displayResults bit = 0
As
/**********************************************************************************************************
NAME: dba_missingIndexStoredProc_sp
SYNOPSIS: Retrieves stored procedures with missing indexes in their cached query plans.
@lastExecuted_inDays = number of days old the cached query plan
can be to still appear in the results;
the HIGHER the number, the longer the
execution time.
@minExecutionCount = minimum number of executions the cached
query plan can have to still appear
in the results; the LOWER the number,
the longer the execution time.
@logResults = store results in dba_missingIndexStoredProc
@displayResults = return results to the caller
DEPENDENCIES: The following dependencies are required to execute this script:
- SQL Server 2005 or newer
NOTES: This is not 100% guaranteed to catch all missing indexes in
a stored procedure. It will only catch it if the stored proc's
query plan is still in cache. Run regularly to help minimize
the chance of missing a proc.
AUTHOR: Michelle Ufford, http://sqlfool.com
CREATED: 2009-09-03
VERSION: 1.0
LICENSE: Apache License v2
USAGE: Exec dbo.dba_missingIndexStoredProc_sp
@lastExecuted_inDays = 30
, @minExecutionCount = 5
, @logResults = 1
, @displayResults = 1;
----------------------------------------------------------------------------
DISCLAIMER:
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 or merchantability and/or fitness for a particular purpose.
----------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- DATE VERSION AUTHOR DESCRIPTION --
---------------------------------------------------------------------------------------------------------
20150619 1.0 Michelle Ufford Open Sourced on GitHub
**********************************************************************************************************/
Set NoCount On;
Set XACT_Abort On;
Set Ansi_Padding On;
Set Ansi_Warnings On;
Set ArithAbort On;
Set Concat_Null_Yields_Null On;
Set Numeric_RoundAbort Off;
Begin
/* Declare Variables */
Declare @currentDateTime smalldatetime;
Set @currentDateTime = GetDate();
Declare @plan_handles Table
(
plan_handle varbinary(64) Not Null
, statementExecutions int Not Null
);
Create Table #missingIndexes
(
databaseID int Not Null
, objectID int Not Null
, query_plan xml Not Null
, statementExecutions int Not Null
);
Create Clustered Index CIX_temp_missingIndexes
On #missingIndexes(databaseID, objectID);
Begin Try
/* Perform some data validation */
If @logResults = 0 And @displayResults = 0
Begin
/* Log the fact that there were open transactions */
Execute dbo.dba_logError_sp
@errorType = 'app'
, @app_errorProcedure = 'dba_missingIndexStoredProc_sp'
, @app_errorMessage = '@logResults = 0 and @displayResults = 0; no action taken, exiting stored proc.'
, @forceExit = 1
, @returnError = 1;
End;
Begin Transaction;
/* Retrieve distinct plan handles to minimize dm_exec_query_plan lookups */
Insert Into @plan_handles
Select plan_handle, Sum(execution_count) As 'executions'
From sys.dm_exec_query_stats
Where last_execution_time > DateAdd(day, -@lastExecuted_inDays, @currentDateTime)
Group By plan_handle
Having Sum(execution_count) > @minExecutionCount;
With xmlNameSpaces (
Default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan'
)
/* Retrieve our query plan's XML if there's a missing index */
Insert Into #missingIndexes
Select deqp.[dbid]
, deqp.objectid
, deqp.query_plan
, ph.statementExecutions
From @plan_handles As ph
Cross Apply sys.dm_exec_query_plan(ph.plan_handle) As deqp
Where deqp.query_plan.exist('//MissingIndex/@Database') = 1
And deqp.objectid Is Not Null;
/* Do we want to store the results of our process? */
If @logResults = 1
Begin
Insert Into dbo.dba_missingIndexStoredProc
Execute sp_msForEachDB 'Use ?;
Select ''?''
, mi.databaseID
, Object_Name(o.object_id)
, o.object_id
, mi.query_plan
, GetDate()
, mi.statementExecutions
From sys.objects As o
Join #missingIndexes As mi
On o.object_id = mi.objectID
Where databaseID = DB_ID();';
End
/* We're not logging it, so let's display it */
Else
Begin
Execute sp_msForEachDB 'Use ?;
Select ''?''
, mi.databaseID
, Object_Name(o.object_id)
, o.object_id
, mi.query_plan
, GetDate()
, mi.statementExecutions
From sys.objects As o
Join #missingIndexes As mi
On o.object_id = mi.objectID
Where databaseID = DB_ID();';
End;
/* See above; this part will only work if we've
logged our data. */
If @displayResults = 1 And @logResults = 1
Begin
Select *
From dbo.dba_missingIndexStoredProc
Where executionDate >= @currentDateTime;
End;
/* If you have an open transaction, commit it */
If @@TranCount > 0
Commit Transaction;
End Try
Begin Catch
/* Whoops, there was an error... rollback! */
If @@TranCount > 0
Rollback Transaction;
/* Return an error message and log it */
Execute dbo.dba_logError_sp;
End Catch;
/* Clean-Up! */
Drop Table #missingIndexes;
Set NoCount Off;
Return 0;
End
Go
Set Quoted_Identifier Off;
Go