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


Conditional WHERE Clauses and Boolean Algebra


Conditional WHERE Clauses and Boolean Algebra

Author
Message
pfranceschini
pfranceschini
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 100
GOSH!
w00t

it's the SAME solution proposed by Toby Harman!!

I didn't read he also suggested the use of ISNULL around the parameter...

Sorry!
:-P
pfranceschini
pfranceschini
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 100
:-D
Rob Fisk
Rob Fisk
Say Hey Kid
Say Hey Kid (667 reputation)Say Hey Kid (667 reputation)Say Hey Kid (667 reputation)Say Hey Kid (667 reputation)Say Hey Kid (667 reputation)Say Hey Kid (667 reputation)Say Hey Kid (667 reputation)Say Hey Kid (667 reputation)

Group: General Forum Members
Points: 667 Visits: 428
I didn't use ISNULL or COALESCE becuase i defaulted the parameters to '' instead of null to remove the need.

Of course you have to ensure that the front end either does not include a parameter if blank or passes and empty string instead of null.

_______________________________________________________
Change is inevitable... Except from a vending machine.

RichB
RichB
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4680 Visits: 1083
Its fine on something small, but absolutely appalling performance wise on bigger tables. GHY if you have a set of joins on big tables and you try to use this approach, cos the optimiser sure won't...



Eric Wahner-345205
Eric Wahner-345205
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 66
Not only does this methodology almost always lead to poor performance in whatever query you are writing be it an Index Scan or Table Scan, this methodology breaks some of the core principals of code separation. You will lose on cache hits since there could ultimately be multiple plans generated from the variety of parameters passed in.

Every procedure should have a single purpose. There is nothing worse than trying to figure out why a procedure performs well sometimes but then performs poorly on other scenarios.
Eric Wahner-345205
Eric Wahner-345205
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 66
FYI SSC-Enthusiast is just a Forum Tag...not his/her handle.
Tony Alicea
Tony Alicea
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 37
Hello,

Lots of posts here about efficiency and COALESCE and ISNULL. The points are well taken, though that really wasn't the point of this little article.

The point was more meant to be the importance of understanding that there are almost always non-procedural ways to accomplish a query task, and that those solutions are generally more elegant in form.

I also personally think it is good for any query writer to know how to think purely mathematically, before using any syntax specially provided by the query language, such as COALESCE and ISNULL. I've seen way too many simply copy-and-paste these techniques without really understanding what was going on. Of course - if you have understanding, then by all means run with it!

As far as efficiency - in 14 years I've always found this technique to be acceptable in the realm of efficiency and (especially in the old days) was given weight over closing the potential security holes of dynamic SQL.

Of course, every scenario is different and I'm sure there are scenarios in which a different approach is preferable (there always are).

Again - I wanted to post something stressing an appreciation of fundamentalism over T-SQL in the learning process, I've always found you start with a mathematically elegant solution and can then generally increase its efficiency by taking advantage of whatever specialties the DBMS you are working in provides.

Thanks for the posts,

Tony
Tobar
Tobar
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1669 Visits: 758
And I think you did a great job Tony.

[This is a paraphrase from a Teddy Roosevelt quote I have read somewhere here on SSC]
-- The glory belongs to those that are in the game not in the bleachers talking about the game.

<><
Livin' down on the cube farm. Left, left, then a right.
ronmoses
ronmoses
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2188 Visits: 1021
I don't claim to be an expert on the subject, but when faced with a query like this:


SELECT field
FROM table
WHERE (field LIKE '%A%') OR (field LIKE '%B%')



I find I get much better performance if I do this...


SELECT field
FROM table
WHERE (field LIKE '%A%')
UNION
SELECT field
FROM table
WHERE (field LIKE '%B%')



INTERSECT and EXCEPT can also be used, along with the judicious application of parentheses, to get the results you desire. It's worked quite well for me on a number of occasions, though I've never tried it with huge data sets.

ron

-----
a haiku...

NULL is not zero
NULL is not an empty string
NULL is the unknown

Rolph Kreis
Rolph Kreis
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 20
Hello together,

Despite it is a good idea using SQL in its pure descriptive nature, because it normally leads to generally better performance, one has to be aware of caveats in assuming that things are evaluated in an "intuitive order".

Neither the relatinonal nor the boolean algebra does explicitly defines an evaluation order.

In regard to this article it is assumed that the boolean subexpressions will be evaluated from right to left in an fail-fast-manner. Although this could be implemented like that it does not need to. Thus leading to unportable and/or bad performing queries. Just imagine that the default values for the positional parameters would have been changed to the empty string and the evaluation would be left-to-right. The query will do always a full scan over the possibly huge table with returning unwanted tuples.

Therefore one should never assume the natural order in SQL-Queries as long as These are not guaranteered by the DBS.
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