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

2 answers

Sort by: Oldest
  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?

  2. Erland Sommarskog 137.9K Reputation points MVP Volunteer Moderator
    2026-11-24T14:56:20.14+00:00

    This approach will give you some information - but not all. Consider for instance:

    CREATE TABLE tbl (str varchar(128) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL PRIMARY KEY)
    INSERT tbl (str)
      SELECT name FROM sys.objects
    go
    SELECT * FROM tbl WHERE str = N'objects'
    go
    DROP TABLE tbl
    
    
    

    Here you will get an implicit conversion from varchar to nvarchar, and because the collation is an SQL collation, this means that an seek of the index is not possible and you get a scan instead. But there is no warning for this in the plan. However, that may be because the table is small. When I change the way I fill the query to

    INSERT tbl (str)
      SELECT convert(varchar(36), newid()) 
      FROM   generate_series(1, 1000000)
    

    I do get a warning.

    Overall, I have not studied this warning for implicit conversion very carefully, but I know that I have been looking at plans with this warning and thinking "eh, what ghosts is it seeing?`"

    When it comes to missing indexes, note that this is not an omnipresent all-knowing feature, but it main make suggestions for indexes covering the query. Sometimes these recommendations certainly makes sense, but followed slavically, you will end up with a number of redundant and overlapping indexes - and you may still need indexes that could help to speed up your performance. As an example, consider these two queries:

    SELECT a, b, c FROM tbl WHERE indexedcol = @value
    SELECT a, b, f FROM tbl WHERE indexedcol = @value
    

    The missing-indexes feature will suggest two indexes: (indexedcol) INCLUDE(a, b, c) and indexedcol INCLUDE(a, b, f). A better approach might to have one index (indexedcol) INCLUDE (a, b, c, f). Or simply have an index on indexcol alone and accept the key lookups. One problem is that next week there is a new requirement, so that column g should be returned, and in that case the index with all INCLUDE-columns is no longer covering, only bloated.

    Despite these disclaimers, I would say that your approach is not an unreasonable one. If you can automate this and parse the XML you get out of it, you can make some findings, but you need to manual post-processing and evaluate what you should implement and what you should ignore.

    I have not made any closer investigation, but quick testing does not reveal any difference in what information that is returned in these regards.

    And as for better ways to find this information - no, there is nothing equally directly accessible you can use.

    Was this answer helpful?

    0 comments No comments

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.