如何使用任何类型的连接或子查询或嵌套查询减少此查询运行时间。
select
`p`.`id`,
`p`.`name`,
SUM(s.quantity) as inst,
from `product` as `p`
LEFT JOIN sales as s ON s.pid=p.`id` AND s.sales_id IN (SELECT invoice.invoice_id FROM invoice WHERE invoice.ccid NOT IN (SELECT ccid FROM ticket WHERE st='1'))
where `p`.`hc` = '1'
GROUP BY `p`.`id`
order by `p`.`name` DESC
答案 0 :(得分:1)
在故障单的st上添加索引,并在发票的ccid和sales的sales_id上添加索引。
在查询之前尝试使用Explain子句是否在possible_keys列输出中使用了创建的索引。
答案 1 :(得分:0)
最后我来找一个解决方案&它有效
select
s.pid,
SUM(s.quantity) AS squan
from invoice as i
LEFT JOIN sales as s ON s.sales_id=i.invoice_id
LEFT JOIN crt ON crt.cc_id=i.cc_id AND crt.ch='0'
WHERE
crt.ch='0'
AND
(i.cc_id!='' OR i.cc_id!='0')
GROUP BY i.invoice_id