that COALESCE should be much more effecient than Dynamic SQL querys,, but
why does my COALESCE query take more than 1000 times longer time to execute
than my dynamic one.
I Cant understand IT.....
For the above the ISNULL is more efficient then COALESCE.
At the end of the day using dynamic SQL in this scenario is far better -
make sure you try and parameterise the query using sp_executesql...
SET @sql = 'SELECT * FROM sysobjects WHERE type=@type'
EXEC sp_executesql @sql, N'@type char(1)', @type = 'T'
--
Tony Rogerson SQL Server MVP
Torver Computer Consultants Ltd
http://www.sql-server.co.uk
"Jackie Boy" <jack.ro...@sreg.com> wrote in message
news:uo1LobHNBHA.2400@tkmsftngp02...
the COALESCE query ended in a clustered index scan..
while my dynamiq query ended in an index seek.
Conclusion,, when I use COALESCE the optimizer doesnt use my own configured index seek. Is it possible to force it to use this in this case?? And if it is possible how do I do??
Thanx by the way, for your answer..
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
With dynamic execution, as non-portable, non standard, ugly as it may be,
you have better chances for a better performing query, as the result query
gets optimized, and in this query, all filters use literals and not
functions. This doesn't mean that dynamic execution doesn't have it's
disadvantages. For example, a user executing a stored procedure that uses
dynamic execution requires direct permissions to perform the activity which
is run dynamically, and not just execute permissions on the proc itself.
You need to consider all factors and decide what's more important to you...
--
BG
Hi-Tech College, Israel
http://sql.hi-tech.co.il
"Jack Ronnovius" <jack.ro...@sreg.com> wrote in message
news:ujP6A#HNBHA.3036@tkmsftngp02...
If this is true, then the ISNULL solution is just as non-portable and
non standard as the dynamic sql solution.
Anyway, the reason why a query like WHERE Cus_Name =
COALESCE(@Cus_Name,Cus_Name) results in an index scan or table scan (as
opposed to an index seek) is, because COALESCE(@Cus_Name,Cus_Name) is
non-SARGable. In this case, when @Cus_Name is null, SQL-Server needs to
evaluate all rows.
Dynamic SQL can provide a solution. IMO a dynamic sql solution is
usually messy, and hard to maintain because of the complexity.
Another posible solution which is good for performance, is something
like:
If @Cus_Name IS NULL
Begin
SELECT ..
End
Else
Begin
SELECT .. WHERE Cus_Name = @Cus_Name
End
This kind of solution has several disadvantages:
1. the code is hard to maintain, because of all the copying and pasting
and only the small changes between the queries;
2. theoretically the number of branches grows exponentially with the
number of COALESCE-constructs. However, see example below;
3. you are doing the work that you should expect SQL-Server to do for
you. It is just too bad that SQL-Server does not handle this situation
very efficiently.
Suppose you have several COALESCE-constructs in your query. If you
choose to use this kind of solution, and are willing to do SQL-Server's
job, then you could probably get sufficient performance using something
like:
If @Cus_Name IS NOT NULL
SELECT ..
WHERE Cus_Name = @Cus_Name
AND Cus_City = COALESCE(@Cus_City,Cus_City)
AND Cus_Country = COALESCE(@Cus_Country,Cus_Country)
Else
Begin
If @Cus_City IS NOT NULL
SELECT ..
WHERE Cus_City = @Cus_City
AND Cus_Country = COALESCE(@Cus_Country,Cus_Country)
Else
Begin
If @Cus_Country IS NOT NULL
SELECT ..
WHERE Cus_Country = @Cus_Country
Else
SELECT ..
End
End
For optimal performance, you should test the most selective column
first. So in this example, the customer name is assumed to be the most
selective column, then customer city, and customer country is assumed to
be least selective.
Hope this helps.
Gert-Jan
WHERE (Field1 = @FieldValue or @FieldValue is null)
Zach
"Gert-Jan Strik" <so...@toomuchspamalready.nl> wrote in message
news:3B952887...@toomuchspamalready.nl...
WHERE Field1 LIKE COALESCE( @SearchVal, '%' )
This will perform as fast or faster than the dynamic SQL. The one
advantage you get with dynamic SQL is when you have multiple search
conditions, you can suppress some columns easily & the plan would use only
indexes that are required. With static SQL, the plan is nearly fixed. I
usually prefer the static SQL because the code is much easier to read &
maintain. In majority of cases, you can tune the queries without using
dynamic SQL.
--
Umachandar Jayachandran
SQL Resources at http://www.umachandar.com/resources.htm
( Please reply only to newsgroup. )
Zach
"Umachandar Jayachandran" <umach...@yahoo.com> wrote in message
news:udiHRcZNBHA.1932@tkmsftngp02...
do you also know a trick that works if the column datatype is non-char
(for example int)?
Gert-Jan
SET @MinVal = -POWER( 2., 31 ) -- or whatever minimum based on your data
SET @MaxVal = POWER( 2., 31 ) - 1
SET @IntVal = 123
WHERE intCol BETWEEN COALESCE( @IntVal, @MinVal ) And
COALESCE( @IntVal, @MaxVal )
You can do the same for datetime columns too! These actually work pretty
good. I have used this technique instead of dynamic SQL lot of times with
nice results. These work based on the boundary values for each datatype &
hence you need to hard-code that in the code. Alternatively, you can do MIN
or MAX queries & set the values.
Gert-Jan
WHERE (intCol = @IntVal) OR (@IntVal IS NULL) ??
Steve Kass
Drew University
Zach
"Steve Kass" <sk...@drew.edu> wrote in message
news:3B97DA7A...@drew.edu...
Oh well.
Steve
Steve