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
 General SQL Server Forums
 New to SQL Server Programming
 performance question

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2014-05-24 : 14:26:18
I have I rather complex query and need to display a Sum in different ways. Now I'm wondering if the performance improves if you nest the queries in the described way. To me it looks, as if the sum just has to be calculated once? Can I expect the performance to be better?
select SUM(tiempo) as time_minutes, 
CONVERT(varchar(5), dateadd (minute,sum(tiempo),'1900-1-1 0:00'), 114) as time_hours,
SUM(time)* 0.95 as time_discount
from table

Select tiempo as time_minutes,
CONVERT(varchar(5), dateadd (minute,tiempo,'1900-1-1 0:00'), 114) as time_hours,
tiempo * 0.95 as time_discount
From (
Select Sum(tiempo) as tiempo
from table)a

regards,
Martin

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-05-25 : 00:27:59
quote:
Originally posted by barnabeck

I have I rather complex query and need to display a Sum in different ways. Now I'm wondering if the performance improves if you nest the queries in the described way. To me it looks, as if the sum just has to be calculated once? Can I expect the performance to be better?
select SUM(tiempo) as time_minutes, 
CONVERT(varchar(5), dateadd (minute,sum(tiempo),'1900-1-1 0:00'), 114) as time_hours,
SUM(time)* 0.95 as time_discount
from table

Select tiempo as time_minutes,
CONVERT(varchar(5), dateadd (minute,tiempo,'1900-1-1 0:00'), 114) as time_hours,
tiempo * 0.95 as time_discount
From (
Select Sum(tiempo) as tiempo
from table)a

regards,
Martin

Assuming the first query has a typo and you really meant SUM(tiempo) in the third column, I don't think you would have any significant performance difference. SQL query optimizer is smart enough to figure out the logic and calculate the sum just once.
Turn on the query plan (control-m) and run both queries and compare the plans.
Go to Top of Page

GouravSaxena1987
Starting Member

23 Posts

Posted - 2014-05-27 : 01:28:52
I guess performance of both query will be same.
You should compare there execution plan as well as check with using query hint if it will help you to optimize query.

Regards,
Gourav Saxena
Data Warehouse Counsultant
GouravSaxena1987@gmail.com
Go to Top of Page
   

- Advertisement -