concept miss index in category sql

appears as: missing index, missing indexes, missing indexes, A missing index, missing index, The missing indexes
SQL Server MVP Deep Dives Vol. 2

This is an excerpt from Manning's book SQL Server MVP Deep Dives Vol. 2.

  • Include columns— Columns in the missing index that should be included to make the index a covering index
  • Investigating missing indexes

    Missing indexes are the best place to start investigating the plan cache. Finding the right indexes to add to a table can often be a challenge. When you discover the right ones, they often lead to noticeable performance improvements.

    An often recommended process for adding missing indexes is to use the sys.dm_db_missing_index_* DMOs. These DMOs provide information on indexes that could be added to improve the performance on some queries in the database. Unfortunately, with the missing index DMOs, no clear relationship exists between the indexes and the queries that are being executed on the SQL Server. When adding indexes to a database, you must be able to identify which queries are improved; these DMOs don’t help with that. The plan cache provides a solution to this issue. When missing index information is collected for the DMOs, the same process includes the information in the plan that’s compiled and placed in the plan cache. Through queries run against the plan cache, you can discover missing index information and quantify the value of the improvement based on the number of times the plan was used.

    In the query shown in listing 1, all query plans with the MissingIndex element are being returned. The MissingIndex element is then shredded to provide information on missing indexes.

    Listing 1. Querying the plan cache for missing indexes
    WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan') ,PlanMissingIndexes AS ( SELECT query_plan, cp.usecounts FROM sys.dm_exec_cached_plans cp OUTER APPLY sys.dm_exec_query_plan(cp.plan_handle) tp WHERE tp.query_plan.exist('//MissingIndex')=1 ) SELECT stmt.value('(//MissingIndex/@Database)[1]', 'sysname') AS database_name ,stmt.value('(//MissingIndex/@Schema)[1]', 'sysname') AS [schema_name] ,stmt.value('(//MissingIndex/@Table)[1]', 'sysname') AS [table_name] ,stmt.value('(@StatementText)[1]', 'VARCHAR(4000)') AS sql_text ,pmi.usecounts ,stmt.value('(//MissingIndexGroup/@Impact)[1]', 'FLOAT') AS impact ,stmt.query('for $group in //ColumnGroup for $column in $group/Column where $group/@Usage="EQUALITY" return string($column/@Name) ').value('.', 'varchar(max)') AS equality_columns ,stmt.query('for $group in //ColumnGroup for $column in $group/Column where $group/@Usage="INEQUALITY" return string($column/@Name) ').value('.', 'varchar(max)') AS inequality_columns ,stmt.query('for $group in //ColumnGroup for $column in $group/Column where $group/@Usage="INCLUDE" return string($column/@Name) ').value('.', 'varchar(max)') AS include_columns ,pmi.query_plan FROM PlanMissingIndexes pmi CROSS APPLY pmi.query_plan.nodes('//StmtSimple') AS p(stmt) ORDER BY stmt.value('(//MissingIndexGroup/@Impact)[1]', 'FLOAT') DESC

    Executing this query provides information about the database and table that the missing index would benefit. It also includes the impact, equality columns, inequality columns, and the included columns with sys.dm_db_missing_index_* DMOs. The impact value is an estimation of the percentage improvement that the query should expect in the query cost. The equality columns are those that the query would use for exact matches to values in the index. The inequality columns would be nonexact matches, such as greater-than or less-than comparisons. Figure 1 shows sample output from the missing indexes query.

    SQL Server DMVs in Action: Better Queries with Dynamic Management Views

    This is an excerpt from Manning's book SQL Server DMVs in Action: Better Queries with Dynamic Management Views.

    Figure 1.3. Output from the missing indexes SQL

    The DMV sys.dm_db_missing_index_group_stats contains metrics for missing indexes, including how it would have been used (seek or scan), if it would have been used by an application or system (for example, DBCC), and various measures of cost saving by using this missing index. The DMV sys.dm_db_missing_index_details contains textual details of the missing index (what database/schema/table it applies to, what columns the index would include). These two DMVs (metrics and names) are linked together via another DMV, sys.dm_db_missing_index_groups, which returns information about missing indexes in a specific missing index group.

    You should note how the Total Cost field of the missing index is calculated . Total Cost should reflect the number of times the index would have been accessed (as a seek or scan), together with the impact of the index on its queries. The results are sorted by the calculated Total Cost .

    Hopefully, you can use the approach and patterns discussed, for example, to implement missing indexes, as a template to implement other features as you deem necessary.

    10.6.1. Implementing missing indexes

    The script we use to automatically implement missing indexes is shown in the following listing. This script is an extension of the one given in chapter 3, section 3.2, which identified the most-important missing indexes.

    SQL Server MVP Deep Dives

    This is an excerpt from Manning's book SQL Server MVP Deep Dives.

    Listing 9. Finding missing indexes
    USE AB_Utility; GO IF OBJECT_ID(N'dbo.AB_GetMissingIndexes', N'IF') IS NOT NULL DROP FUNCTION dbo.AB_GetMissingIndexes GO CREATE FUNCTION dbo.AB_GetMissingIndexes ( @database_name SYSNAME, @number_of_rows INT ) RETURNS TABLE AS RETURN ( SELECT TOP (@number_of_rows) *, -- must give credit to Tibor Karazsi here: [statement] = 'CREATE INDEX [<<index name>>]' + ' ON ' + [table] + ' (' + COALESCE(eq + COALESCE(', ' + iq, ''), iq) + ')' + COALESCE(' INCLUDE(' + ic + ');', ';') FROM ( SELECT [table] = AB_Utility.dbo.AB_GetTwoPartName( d.[object_id], d.database_id), eq = d.equality_columns, iq = d.inequality_columns, ic = d.included_columns, relative_benefit = (s.user_seeks + s.user_scans) * (s.avg_total_user_cost * s.avg_user_impact), s.user_seeks, s.user_scans, s.last_user_seek, s.last_user_scan FROM sys.dm_db_missing_index_details AS d INNER JOIN sys.dm_db_missing_index_groups AS g ON d.index_handle = g.index_handle INNER JOIN sys.dm_db_missing_index_group_stats AS s ON g.index_group_handle = s.group_handle WHERE d.database_id = DB_ID(@database_name) ) x CROSS JOIN AB_Utility.dbo.AB_Uptime() ORDER BY relative_benefit DESC ); GO SELECT * FROM AB_Utility.dbo.AB_GetMissingIndexes ( 'Org00010001', 50 );