18 December 2012

Hypothetical Indexes on SQL Server

Sometimes, you have to test out alternative indexing strategies, but the task of creating the candidate indexes would just take too long. Is there another way? Well, yes, why not use the same method the DTA (Database Tuning Advisor) uses, and take the tedium out of the job.

If we want to predict how well a query will perform as a result of adding a new index on the table, we need to wait while the index is created before we can test it. On larger tables, the creation of the index can take a significant amount of time and if you are trying a number of alternative indexing strategies, the wait can become very tedious. Furthermore, it is a common frustration to find that, after waiting for many minutes for the creation of the index, you realize that it is not using the index when you go to look at the query plan.

So wouldn’t it be nice if we could try a hypothetical index just to test if the index really will be useful for the query. That is possible, but not straightforward; The reason that the technique exists is that it is used by the DTA (Database Tuning Advisor) to recommend a missing index. In this article I’ll present you some undocumented commands that are used to do it.

Creating a hypothetical index

There is a special syntax of the CREATE INDEX command that allows us to create a hypothetical index. This is an index that creates the metadata of the index on sysindexes and a statistic associated to the index, but does not create the index itself.

Suppose we have the following query from AdventureWorks2012 database:

If we want to create a hypothetical index on SalesOrderHeader table we could run:

The relational index option STATISTICS_ONLY = -1, which is undocumented, means that the index itself will not be created, but only the statistic associated with the index. This index be neither considered nor used by the query optimizer unless you run a query in AUTOPILOT mode.


There is command called “SET AUTOPILOT ON” used to enable support to hypothetical indexes, and this is used with other DBCC command called “DBCC AUTOPILOT“.

First let’s see them working together and then I’ll give you more details about it:



When running on autopilot mode, SQL Server doesn’t execute the query but it returns an estimated execution plan that considers all indexes enabled by DBCC AUTOPILOT command, including the hypothetical ones.


There are a few things you could do with this command, first let’s find out what the syntax is. We can find out the syntax of all undocumented commands by using the trace flag 2588 and then running DBCC HELP to see:

Making AUTOPILOT easier to use

The parameters that you have to use are not straightforward. This means that, if you are working with a query with lots of tables, it can get boring to write all the DBCC AUTOPILOT commands and this might discourage you from using it. Because of this, I’ve created a procedure to make it a little easier to use.

Originally I created this procedure after answering a student’s question about how to make it easier to use hypothetical indexes on SQL Server. So I thought you may like it.

Unfortunately it relies on a CLR stored procedure to SET the AUTOPILOT, but if you don’t mind to use it in a develop environment (which is something normal to do) then you can use it, following is the CLR code, and if you are interested you can download the project code here:

And following is the code to compile it on SQL Server and to create another procedure to simulate the hypothetical indexes:

The stored procedure st_TestHipotheticalIndexes expects two input parameters:

  • @SQLIndex: Here you should specify the command to create the index that you want to try (the hypothetical indexes), if you want to try more than one index, just call it separating many “create index” commands by a semicolon. For instance:

  • @Query: Here you should write the query you want to try.

Here is a sample of how to call it:

The results of the query above is an XML datatype with the query plan considering the suggested index:


Another sample:


Now it is easier to try out the effect of various indexes. Let me know what do you think and please don’t mind the clumsy code in the procedure to get the tablename, indexname.


There is a lot of mystery about these undocumented features, but I’m sure this will be enough to get you started with doing tests using Hypothetical indexes. I am sure I don’t need to tell you not to use this is in production environment do I? This is undocumented stuff, so nobody can guarantee what it is really doing, and the side-effects unless Microsoft chooses to make it officially public and documented.

That’s all folks.

For more articles like this, sign up to the fortnightly Simple-Talk newsletter.

Tags: , , ,


  • Rate
    [Total: 58    Average: 4.5/5]

Fabiano is fascinated by the SQL Server Query Processor and the way it works to optimize queries, procedures and functions. He graduated as a Technical Processor from Colégio Bezerra de Menezes, SP- Brazil, and has worked for several years with SQL Server, focusing in creating Data Warehouses and optimizing T-SQL codes for many companies in Brazil and Argentina. Fabiano is a SQL Server MVP, MCP for SQL Server 2000, MCTS and MCITP Data Base Developer for SQL Server 2005 and 2008. He also is actively involved in SQL Server community though forums such as MSDN and TechNet Brazil, writes articles for Simple-Talk and SQL Server Magazine Brazil, and he also presents online Webcasts and In-Person events for Microsoft Brazil. His blog is on http://blogfabiano.com

View all articles by Fabiano Amorim

  • Anonymous

    Good Article :). Started New Year with your great article 🙂

  • Anonymous

    Fabiano, great stuff thank you.

  • Mariana

    Fabiano, it´s exciting read something written by you. You make the internal mechanisms of SQL Server look simple.

    Thanks for all your posts that I´ve ever read.

    I can say I´m your fan!!

  • mcflyamorim

    Thanks all for the kind words in your comments, I really appreciate it.

    That is what motivates me on keep writing 🙂

    Fabiano Amorim

  • Anonymous

    Warning Message

    The command to create the hypothetical index works, but SSMS highlights STATISTICS_ONLY as an error with this text:

    Invalid usage of the option STATISTICS_ONLY in the CREATE INDEX statement.

    Any ideas, perhaps since the option is unofficial, undocumented?

  • Anonymous

    Answered my own question
    If you modify the provided command slight to include parentheses around the STATISTICS_ONLY option, the error is removed:


  • Anonymous

    Very interesting
    I always wondered how DTA worked. Interesting article but it does need to be proof read.

  • Jonathan D. Myers

    Is this valid for only 2012, or is it backwards-compatible with 2005 and 2008? If not, is there a related method to do similar work in the legacy versions?


  • Roshan Joseph

    Great article
    Thanks for sharing the tip. Definitely helpful !

  • Anonymous

    nice explanations

  • mcflyamorim

    Jonathan D. Myers
    Hi Jonathan I’ve tried this on SQL2005/2008/2012 and they all works.

    Not sure about 2000…