1
SELECT a.FundIDRecv,a.SubscribeDt, b.FundName, 
(
    SELECT SUM(c.PricePerWeek) 
    FROM tbl_Hive c
    WHERE c.FundID IN 
    (
        SELECT FundID from tbl_FundStatic 
        WHERE FundID IN
        (
            SELECT FundIDSend
            FROM tbl_FundSubscriptions 
            WHERE FundIDRecv = a.FundIDRecv
            AND SubscribeDt >= subdate(CURDATE(), INTERVAL weekday(CURDATE()) DAY)
        )
        AND UserID = '14'
    )
) as Price
FROM tbl_FundSubscriptions a, tbl_Hive b
WHERE a.FundIDRecv = b.FundID
AND a.SubscribeDt >= subdate(CURDATE(), INTERVAL weekday(CURDATE()) DAY)
AND a.FundIDRecv IN
(
    SELECT FundIDRecv
    FROM tbl_FundSubscriptions
    WHERE FundIDSend IN (
        SELECT FundID
        FROM tbl_FundStatic
        WHERE UserID = '14'
    )
)
GROUP BY FundIDRecv

This Query takes a huge amount of time to fetch data.

How can I optimize this query so as to make it execute and fetch results faster than it does?

3
  • Depends. What are your tables and indexes? That makes a big difference. Commented Mar 16, 2011 at 11:04
  • Tells out what the query shall do. Commented Mar 16, 2011 at 11:12
  • Please give DB structures (tables/indexes) and execution plan (explain) for your query Commented Mar 16, 2011 at 11:25

2 Answers 2

3

Replace your nested selects by JOIN clauses. Take this for instance:

AND a.FundIDRecv IN
(
    SELECT FundIDRecv
    FROM tbl_FundSubscriptions
    WHERE FundIDSend IN (
        SELECT FundID
        FROM tbl_FundStatic
        WHERE UserID = '14'
    )
)

Why not just join tbl_FundStatic to the outer query like this

FROM tbl_FundSubscriptions a
JOIN tbl_FundStatic s ON (a.FundIDSend = s.FundID)
WHERE s.UserID = '14'

Of course, I don't know if this is still correct, but it'll give you an idea. Also, you should avoid nested selects in the SELECT clause itself. It's better to join tbl_FundStatic and then select fields from it

Sign up to request clarification or add additional context in comments.

2 Comments

How can I do that? Can you provide more information on how to do that? I have no idea about it
Yeah i will try out that. Thanks for the input though.
3

Untested but this will give you a go:

SELECT a.fundidrecv, 
       a.subscribedt, 
       b.fundname, 
       SUM(b.priceperweek) AS price 
FROM   tbl_fundsubscriptions a 
       JOIN tbl_hive b 
         ON a.fundidrecv = b.fundid 
       JOIN tbl_fundsubscriptions fs 
         ON fs.fundidrecv = a.fundidrecv 
       JOIN tbl_fundstatic fst 
         ON fst.fundid = fs.fundidsend 
            AND fst.userid = '14' 
WHERE  a.subscribedt >= SUBDATE(Curdate(), INTERVAL Weekday(Curdate()) DAY) 
GROUP  BY a.fundidrecv 

You need to add indexes on the following columns:

  • (a.fundidrecv,a.subscribedt)
  • (b.fundid)
  • (fs.fundidrecv)
  • (fst.fundid,fst.userid)

3 Comments

I will check it soon and get back to you.
@Pentuim10 : Thanks for the input. I appreciate your effort but SUM(b.priceperweek) is showing wrong data. It does not show the expected result.
Update your original question, and describe what the query should do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.