Oct 22, 2010

NO Recompilations when (parameter) values are different

Please read another post on this subject by Dinesh Priyankara.

This is another topic came in the last user group meeting: 
Does SQL Server generate a new plan (in other word recompile) the query if the parameter value is changed?
The actual answer is it depends:
First of all, SQL Server does not go through the recompilation process simply because the parameter is changed.
As I mentioned in the previous blog, there are only certain conditions where an existing execution plan is thrown away and a new plan is used.

Even though in my example, a new plan is created between these two statements, the value is not the cause.


SELECT * FROM Sales.SalesOrderDetail
WHERE SalesOrderDetailID <= 1
SELECT * FROM Sales.SalesOrderDetail
WHERE SalesOrderDetailID <= 1000
When I ran the queries and captures the SQL:Recompile event, nothing was captured. Interestingly, ShowPlan XML for Query Compilation too, didn't give any number as the reason for recompilation. For me it looked like SQL Server didn't consider them as same query.
I removed the GO statement in between and tried too. The result was same. (except the column line number showed 1 and 2 instead of 1 always.
The image is attached.

Fortunately, SQL Server provides a few DMVs (dynamic management views) which allows us to check the execution plans

Oct 21, 2010

Recompilations after index creation

Yesterday I got an interesting question: Does adding an index to a table force the execution plans to be recreated?
Books online says this:

Please note the first, second and fourth bulleted points.

It says either a structure should be changed or index used by the query should be changed. In other words not any change in the index will not force recompile.

Oct 20, 2010

Today I will be speaking at the user group meeting

As some of you know, SS SLUG (SQL Server Sri Lanka User Group) meets today. Generally we have two topics to discuss. 
According to the plan we had earlier, I will be in the panel of the second topic (database design)
But due to certain unforeseen issues, at the last minute, I accepted the challenge to speak at today's user group meeting. That means I will speak on execution plans first and then will be in the panel for the second topic too.

If everything goes well with the plan, In today's presentation, I will talk about reading execution plan, various table and index operations, and the logic behind it. At the end of the session, I expect the users to guess the execution plan for simple queries

See you there!