我的数据库中有数百万个条目。
因为我用的是 3!!内部连接并放置大范围的日期,这个查询需要很长时间,比如几分钟。
有没有办法改进这个查询并仍然获得相同的数据?
这是我的查询:
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`