|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Yesterday @ 12:56 PM
Points: 22,
Visits: 240
|
|
| Comments posted here are about the content posted at temp
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, February 03, 2012 12:19 AM
Points: 22,
Visits: 68
|
|
This technique, although interesting, should be used sparingly as it affects performance. The optimizer will perform full table scans on all tables that have a smart filter on them. Therefore, I believe this technique is only acceptable if you're expecting your query to need full table scans anyway. Switch on Execution Plan viewing and inspect the execution plans for the SQL below. The SELECT with the smart filter will always read the entire Customers table, the SELECT you would normally arrive at doing it the conventional way (by building up your SQL according to the filter values that were not null or not the default value) will use a clustered index seek. DECLARE @CustomerID nchar(5) SET @CustomerID = 'AROUT' SELECT * FROM Customers WHERE (CustomerID = @CustomerID OR @CustomerID IS NULL) SELECT * FROM Customers WHERE CustomerID = @CustomerID
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, January 07, 2007 9:38 PM
Points: 1,
Visits: 1
|
|
Another way i commonly use is an extension on the above WHERE ((@customerid is not null and customerid = @customerid) or @customerid is null)
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, April 07, 2010 12:40 AM
Points: 8,
Visits: 17
|
|
If NULL is the default value used, the following construct can also be used: WHERE (Column_Name = ISNULL(@Param_Name, Column_Name))
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 1:35 AM
Points: 139,
Visits: 4,605
|
|
Or the use of the SQL standard COALESCE. Example:
WHERE Column_Name=COALESCE(@Param_Name, Column_Name)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 2:10 PM
Points: 2,668,
Visits: 688
|
|
I experimented with this many years ago, if tables are small the benefits are good but on anything else the performance is apalling in terms of i/o and data cache flush. The more optional parameters there are the worse it gets. However, if the proc doesn't get called too often e.g. it's not in an oltp system and you need to avoid dynamic sql because of the security issues and views don't do much then I agree it's a good solution. From my view in performance tuning developers usually find this and use it without testing on higher numbers of rows ( I won't say large table as it's all relative ) - so it works fine in test - in production tables get into double figure thousands of rows ( or worse ) and suddenly you're looking at a simple proc which is doing very high i/o to return ( usually ) 1 row. So basically what I'd say is this solution is good but it doesn't scale, so use with care. I'm not having a pop here Alex btw .. but this illustrates that you should test with execution plans when developing code solutions and remember to scale up and out as part of testing. I hope we don't get into another one of these silly dynamic sql arguments either!!
The GrumpyOldDBA www.grumpyolddba.co.uk http://sqlblogcasts.com/blogs/grumpyolddba/
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 29, 2012 11:22 AM
Points: 1,755,
Visits: 4,652
|
|
Alex - were you aware of another of Erland's excellent articles which discusses just this problem (in a good deal of detail)? http://www.sommarskog.se/dyn-search.html
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, February 07, 2013 11:44 AM
Points: 51,
Visits: 40
|
|
Thanks for the tip, I've come at this problem in the past without any good solutions. For smaller tables, this is perfect!
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 21, 2008 12:41 PM
Points: 1,
Visits: 2
|
|
how is this technique all that clever? If anything, using select * from Customers where customerid=isnull(@customerid,customerId) is more efficient, easier to read and takes up less space.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Monday, July 30, 2012 10:42 AM
Points: 3,434,
Visits: 519
|
|
I found the example difficult to follow. To demonstrate the technique you could use one table with one WHERE without XML processing that is not easy for entry-level developers and professional level administrators and without joins. Also there is too much stress on NULL as a defult value. Do not forget that we mostly are in the described situation when users have to select something and they do it through the front end. The developers often set default value for the dropdown boxes to something meaningful like "please, select the value from the list" or in many cases "All"
Regards, Yelena Varshal
|
|
|
|