0

I've got millions of entries in my DB.

and because im using 3!! inner joins and put large range of dates this query takes really long like up to few minutes.

Is there a way to Improve this query and still get the same Data?

This is my query:

SELECT 
    subscriptions.service_id, 
    service_send_type.usage, 
    service_send_type.price, SUM(IF(msg_t.status LIKE 'Success%', 1, 0)) AS s, 
    COUNT(1) AS t 
FROM (`subscriptions`) 
    INNER JOIN 
        `msg_t` ON `subscriptions`.`phone` = `msg_t`.`phone` AND subscriptions.id = msg_t.sub_id 
    INNER JOIN
        `msg` ON `msg`.`id` = `msg_t`.`msg_id` 
    INNER JOIN `service_send_type` ON `msg`.`service_id` = `service_send_type`.`service_id` 
        AND msg.sushi_service_id = service_send_type.sushi_service_id 
        AND msg.send_type = service_send_type.name 
        AND msg.service_id = subscriptions.service_id 

    WHERE 
        `subscriptions`.`service_id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) 
        AND 
        `subscriptions`.`added` >= '2013-01-28 00:00:00' 
        AND 
        `subscriptions`.`added` <= '2013-01-28 23:59:59' 
        AND 
        `msg_t`.`send_time` >= '2013-01-28 00:00:00' 
        AND 
        `msg_t`.`send_time` <= '2013-01-28 23:59:59' 

    GROUP BY 
        `subscriptions`.`service_id`, 
        `service_send_type`.`usage`, 
        `service_send_type`.`price`
2
  • 1
    Unless you provide more information, then no. Feel free to take a look at what info people provide when asking for MySQL query performance improvements. Commented Jan 30, 2013 at 8:52
  • Consider indexes on your date columns. Commented Aug 14, 2013 at 2:45

2 Answers 2

2

Do you always need to have the IN query? In your query, the IN clause can be replaced by subscriptions.service_id < 17 and that will make a huge difference. IN queries are suitable when you have a list of discrete values.

Also look at the indexing on the tables. You should have index set on columns in the where condition and group by clauses.

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

1 Comment

@DanRevah: What change gave you the most significant improvement?
2

You can try to optimize your query by analyzing the Explain plan, including index optimizing which can be very helpful if you still dont have idnexes on your tables. regarding the statement optimizing take a look here.

Hope this helps you further

2 Comments

@ypercube your right its actually only a syntax issue, just learned something, thank you.
I prefer c >= '2013-01-28' AND c < '2013-01-29 because it's easier to code, works with any datetime type (date, datetime, timestamp) and does not break if the column's datatype has higher than second granularity.

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.