Search code examples
sql-server-2005sql-execution-plan

How is working the "Query cost" in the execution plan?


I am trying to increase one of my request performance. My request is made of 10 different select .

The actual production query is taking 36sec to execute.

If I display the execution plan, for one select I have a query cost of 18%.

So I change a in clause (in this select) with an xml query (http://www.codeproject.com/KB/database/InClauseAndSQLServer.aspx).

The new query now takes 28 sec to execute, but sql server tells me that the above select has a query cost of 100%. And this is the only change I made. And there is no parallelism in any query.

PRODUCTION : 36sec, my select is 18% (the others are 10%).

NEW VERSION : 28sec, my select is 100% (the others are 0%).

Do you have any idea how sql server compute this "query cost" ? (I start to believe that it's random or something like that).


Solution

  • Query cost is a unitless measure of a combination of CPU cycles, memory, and disk IO.

    Very often you will see operators or plans with a higher cost but faster execution time.

    Primarily this is due to the difference in speed of the above three components. CPU and Memory are fairly quick, and also uncommon as bottlenecks. If you can shift some pressure from the disk IO subsystem to the CPU, the query may show a higher cost but should execute substantially faster.

    If you want to get more detailed information about the execution of your specific queries, you can use:

    SET STATISTICS IO ON
    SET STATISTICS TIME ON
    

    This will output detailed information about CPU cycles, plan creation, and page reads (both from disk and from memory) to the messages tab.