Supported method to detect implicit conversions and missing indexes without executing T-SQL

Lochan Gabda (lgabda) 0 Reputation points
2026-11-24T08:27:14.3066667+00:00

For a given T-SQL statement, we need to programmatically detect, without executing the statement:

  1. Performance-affecting implicit conversions.
  2. Missing-index recommendations.

We are evaluating the estimated execution plan obtained through SET SHOWPLAN_XML ON and inspecting Showplan XML for:

  • PlanAffectingConvert / CONVERT_IMPLICIT
  • MissingIndexes / MissingIndexGroup

Questions:

  • Is this a recommended approach?
  • Is there a more reliable Microsoft-supported API or mechanism for obtaining this information?
  • Are there differences or limitations compared with information available in an actual execution plan?
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

0 comments No comments

1 answer

Sort by: Most helpful
  1. AI answer

    2026-11-24T08:28:39.2533333+00:00

    SET SHOWPLAN_XML ON and programmatic inspection of the returned Showplan XML is the supported method for analyzing a specific statement without executing it; no separate API in the documented SQL Server interfaces provides more authoritative per-statement conversion warnings or missing-index recommendations.

    Run the statement in a dedicated session using separate batches:

    SET SHOWPLAN_XML ON;
    GO
    
    SELECT ...
    FROM dbo.TableName
    WHERE IndexedColumn = @Parameter;
    GO
    
    SET SHOWPLAN_XML OFF;
    GO
    

    When enabled, SHOWPLAN_XML compiles and optimizes subsequent statements but does not execute them. It returns one XML document per batch as nvarchar(max), specifically so applications can process the plan output.^1^

    Operational requirements:

    • SET SHOWPLAN_XML ON and OFF must each be the only statement in their batch.
    • Use a dedicated connection because every subsequent statement is handled as Showplan until it is turned off.
    • The caller needs permission to execute the analyzed statements and SHOWPLAN permission in every database containing referenced objects.^1^
    • Statements that depend on preceding DDL in the same analysis sequence can fail because the DDL is not executed.

    Parse the XML using its namespace rather than relying on text searches:

    ;WITH XMLNAMESPACES
    (
        DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan'
    )
    SELECT
        HasPlanAffectingConvert =
            @plan.exist('//PlanAffectingConvert'),
        HasImplicitConvert =
            @plan.exist('//ScalarOperator[contains(@ScalarString,
                                                   "CONVERT_IMPLICIT")]'),
        HasMissingIndexes =
            @plan.exist('//MissingIndexes/MissingIndexGroup');
    

    Prefer structural elements and attributes over parsing display strings where possible. Microsoft publishes the Showplan XSD with SQL Server installations and through the Microsoft SQL Server XML Schemas location, making Showplan XML the relevant supported interchange format.^1^ However, the supplied documentation does not establish PlanAffectingConvert as a separate, version-independent API contract. Test the parser against every supported SQL Server version and tolerate absent or additional elements.

    Interpreting conversions

    Treat these two findings differently:

    • CONVERT_IMPLICIT identifies an implicit conversion in a scalar expression. Its presence does not by itself prove a material performance problem.
    • PlanAffectingConvert is the optimizer warning indicating that a conversion may affect plan quality.
    • The most important pattern is conversion of the indexed column rather than the parameter or literal. Converting the indexed column can prevent efficient index-seek use, although optimizer exceptions exist.^2^

    An estimated plan has no runtime information or runtime warnings. Microsoft’s troubleshooting guidance specifically recommends looking for runtime CONVERT_IMPLICIT warnings in actual plans when investigating non-SARGable predicates.^3^ Therefore:

    • A warning found in an estimated plan is useful compile-time evidence.
    • Absence of that warning cannot rule out a conversion whose impact becomes evident only during execution.
    • Parameter values, parameterization, statistics, database compatibility settings, and session settings used during compilation can affect the selected plan.

    Interpreting missing-index requests

    Inspecting MissingIndexes, MissingIndexGroup, and their child elements is explicitly documented. XML is also the way to obtain every missing-index request in one plan; the SSMS graphical interface can display only one even when the XML contains several.^4^

    These are optimizer estimates, not prescriptions:

    • They are produced during optimization of one query, before execution.
    • They are not tested or revised after execution.
    • Only nonclustered disk-based rowstore indexes are suggested.
    • Unique and filtered indexes are not suggested.
    • Key-column order is not specified.
    • Similar or overlapping suggestions can be generated.
    • Suggestions are not produced for trivial plans.
    • Estimates involving only inequality predicates are less accurate.^4^

    Consequently, an actual plan does not make the embedded missing-index recommendation inherently more accurate: that recommendation remains a compile-time result. Before generating DDL, compare it with existing indexes and consolidate overlapping recommendations.

    Alternative mechanisms

    • sys.dm_exec_query_plan returns the same plan information as SET SHOWPLAN_XML, using the SQL Server xml type.^1^ It is useful for plans that already exist in cache, not as a replacement for compiling an arbitrary statement without running it.
    • Missing-index DMVs provide database-wide accumulated recommendations, but they are updated when statements are optimized, are not persisted across engine restarts, and are limited to 600 rows.^5^
    • Query Store and cached-plan DMVs are appropriate for previously processed workloads, rather than isolated, never-executed input statements.^4^

    An actual plan requires execution and adds actual row counts, resource metrics, elapsed and CPU time, and runtime warnings. The estimated plan contains only the compiled plan and estimates.^6^


    References

    1. SET SHOWPLAN_XML (Transact-SQL)
    2. When indexes are not available in SQLServer - Microsoft Q&A
    3. Post-migration validation and optimization guide
    4. Tune nonclustered indexes with missing index suggestions
    5. sys.dm_db_missing_index_details (Transact-SQL)
    6. Display and save execution plans
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.