我想减少SQL请求的处理时间(实际上它运行10分钟......) 我认为问题来自嵌套的SQL查询。 (对不起我的英语,我是法国学生)
SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, `go.amount`
FROM globe_statistique
JOIN globe_customers ON `gst.codeAP21`=`gc.codeAP21`
JOIN globe_orders ON `gc.ID`=`go.FK_ID_customers`
WHERE `gst.page` = 'send_order'
AND `gst.date` = FROM_UNIXTIME(`go.date`,'%%Y-%%m-%%d')
UNION
SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, '-'
FROM globe_statistique
WHERE `gst.page` NOT LIKE 'send_order' "
AND (`gst.codeAP21`,`gst.date`) NOT IN
( SELECT `gst.codeAP21`,`gst.date` FROM globe_statistique
WHERE `gst.page`='send_order');
由于
答案 0 :(得分:0)
试试这个:
SELECT DISTINCT `gst.codeAP21`, `gst.email`, `gst.date`, `go.amount`
FROM globe_statistique
JOIN globe_customers ON `gst.codeAP21`=`gc.codeAP21`
JOIN globe_orders ON `gc.ID`=`go.FK_ID_customers`
WHERE `gst.page` = 'send_order'
AND `gst.date` = FROM_UNIXTIME(`go.date`,'%%Y-%%m-%%d')
UNION
SELECT DISTINCT t1.`gst.codeAP21`, t1.`gst.email`, t1.`gst.date`, '-'
FROM globe_statistique t1
left join globe_statistique t2 on t1.gst.page =t2.gst.page and t1.gst.date =t2.gst.date and t2.gst.page =send_order
WHERE `gst.page` <> 'send_order' AND t2.gst.date is null
但我建议重命名你的列名并删除这些点。
还可以使用EXPLAIN
找出查询速度慢的原因并添加正确的索引
答案 1 :(得分:0)
尽量避免使用不同的。为此,应使用UNION ALL。最后的分组给出了相同的结果:
select codeAP21, email, date, amount
from ( --> your query without distinct but with UNION ALL <-- )
group by codeAP21, email, date, amount
请参阅:Huge performance difference when using group by vs distinct