Optimize Queries with Suggested Indexes

SQL Server continuously monitors query execution, and when it detects that a query could benefit from an index, it generates a Missing Index, although we like to refer to them as Suggested Indexes since they may not always be useful and could potentially hurt overall performance.


Why Suggested Indexes Matter

SQL Server's suggested indexes are designed to minimize costly table scans and reduce resource usage by improving query performance. These indexes target specific query patterns, helping to optimize data retrieval and make queries more efficient.


How to Manually Find Suggested Indexes in SQL Server

SQL Server tracks index recommendations through its dynamic management views (DMVs), which help identify missing indexes that could enhance query performance. The sys.dm_db_missing_index_details DMV is particularly useful for finding index suggestions generated during query execution.

Here’s an example query to find suggested indexes manually:

SELECT 
    migs.avg_total_user_cost AS [Total Cost],
    migs.avg_user_impact AS [User Impact %],
    migs.user_seeks AS [Seeks],
    migs.user_scans AS [Scans],
    mid.statement AS [Table Name],
    mid.equality_columns AS [Equality Columns],
    mid.inequality_columns AS [Inequality Columns],
    mid.included_columns AS [Included Columns]
FROM 
    sys.dm_db_missing_index_group_stats AS migs
    INNER JOIN sys.dm_db_missing_index_groups AS mig
        ON migs.group_handle = mig.index_group_handle
    INNER JOIN sys.dm_db_missing_index_details AS mid
        ON mig.index_handle = mid.index_handle
ORDER BY 
    migs.avg_user_impact DESC;

Explanation of the Query

  • Equality Columns: Columns that SQL Server suggests indexing because they are often used in WHERE clauses with equality comparisons (e.g., =).
  • Inequality Columns: Columns used with comparison operators such as >, <, or LIKE, which can benefit from an index.
  • Included Columns: Non-key columns that SQL Server recommends including in the index to cover specific queries, improving query performance by avoiding the need to access the table itself.
  • User Impact %: The percentage by which SQL Server estimates that the suggested index could improve query performance.
  • Total Cost: The estimated cost savings associated with creating the suggested index.
  • Seeks and Scans: The number of seeks or scans SQL Server has performed that could benefit from the missing index.

Interpreting the Results

When evaluating the output of the suggested index query:

  1. Evaluate the Columns: Check the equality, inequality, and included columns to ensure they are relevant to your workload.
  2. Consider User Impact: A higher user impact percentage indicates that the index could significantly improve query performance.
  3. Check for Redundancy: Before creating the suggested index, verify whether an existing index already covers the recommended columns. If so, you might only need to adjust or merge the indexes.

⚠️ Note: These suggestions are based on recent query patterns. If your SQL Server instance is frequently restarted, suggestions may not reflect typical usage patterns, so ensure the results are based on representative data.


Suggested Actions

When reviewing SQL Server's suggested indexes:

  • Evaluate the columns: Ensure the suggested index targets relevant queries and workloads.
  • Check for redundancy: Ensure that an existing index doesn't already cover the suggested index.
  • Balance read-write performance: On write-heavy tables, too many indexes can degrade performance. Carefully evaluate the benefits of adding new indexes.

Aireforge provides refined suggestions, ensuring that any new index does not conflict with existing ones, while minimizing unnecessary overhead.


How Aireforge Handles Suggested Indexes

Aireforge automates the identification of suggested indexes by querying SQL Server’s DMVs for missing index details and refining these suggestions based on your existing index structures and usage patterns. Aireforge ensures that any suggested indexes don’t overlap with existing ones and that the potential performance improvements are carefully evaluated.

Here's how Aireforge handles suggested indexes:

  1. Index Structure Analysis: Aireforge uses a tailored query to extract suggested indexes specific to each table from SQL Server’s dynamic management views (sys.dm_db_missing_index_*). This query gathers information about equality columns, inequality columns, included columns, and the estimated impact of creating the index.

  2. User Impact Evaluation: Aireforge ranks suggested indexes based on the User Impact %, which measures the potential performance improvements that creating the index could bring. This is calculated using SQL Server’s estimates for how the index would affect query execution.

  3. Redundancy Check: Aireforge checks if any suggested index overlaps with an existing index on the table. If a redundant index is found, Aireforge may recommend merging the indexes or removing redundant ones to streamline performance.

  4. Merge and Drop Suggestions: Where applicable, Aireforge suggests merging existing indexes or dropping redundant ones, ensuring optimal index performance without unnecessary maintenance overhead.

  5. SQL Script Generation: Aireforge generates a SQL script to create the recommended index or merge existing indexes, helping you quickly implement the changes while optimizing your database.


Conclusion

SQL Server’s suggested indexes provide a valuable way to optimize query performance. By regularly reviewing and applying relevant index suggestions, you can significantly enhance your database's efficiency. Aireforge simplifies this process by refining SQL Server’s recommendations, ensuring that your indexes are optimized for performance without creating redundancy or unnecessary overhead.