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`