Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Please help with simple proc-- slow CASE statement
Hi, I am having trouble with this query... the CASE statement makes it
terribly slow, it is very fast without it.  What is the best solution for
queries like this where it is predicated on different columns?  Query has
been simplified for posting here.

Thanks,
Dan

----------------------------------------------------------------------------
----------
declare @col CHAR(3) --normally this is passed in to the stored proc as a
param
set @col='CRE'

select *
from data
where
CASE @col
WHEN 'CRE' then created
WHEN 'MOD' then modified
WHEN 'XXX' then assigned
END
Between '2006-04-01' and '2006-04-02'



Report this thread to moderator Post Follow-up to this message
Old Post
Dan English
04-06-06 06:23 PM


Re: Please help with simple proc-- slow CASE statement
Why don't you do something like this?

if @col ='CRE'
begin
select *
from data
where created Between '2006-04-01' and '2006-04-02'
end

if @col ='MOD'
begin
select *
from data
where modified Between '2006-04-01' and '2006-04-02'
end

if @col ='XXX'
begin
select *
from data
where assigned Between '2006-04-01' and '2006-04-02'
end

Denis the SQL Menace
http://sqlservercode.blogspot.com/


Report this thread to moderator Post Follow-up to this message
Old Post
SQL
04-06-06 06:23 PM


Re: Please help with simple proc-- slow CASE statement
"SQL" <denis.gobo@gmail.com> wrote in message
news:1144345877.726389.250870@v46g2000cwv.googlegroups.com...
> Why don't you do something like this?

Thanks, thats helps with a small query like this but the actual select
statement is pretty long, would be a lot of duplication.



Report this thread to moderator Post Follow-up to this message
Old Post
Dan English
04-06-06 06:23 PM


Re: Please help with simple proc-- slow CASE statement
try:

declare @col CHAR(3) --normally this is passed in to the stored proc as
a
param
set @col='CRE'


select *
from data
where
(@col = 'CRE' AND created Between '2006-04-01' and '2006-04-02' ) OR
(@col = 'MOD' AND modified Between '2006-04-01' and '2006-04-02')  OR
(@col = 'XXX' AND assigned Between '2006-04-01' and '2006-04-02' )


Report this thread to moderator Post Follow-up to this message
Old Post
kniemczak@gmail.com
04-07-06 01:23 AM


Re: Please help with simple proc-- slow CASE statement
<kniemczak@gmail.com> wrote in message
news:1144348706.473651.16680@u72g2000cwu.googlegroups.com...
> try:
>  (@col = 'CRE' AND created Between '2006-04-01' and '2006-04-02' ) OR
>  (@col = 'MOD' AND modified Between '2006-04-01' and '2006-04-02')  OR
>  (@col = 'XXX' AND assigned Between '2006-04-01' and '2006-04-02' )


Thanks, but this is not any faster.  Looks like I have to resort to dynamic
sql.



Report this thread to moderator Post Follow-up to this message
Old Post
Dan English
04-07-06 01:23 AM


Re: Please help with simple proc-- slow CASE statement
<kniemczak@gmail.com> wrote in message
news:1144348706.473651.16680@u72g2000cwu.googlegroups.com...
>  (@col = 'CRE' AND created Between '2006-04-01' and '2006-04-02' ) OR
>  (@col = 'MOD' AND modified Between '2006-04-01' and '2006-04-02')  OR
>  (@col = 'XXX' AND assigned Between '2006-04-01' and '2006-04-02' )

Apparently it still tries to access all three columns, even though @col only
matches one.  In other words, it isn't doing a short-circuit boolean
evaulation.  Anyone know why?



Report this thread to moderator Post Follow-up to this message
Old Post
Dan English
04-07-06 01:23 AM


Re: Please help with simple proc-- slow CASE statement
Even if SQL were to shortcut this boolean expression, it would still have to
go all to the end if ((@col = 'XXX' AND assigned Between '2006-04-01' and
'2006-04-02' ) yielded TRUE. Where does @col come from by the way? Is it a
client that passes it on its own discretion, or does it come from some other
query?


"Dan English" <dan_english2@cox.net> wrote in message
news:ecakjrcWGHA.4768@TK2MSFTNGP05.phx.gbl...
> <kniemczak@gmail.com> wrote in message
> news:1144348706.473651.16680@u72g2000cwu.googlegroups.com... 
>
> Apparently it still tries to access all three columns, even though @col
> only matches one.  In other words, it isn't doing a short-circuit boolean
> evaulation.  Anyone know why?
>



Report this thread to moderator Post Follow-up to this message
Old Post
Jan Van der Eecken
04-07-06 12:24 PM


Re: Please help with simple proc-- slow CASE statement
"Jan Van der Eecken" <jvandereecken@omam.com> wrote in message
news:OZ5yHfjWGHA.3760@TK2MSFTNGP02.phx.gbl...
> Even if SQL were to shortcut this boolean expression, it would still have
> to go all to the end if ((@col = 'XXX' AND assigned Between '2006-04-01'
> and '2006-04-02' ) yielded TRUE. Where does @col come from by the way? Is
> it a client that passes it on its own discretion, or does it come from
> some other query?

Client passes it.  Most of the time the first statment is true.



Report this thread to moderator Post Follow-up to this message
Old Post
Dan English
04-07-06 06:23 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 07:40 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006