Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Short Circuit in sql Server!!! Expand / Collapse
Author
Message
Posted Thursday, April 18, 2013 11:34 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:22 AM
Points: 720, Visits: 552
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.






Post #1444234
Posted Friday, April 19, 2013 12:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 11:23 AM
Points: 5,171, Visits: 12,026
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'?


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1444242
Posted Friday, April 19, 2013 1:14 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:22 AM
Points: 720, Visits: 552
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
1 1 1
2 2 2
3 3 3
4 4 4
5 abc 5

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












Post #1444246
Posted Friday, April 19, 2013 1:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 11:23 AM
Points: 5,171, Visits: 12,026
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?




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1444251
Posted Friday, April 19, 2013 1:28 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 11:01 AM
Points: 40,197, Visits: 36,600
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 2008, MVP
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

Post #1444254
Posted Friday, April 19, 2013 1:41 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:42 PM
Points: 224, Visits: 1,732
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...
Post #1444257
Posted Friday, April 19, 2013 1:42 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:22 AM
Points: 720, Visits: 552
Gail Shaw,

Can you please refer to my second example where I have given the script to create table.
Post #1444259
Posted Friday, April 19, 2013 2:14 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 11:01 AM
Points: 40,197, Visits: 36,600
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 2008, MVP
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

Post #1444267
Posted Friday, April 19, 2013 4:00 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:42 PM
Points: 224, Visits: 1,732
...an excerpt from the book with explanation the short-circuit and the all-at-once concept :
http://books.google.pl/books?id=hTw3BkDrGvkC&pg=RA1-PA12&lpg=RA1-PA12&dq=mssql+short-circuit+concept+and+all-at-once&source=bl&ots=UoSbEPLcNw&sig=35RifQXXyFiP5xrdWWy2Crd07yY&hl=en&sa=X&ei=VhRxUcTdAeKI7AbwqYHADw&sqi=2&ved=0CC8Q6AEwAA#v=onepage&q&f=false
Post #1444296
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse