Short Circuit in sql Server!!!

  • Hi All,

    Can I short circuit WHERE clause in this query without using CASE...

    select * from company c1 where country = 'USA' or country = 'JPN'

    One more Issue:

    Query 1.

    select * from company c1 where

    country =

    case when 1=1 then

    'USA'

    else

    'JPN'

    end

    (107 row(s) affected)

    Table 'company'. Scan count 1, logical reads 7

    Query 2.

    select * from company c1 where country = 'USA' or country = 'JPN'

    (112 row(s) affected)

    Table 'company'. Scan count 1, logical reads 7

    Why I/O Statistics are same for both the above queries.

  • I've just read this a few times and I'm still not sure what you mean. What, exactly, do you mean by 'short circuit'?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Ok, to understand short circuit take this example

    CREATE TABLE Test_Circuit (

    a INT NOT NULL PRIMARY KEY CLUSTERED,

    b VARCHAR(128) NULL,

    c INT NULL

    )

    and input values as below

    col1 col2 col3

    111

    222

    333

    444

    5abc5

    Now run below scripts one by one, do you find any difference...

    Script 1.

    SELECT *

    FROM

    Test_circuit

    WHERE

    ISNUMERIC(b)= 1 and b < 10

    Script 2.

    SELECT *

    FROM

    Test_circuit

    WHERE

    b < 10 and ISNUMERIC(b)= 1

  • Now run below scripts one by one, do you find any difference...

    No. Each side of the AND condition has to be evaluated to determine whether the row is selected.

    But if you'd used OR rather than AND, are you wondering whether, if the first condition evaluates to TRUE, subsequent tests are omitted?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • No, SQL Server does not do reliable position-based short circuiting of expressions. The order things will be evaluated in depends on indexes, data distribution, estimated row counts and whatever plan the optimiser comes up with.

    I would imagine both of those queries do 7 reads because they're scanning the entire table (or maybe scanning an index). Without table defs and index defs impossible to say for sure.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • T.Ashish (4/18/2013)


    ....

    Why I/O Statistics are same for both the above queries.

    Try on bigger one and with bigger difference in selectivity. Probably 112 rows reside on 7 pages and 107 rows also reside on 7 pages...

  • Gail Shaw,

    Can you please refer to my second example where I have given the script to create table.

  • Same answer. Those queries do the same number of reads because they're doing table scans, there's no useful indexes. SQL has to read every single row of every page in that table to evaluate whether or not a row matches those conditions.

    SQL does not do reliable position-based short circuiting. Evaluation order is determined by indexes, data distribution, estimated row counts and the plan that the optimiser comes up with, not the position of the predicate within the where clause. SQL is a declarative language, you specify what you want, not how it must be achieved.

    Hence you may in some cases get short circuiting but it is not guaranteed and a change of data or addition of indexes can change the behaviour entirely. Do not rely on any perceived short circuiting that you see.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply