Downstream query recompile when index is drop and re-created.

  • Hi

    I haven't quite figured out how to prove or disprove this reliably so I'm hoping for a tip.

    What I need to know is, if I drop and recreate a (NonClustered, Non-PK) index on a table, using the same name, will queries/procs that refer to that table recompile when executed? Ie will they "know" that the index is not the same one even if it's named identically (and structured identically)?

    I'm assuming that they will recompile (because potentially the parameters of the index might have changed even if the name is the same, which could be a performance disaster), but I'm not proficient enough with Query Plans Caches, DMVs etc. to quite figure it out for myself 🙁

    Thanks in advance.

  • Test it:

    CREATE TABLE dbo.LetsRecompile

    (

    ID INT IDENTITY(1, 1),

    MyVal VARCHAR(50)

    );

    GO

    CREATE INDEX WhatHappens ON dbo.LetsRecompile (MyVal);

    GO

    INSERT INTO LetsRecompile

    (MyVal)

    VALUES ('Some'),

    ('Thing'),

    ('Has'),

    ('To'),

    ('Be'),

    ('Here'),

    ('For'),

    ('Statistics');

    GO

    SELECT MyVal

    FROM dbo.LetsRecompile

    WHERE MyVal = 'Thing';

    DROP INDEX dbo.LetsRecompile.WhatHappens;

    GO

    CREATE INDEX WhatHappens ON dbo.LetsRecompile (MyVal);

    GO

    SELECT MyVal

    FROM dbo.LetsRecompile

    WHERE MyVal = 'Thing';

    SELECT deqs.execution_count,

    dest.text

    FROM sys.dm_exec_query_stats AS deqs

    OUTER APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

    WHERE dest.text LIKE '%MyVal%';

    Now, this test is a little overly simplistic, and we're getting a parameterized query which could skew the results, but, you'll note that the execution count for the query stays at one.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • SELECT deqs.execution_count,

    dest.text

    FROM sys.dm_exec_query_stats AS deqs

    OUTER APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

    WHERE dest.text LIKE '%MyVal%';

    Now, this test is a little overly simplistic, and we're getting a parameterized query which could skew the results, but, you'll note that the execution count for the query stays at one.

    No, this was perfect, just couldn't ID dm_exec_query_stats as my root (long time, no DBAing!). By further hooking to dm_exec_query_plan I was able to answer my questions and more besides.

    Thank you very much 🙂

  • Happy to help.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply