|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 9:01 AM
Points: 4,
Visits: 87
|
|
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!
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, January 11, 2013 9:01 AM
Points: 4,
Visits: 87
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, June 29, 2012 4:56 AM
Points: 163,
Visits: 427
|
|
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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 3:06 AM
Points: 1,026,
Visits: 751
|
|
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...
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, February 07, 2013 7:52 AM
Points: 19,
Visits: 51
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, February 07, 2013 7:52 AM
Points: 19,
Visits: 51
|
|
| FYI SSC-Enthusiast is just a Forum Tag...not his/her handle.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, September 02, 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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 7:58 AM
Points: 167,
Visits: 550
|
|
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.
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Yesterday @ 5:12 AM
Points: 649,
Visits: 686
|
|
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
|
|
|
|
|
Grasshopper
      
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.
|
|
|
|