Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15889

Correlate query to missing index

$
0
0

Hi all,

The following query is returning unexpected results on my server:

SELECT mig.*, 
statement AS table_name,
mid.equality_columns,
mid.inequality_columns,
mid.included_columns,
user_seeks * avg_total_user_cost * (avg_user_impact * 0.01) AS [index_rating], 
s.user_scans,
s.user_seeks,
s.avg_total_user_cost,
s.avg_user_impact
FROM sys.dm_db_missing_index_details AS mid WITH (NOLOCK)
INNER JOIN sys.dm_db_missing_index_groups AS mig WITH (NOLOCK)  ON mig.index_handle = mid.index_handle 
INNER JOIN sys.dm_db_missing_index_group_stats as s WITH (NOLOCK)  ON S.group_handle = mig.index_group_handle
ORDER BY 
index_rating desc

The number 1 index recommendation returned is on single "inequality column" - on a column where I really didn't expect to see it. I have asked our developers to search the code for this column in an inequality predicate - it was not found.

I have ran this code in attempt to find the query that is causing this:

WITH CTE AS (
SELECT SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1 ,
 ((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(qt.TEXT)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2)+1) as qtext,
qs.execution_count,
qs.total_logical_reads, qs.last_logical_reads,
qs.total_logical_writes, qs.last_logical_writes,
qs.total_worker_time,
qs.last_worker_time,
qs.total_elapsed_time/1000000 total_elapsed_time_in_S,
qs.last_elapsed_time/1000000 last_elapsed_time_in_S,
qs.last_execution_time,
qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
where query_plan is not null
)
SELECT 
* 
FROM 
CTE 
WHERE qtext like '%MY_COLUMN%'

The query returns no result (other than itself).

As far as I can tell there is no way to get to sys.dm_exec_sql_text from sys.dm_db_missing_index_details.

I'm obviously misunderstanding something somewhere...

Any advice much appreciated.

Thanks for reading,

Clay


Viewing all articles
Browse latest Browse all 15889

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>