Commit 3a1c3fbe authored by dwentzel's avatar dwentzel

missing indexes from query plans works correctly now

parent ddb6f07d
IF OBJECT_ID('PerformanceCollector.MissingIndexesFromQueryPlansDtls') IS NULL
IF OBJECT_ID('PerformanceCollector.MissingIndexesFromQueryPlansDtls') IS NOT NULL
BEGIN
EXEC ('DROP TABLE PerformanceCollector.MissingIndexesFromQueryPlansDtls');
END
GO
Create table PerformanceCollector.MissingIndexesFromQueryPlansDtls
( CurrTime datetime,
TotalCost bigint,
TableName nvarchar(4000),
EqualityColumns varchar(4000),
InequalityColumns varchar(4000),
IncludedColumns varchar(4000)
query_plan XML,
sql_text varchar(max)
)
END
GO
GO
\ No newline at end of file
IF OBJECT_ID('PerformanceCollector.MissingIndexesFromQueryPlansSummary') IS NULL
IF OBJECT_ID('PerformanceCollector.MissingIndexesFromQueryPlansSummary') IS NOT NULL
BEGIN
Create table PerformanceCollector.MissingIndexesFromQueryPlansSummary
EXEC ('DROP TABLE PerformanceCollector.MissingIndexesFromQueryPlansSummary');
END
GO
Create table PerformanceCollector.MissingIndexesFromQueryPlansSummary
( CurrTime datetime,
TotalCost bigint,
TableName nvarchar(4000),
QtyStatements bigint,
MaxImpact decimal(15,10),
database_id bigint,
object_id bigint,
statement nvarchar(4000),
EqualityColumns varchar(4000),
InequalityColumns varchar(4000),
IncludedColumns varchar(4000)
)
END
GO
GO
\ No newline at end of file
......@@ -46,10 +46,10 @@ BEGIN
DECLARE dbs CURSOR FOR
SELECT name
FROM sys.databases
WHERE database_id = db_id()
AND state_desc = 'ONLINE'
WHERE state_desc = 'ONLINE'
AND user_access_desc = 'MULTI_USER'
AND is_in_standby = 0
AND name NOT IN ('master','model','msdb','tempdb')
OPEN dbs
FETCH NEXT FROM dbs INTO @dbname
WHILE @@FETCH_STATUS = 0
......@@ -82,7 +82,9 @@ BEGIN
WHERE d.database_id NOT IN (select database_id from sys.databases WHERE name IN ('master','distribution','msdb'))
ORDER BY [Total Cost] DESC
--Step4:
--Missing Indexes derived from cached query plans
EXEC PerformanceCollector.GatherMissingIndexesFromPlans ;
END;
......
......@@ -46,6 +46,10 @@ BEGIN
DELETE from PerformanceCollector.PageLifeExpectancies WHERE Currtime < @dtRetentionDate
DELETE FROM PerformanceCollector.TempDbLogWatch WHERE CurrTime < @dtRetentionDate;
DELETE FROM PerformanceCollector.TempDbUsage WHERE CurrTime < @dtRetentionDate;
DELETE FROM PerformanceCollector.MissingIndexesFromQueryPlansDtls WHERE CurrTime < @dtRetentionDate;
DELETE FROM PerformanceCollector.MissingIndexesFromQueryPlansSummary WHERE CurrTime < @dtRetentionDate;
UPDATE PerformanceCollector.Config SET ConfigValueText = @now WHERE ConfigSetting = 'LastPurgeRun'
......
......@@ -76,7 +76,7 @@ BEGIN
AND t1.counter_name = t2.counter_name
AND isnull(t1.counter_name,'') = isnull(t2.counter_name,'')
TRUNCATE TABLE DBA_PM_PerfCountersPrev
TRUNCATE TABLE PerformanceCollector.PerfCountersPrev
INSERT INTO PerformanceCollector.PerfCountersPrev
SELECT * FROM #PerfCounters
......
......@@ -33,12 +33,11 @@ BEGIN
user_updates AS [Total Writes],
user_seeks + user_scans + user_lookups AS [Total Reads],
user_updates - (user_seeks + user_scans + user_lookups) AS [Difference]
FROM ' + quotename(@dbname) + '.sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
FROM sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
INNER JOIN ' + quotename(@dbname) + '.sys.indexes AS i WITH (NOLOCK)
ON s.[object_id] = i.[object_id]
AND i.index_id = s.index_id
WHERE OBJECTPROPERTY(s.[object_id],''IsUserTable'') = 1
AND s.database_id = DB_ID(''' + @dbname + ''')
WHERE s.database_id = DB_ID(''' + @dbname + ''')
AND user_updates > (user_seeks + user_scans + user_lookups)
AND i.index_id > 1
--AND (user_updates - (user_seeks + user_scans + user_lookups)) > 100 --Difference > 100
......
......@@ -8,9 +8,9 @@ As
BEGIN
set nocount on
/*
WITH XMLNAMESPACES
;WITH XMLNAMESPACES
(DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT query_plan,
......@@ -65,32 +65,38 @@ SET equality_columns = LEFT(equality_columns,LEN(equality_columns)-1),
--DROP TABLE #MissingIndexInfo
--INSERT INTO PerformanceCollector.MissingIndexSummaryFromQueryPlans
INSERT INTO PerformanceCollector.MissingIndexesFromQueryPlansSummary
select
getdate() AS CurrTime,
count(*) as qty_statements,
MAX(impact) AS max_impact,
database_id,
OBJECT_ID,
statement, equality_columns, inequality_columns, include_columns
statement,
equality_columns,
inequality_columns,
include_columns
from #missingindexinfo
group by
database_id, OBJECT_ID, statement, equality_columns, inequality_columns, include_columns
order by 3 desc
--INSERT INTO PerformanceCollector.MissingIndexesFromQueryPlansDtls
INSERT INTO PerformanceCollector.MissingIndexesFromQueryPlansDtls
SELECT
getdate() AS CurrTime,
i.query_plan AS query_plan,
i.sql_text AS sql_text,
*
i.sql_text AS sql_text
FROM #MissingIndexInfo i
*/
--exec tempdb..sp_help #MissingIndexInfo
END
GO
\ No newline at end of file
GO
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment