Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Optimizing Sql Union Query with passing parameters

Author  Topic 

sk.faheemuddin
Starting Member

2 Posts

Posted - 2012-05-12 : 01:31:14
Hi,

I am having a query containing three unions.
But i want to compulsory execute 1st query
rest two query should be optional based on the
users interest using parameters
because 2nd and 3rd query are time consuming.

But even though data retrieval point of view
it is working very fine but time taken is same
there is no change

for example

/*Parameter Area*/
declare @StartDate as datetime
set @StartDate ='09-MAY-12'
declare @EndDate as datetime
set @EndDate ='09-MAY-12'
declare @With_MonthYear as nvarchar(1)
set @With_MonthYear ='N'
/*Main Query Area*/

select * from a where a.date betwn @startdate and @enddate;
union all --B
select * from a where a.date betwn @startdate and @enddate
and 'Y'=@With_MonthYear;
union all --C
select * from a where a.date betwn @startdate and @enddate
and 'Y'=@With_MonthYear;

i.eee the 2nd n 3rd should only work when user selects 'Y'
but it execute i guess and take same amount of time
approx..more thn 2mins

Please advice

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-12 : 06:15:54
[code] Try this


/*Parameter Area*/
declare @StartDate as datetime
set @StartDate ='09-MAY-12'
declare @EndDate as datetime
set @EndDate ='09-MAY-12'
declare @With_MonthYear as nvarchar(1)
set @With_MonthYear ='N'
/*Main Query Area*/


if(@With_MonthYear ='Y')
begin
select * from a where a.date betwn @startdate and @enddate;
union all --B
select * from a where a.date betwn @startdate and @enddate
and 'Y'=@With_MonthYear;
union all --C
select * from a where a.date betwn @startdate and @enddate
and 'Y'=@With_MonthYear;

else
select * from a where a.date betwn @startdate and @enddate;

END
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-12 : 20:08:10
quote:
Originally posted by sk.faheemuddin

Hi,

I am having a query containing three unions.
But i want to compulsory execute 1st query
rest two query should be optional based on the
users interest using parameters
because 2nd and 3rd query are time consuming.

But even though data retrieval point of view
it is working very fine but time taken is same
there is no change

for example

/*Parameter Area*/
declare @StartDate as datetime
set @StartDate ='09-MAY-12'
declare @EndDate as datetime
set @EndDate ='09-MAY-12'
declare @With_MonthYear as nvarchar(1)
set @With_MonthYear ='N'
/*Main Query Area*/

select * from a where a.date betwn @startdate and @enddate;
union all --B
select * from a where a.date betwn @startdate and @enddate
and 'Y'=@With_MonthYear;
union all --C
select * from a where a.date betwn @startdate and @enddate
and 'Y'=@With_MonthYear;

i.eee the 2nd n 3rd should only work when user selects 'Y'
but it execute i guess and take same amount of time
approx..more thn 2mins

Please advice



all queries look same in above. then why are calling it again and again? i cant understand what you're trying to achieve. Are you trying to replicate the data?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2012-05-13 : 01:50:26
You could split the two options into 2 stored procedures. Make the decision at the application level

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

sk.faheemuddin
Starting Member

2 Posts

Posted - 2012-05-13 : 05:25:45
Hi @Vijays3,

Thanks, It's Done.

Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-13 : 07:45:24
[code]Welcome[/code]

Vijay is here to learn something from you guys.
Go to Top of Page
   

- Advertisement -