如何使用任何类型的连接或子查询或嵌套查询来减少此查询运行时间。如何使用mysql减少嵌套查询时间
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
使用,而不是内部联接的查询和左的连接,而不是不.....这将减少查询时间,因为在查询需要太多时间 –
'从产品选择 p.id, p.name AS名称, SUM(ss.instock)作为研究所 为p LEFT JOIN(SELECT s.id,s.pid,CASE COUNT(crt.salvage_part) WHEN 1 THEN 0 ELSE s.quantity END AS instock FR OM sales as s LEFT JOIN invoice as i ON i.invoice_id = s.sales_id LEFT JOIN crt ON crt.cc_id = i.cc_id AND crt.st ='1' GROUP BY s.id)as ss ON ss。 pid = p.id 其中p.hc ='1' GROUP BY p.id order by p.name DESC' **试过这个太,但不能减少它的运行时间。** –
这个问题是无意义的显然,这个查询会产生一个语法错误。 – Strawberry