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 ««12345»»»

Conditional WHERE Clauses and Boolean Algebra Expand / Collapse
Author
Message
Posted Tuesday, August 31, 2010 4:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 1:51 AM
Points: 4, Visits: 97
GOSH!


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

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

Sorry!
Post #977809
Posted Tuesday, August 31, 2010 4:16 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 24, 2014 1:51 AM
Points: 4, Visits: 97
Post #977813
Posted Tuesday, August 31, 2010 4:21 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, May 22, 2014 8:32 AM
Points: 163, 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.
Post #977816
Posted Tuesday, August 31, 2010 4:32 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:49 AM
Points: 1,069, Visits: 897
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...


Post #977818
Posted Tuesday, August 31, 2010 5:07 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 15, 2014 6:55 PM
Points: 19, Visits: 53
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.
Post #977835
Posted Tuesday, August 31, 2010 5:10 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 15, 2014 6:55 PM
Points: 19, Visits: 53
FYI SSC-Enthusiast is just a Forum Tag...not his/her handle.
Post #977838
Posted Tuesday, August 31, 2010 5:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, September 2, 2010 8:09 AM
Points: 1, 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
Post #977850
Posted Tuesday, August 31, 2010 5:59 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, July 8, 2014 1:39 PM
Points: 245, Visits: 735
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.
Post #977858
Posted Tuesday, August 31, 2010 6:06 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 8:24 AM
Points: 854, Visits: 860
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
Post #977865
Posted Tuesday, August 31, 2010 6:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 27, 2011 2:07 AM
Points: 11, 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.

Post #977885
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse