Search code examples
sqlsql-serverestimation

SQL for query cost estimate on SQL Server


In SQL Server, is there some magic SQL I can preprend my SQL query to get an estimated query cost rather than the query executed?

Possibly something like the below is my best guess myself.. seems very verbose though

SET STATISTICS PROFILE ON
GO

SELECT *  FROM [Account]

GO 
SET STATISTICS PROFILE OFF

Solution

  • SET SHOWPLAN_XML ON;
    GO
    SELECT * FROM master..spt_values
    GO
    SET SHOWPLAN_XML OFF;
    

    STATISTICS PROFILE gives you the actual plan, which means that the query will get executed. SHOWPLAN_XML or SHOWPLAN_ALL (text plan) will give you the estimated plan without executing the query. You can click on the xml result to see the graphical plan. If that does not work use SQL Sentry Plan Explorer (free) to show the plan.