We help IT Professionals succeed at work.

A Difficult Query / Performance issue

302 Views
Last Modified: 2012-06-21
Hi There,

I have the following query i'm running, and i want to see if i can speed it up.

Here is the situation:

I have a temp table (#temp), which holds the results of a number of queries.

The calculation i am performing is based off of data from three tables, all of which contain a couple hundred thousand records.

For the purposes of this question here are some basics:

Table1 - Main table. Has 1 primary key guid column
           - Has a field, Limit, which we subtract from our calculation.

Table2 - Secondary table. Has a foreign key relationship with table1
           - Has a field, Amount, which we want to find the sum of.

Table3  - Secondary table. Has a foreign key relationship with table1
           - Has a field, Amount2, which we want to find the sum of.
           - Very large table. Also need to filter this to records for the next 7 days based on a date field

Here is the query:

Update #temp
Set CalculatedField =
(Select Sum(t2.Amount) - Sum(t1.Limit) +
    (Select Sum(t3.Amount) from table3 t3 
    where date <= dateadd(day,7,getdate())
    and ID = #temp.ID)

from table1 t1
left join table2 t2 on t1.ID = t2.ID
where t1.ID = #temp.ID)

Open in new window


EDIT: I should mention that the reason i have the subquery is purely for the filtering by 7 days records. I didn't have this subquery in the past, i had the table joined in. But i haven't been able to figure out how to filter the results from that table without a subquery.

Any advice anyone can provide is most appreciated!!

Cheers,
Ben
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
This is what i ended up going with:

		(Select (select COALESCE(SUM(table3.Amount),0) from table3 where table3.id=temp.ID AND date<= dateadd(day,7,getdate())) + Table2Amount as totalamount
		
		 from

		(SELECT ID, COALESCE(SUM(table2.amount),0)  as Table2Amount 
		from table1
		LEFT JOIN table2 on table1.id = table2.id
		GROUP BY id) as table1 WHERE table1.id = #temp.id)

Open in new window

           

This brought it down to around a second, but didnt filter out records from table 1 and 2 if there was no corresponding record in table 3 in the next 7 days.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.