April 18, 2013 at 11:34 pm
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.
April 19, 2013 at 12:51 am
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'?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 19, 2013 at 1:14 am
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
April 19, 2013 at 1:23 am
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?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 19, 2013 at 1:28 am
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
April 19, 2013 at 1:41 am
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...
April 19, 2013 at 1:42 am
Gail Shaw,
Can you please refer to my second example where I have given the script to create table.
April 19, 2013 at 2:14 am
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
April 19, 2013 at 4:00 am
...an excerpt from the book with explanation the short-circuit and the all-at-once concept :
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply