SQL SERVER – Order of Conditions in WHERE Clauses

Sr. Developer in my organization asked me following question:

Question : Does order of conditions matter in WHERE clause? If we move the conditions in WHERE clause does it increases the performance? Does SQL Server WHERE conditions supports short circuit?

Answer: No

In SQL Server order does not matter in WHERE clause. SQL Server does not short circuit conditions as well it does not help in performance.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

5 thoughts on “SQL SERVER – Order of Conditions in WHERE Clauses

  1. I disagree.

    case in point:

    If you have a column which contains both text and integers and you try to query only int. using ismumeric funtion, it matters where you place this check in your where clause.

    Following will fail:

    select
    from
    where convert(int, txtCol) = 1
    and isnumeric(txtCol) = 1

    Following will work:

    select
    from
    where isnumeric(txtCol) = 1
    and convert(int, txtCol) = 1

    At least this is my obersvation.

    Like

  2. I have been trying to research this, issue as well as the effect of the order of conditions in a join (separated by AND).

    For example, if Table b is partitioned on Col3, could we see a performance improvement by listing a.Col3 = b.Col3 first in the following join?

    SELECT a.Col1, b.col1
    FROM TableA AS a
    INNER JOIN TableB AS b
    ON a.Col1 = b.Col1
    AND a.col2 = b.Col2
    AND a.Col3 = b.Col3

    Some people say that the optimizer will do this internally. I asked this question of another SQL Server Expert and was told that they are processed in order, so it does make a difference. He provided the following queries as evidence:

    This query will not error. The first condition is met, so the 2nd condition is never processed.

    select 1
    where 1=0
    and cast(‘bob’ as datetime)=’hello’

    This query will error since the first condition is impossible.

    select 1
    where 1=1
    and cast(‘bob’ as datetime)=’hello’

    This was shown as proof that conditions are processed in order. He said that join conditions are processed in the same way.

    My initial thinking is that if the column on which the table is partitioned is listed first, SQL Server would only have consider that single partition, saving on reads of the others disks and therefore improving performance of that stored procedure and of the database overall.

    When I tested this however, I was unable to see any difference in the execution plan.

    If there is no difference, can you explain to me how SQL Server does this, and why the above queries operate as they do?

    Thank you

    Like

  3. This is an interesting debate. I am a PeopleSoft developer and have worked extensivvely on SQLs. I think order of clause has very less impact.

    To justify what I say I have a simple example:
    There is a big query which joins many tables and so has various mappings. Now I want this query to run conditionally. So I declare a variable outside the SQL, and use it in the SQL. Where ever I place this fairly simple condition ( substitued condition looks like ‘C’ = ‘C’ or ‘C’ = ‘F’) the query exectution doesnt change much. It takes aprox 4 secs where a false boolean is encountered.

    When the amount of data to be fetched is less, on re-executing the same query many times we get different timings. This has various parameters like was the Query compiled/executed recnetly, etc.

    Like

  4. Pingback: SQL SERVER – Weekly Series – Memory Lane – #032 | Journey to SQL Authority with Pinal Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s