我所試圖做的是創造100個查詢 將做以下檢查:以兩個SQL查詢到一個查詢工作太慢
我已經得到了無論是在「yesnotmp」數據庫2個表。 一個叫做msg_t
它包含了所有的消息,它有很多的列。我在意的是這兩個查詢中的那兩個是send_time
,status
,phone
,sushi_sub_id
。
在這裏,我得到了昨天的所有電話,sushi_sub_id記錄。 (和它的作品)
SELECT phone, sushi_sub_id FROM `yesnotmp`.`msg`
LEFT JOIN `yesnotmp`.`msg_t` ON (`msg`.`id`=`msg_t`.`msg_id`)
WHERE `msg_t`.`send_time` BETWEEN '2013-06-02' AND '2013-06-03'
AND `msg_t`.`status` = 'Failure.Provider.Connection'
我所試圖做的是去檢查retry
表多少phone
插入具有相同sushi_sub_id
多少不是。 (此查詢需要很長時間)
SELECT
Sum(CASE WHEN (SELECT Count(*) FROM `retry` AS `rty` WHERE `rty`.`phone` = `msgs`.`phone` AND `rty`.`sushi_subscription_id`=`msgs`.`sushi_sub_id`) > 0 THEN 1 ELSE 0 END) AS `In_Retry`,
Sum(CASE WHEN (SELECT Count(*) FROM `retry` AS `rty` WHERE `rty`.`phone` = `msgs`.`phone` AND `rty`.`sushi_subscription_id`=`msgs`.`sushi_sub_id`) > 0 THEN 0 ELSE 1 END) AS `Not_In_Retry`
FROM
(SELECT phone, sushi_sub_id FROM `yesnotmp`.`msg`
LEFT JOIN `yesnotmp`.`msg_t` ON (`msg`.`id`=`msg_t`.`msg_id`)
WHERE `msg_t`.`send_time` BETWEEN '2013-06-02' AND '2013-06-03'
AND `msg_t`.`status` = 'Failure.Provider.Connection') AS `msgs`
你說'它有效',但是WHERE子句有效地將你的LEFT [outer] JOIN作爲[inner] JOIN進行渲染,所以你不妨寫下它以開始... SELECT m.phone , m.sushi_sub_id 味精米 JOIN msg_t噸 ON t.msg_id = m.id WHERE t.send_time BETWEEN '2013年6月2日' 與 '2013年6月3日' AND t.status =「失效。 Provider.Connection「; – Strawberry