Hi Friends,

In my previous blog post, we have seen that SQL Sever estimates number of rows in a table variable was 1. Now the question is, Is there any way to make the correct estimation for the number of rows while using table variable?

My today’s blog post is focused on the correct cardinality estimation using table variable. The answer of the above question is: yes, the optimizer can make a correct cardinality estimation for table variables by using OPTION (RECOMPILE) hint for that. Using this hint also has some cons like new plan will generate every time. It’s like a trade-off between both the mechanism. Let me show you the same thing.

First: Use of table variable without Option Recompile


Second: Use of table variable with Option Recompile


From the above image you can see both actual and estimated number of rows are same and decision of using join also has been changed, the earlier nested loop was used but now it is using hash match.

Is there really a performance improvement due to the above change? Yes, you can see the stats below:



Prince Kumar Rastogi

Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook

Follow Prince Rastogi on Twitter | Follow Prince Rastogi on FaceBook