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

Is my execution plan trying to trick me?


I am trying to speed up a long running query that I have (takes about 10 minutes to run...). In order to track down what part of the query is costing me the most time I included the Actual Execution Plan when I ran it and found a particular section that was taking up 55% (screen shot below)

alt text http://img109.imageshack.us/img109/9571/53218794.png

This didn't quite seem right to me so I added Print '1' and Print '2' before and after this trouble section. When I run the query for a mere 17 seconds and then cancel it the 1 and 2 print out which I'm assuming means it's getting through that section in the first 17 seconds.

alt text http://img297.imageshack.us/img297/4739/66797633.png

Am I doing something wrong here or is my Execution plan misleading me?


Solution

  • I wouldn't trust that printing the '1' and '2' will prove anything about what has executed and what has not. I do the same thing, but I just wouldn't rely on it as proof. You could print the @@rowcount from that first insert query - that would indicate for sure that the insert has occurred.

    Although the plan says that query may take 55% of the cost, it may not be 55% of the execution time, especially if the query results are cached.

    Another advantage of printing the @@rowcount is to compare the actual number of rows to the estimated rows (51K). If they differ by a lot then you might investigate the statistics for your indexes.