我有三张桌子,我正在使用下面的代码加入他们
orders
有30k行orders_details
有超过100k行services
有4行当我在Fish Database.net
执行下面的脚本时,在显示结果之前需要花费13分钟才能运行!!!
select
`o`.`created` AS `created`,
sum(`o`.`total`) AS `total`,
sum(`o`.`paid`) AS `paid`,
`od`.`service_id` AS `service_id`,
`s`.`name` AS `grp`
from ((`orders` `o`
left join `orders_details` `od` on
(
(`od`.`order_id` = `o`.`id`)
))
left join `services` `s` on
(
(`s`.`id` = `od`.`service_id`)
))
group by
`od`.`service_id`,
`o`.`created`,
`s`.`name`
order by
`o`.`created`
id
的所有列名都是整数主键列这是解释结果
EDITS
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE o ALL NULL NULL NULL NULL 23558 Using temporary; Using filesort
1 SIMPLE od ALL NULL NULL NULL NULL 40304
1 SIMPLE s eq_ref PRIMARY PRIMARY 4 mydb.od.service_id 1
我可以改善/找到瓶颈吗?
答案 0 :(得分:2)
使用(unuseful)nested()会降低性能
尝试删除unseful()
确保你在od.order_id,o.id,s.id,od.service_id
select
o.created AS created,
sum(o.total) AS total,
sum(o.paid) AS paid,
od.service_id AS service_id,
s.name AS grp
from orders o
left join orders_details od on od.order_id = o.id
left join services s on s.id = od.service_id
group by
od.service_id,
o.created,
s.name
order by
o.created
为了更好的阅读,我已经删除了背景...你没有重复的单词od多字列名,所以不应该需要)
答案 1 :(得分:0)
有一些步骤可以找到sql脚本中的瓶颈。