SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Short Circuit in sql Server!!!


Short Circuit in sql Server!!!

Author
Message
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1027 Visits: 602
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.
Phil Parkin
Phil Parkin
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25622 Visits: 20678
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.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1027 Visits: 602
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
Phil Parkin
Phil Parkin
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25622 Visits: 20678
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.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114187 Visits: 45475
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


e4d4
e4d4
Mr or Mrs. 500
Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)

Group: General Forum Members
Points: 587 Visits: 2399
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...
T.Ashish
T.Ashish
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1027 Visits: 602
Gail Shaw,

Can you please refer to my second example where I have given the script to create table.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114187 Visits: 45475
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


e4d4
e4d4
Mr or Mrs. 500
Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)

Group: General Forum Members
Points: 587 Visits: 2399
...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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search