Thursday, April 10, 2008

I want some Moore - Short circuiting Sql Parameters

 

OR Conditions

Run this statement:

SELECT * FROM t1 
WHERE id = 3
OR id / 0 = 1

Here it is a whole different story than above with only AND conditions. Here you immediately see the error "Divide by zero error encountered". If you take a closer look you'll see that we never get to the execution plan phase. Why does this differ so much from the AND conditions?  Because OR's immediately mean Table/Index scan. Therefore no parameterization is done and at the query evaluation time all of the values are known. This is when the divide by zero is caught before the execution plan is even built or taken from cache and our query fails. It doesn't matter that our first condition is already true.


I want some Moore

No comments: