这是三个表,order,order_record,pay,附近有2300000条记录。
在给1个order_id时,pay表中会有超过1条记录,所以我需要使用DISTINCT来删除重复的结果
现在我需要从order_id上的那三个表连接中获取不同的数据,下面是示例查询sql:
SELECT
DISTINCT (a.order_id)
a.order_id,a.user_id
b.boss_order_id,
c.pay_id,
FROM order a
LEFT JOIN order_record b ON a.order_id = b.order_id AND b.is_delete IN (0,1)
LEFT JOIN pay c ON a.order_id = c.order_id AND c.is_delete =0 WHERE 1=1 AND a.is_delete IN (0,1)
ORDER BY a.id DESC LIMIT 0, 10
此查询将花费大量时间。 然后我改为使用“GROUP BY”:
SELECT
a.order_id,a.user_id
b.boss_order_id,
c.pay_id,
FROM order a
LEFT JOIN order_record b ON a.order_id = b.order_id AND b.is_delete IN (0,1)
LEFT JOIN pay c ON a.order_id = c.order_id AND c.is_delete =0 WHERE 1=1 AND a.is_delete IN (0,1)
GROUP BY a.order_id
ORDER BY a.id DESC LIMIT 0, 10
这次查询需要122秒。
有没有更快的实施方式?
答案 0 :(得分:0)
您正在使用left join
。因此,您可以这样做:
SELECT o.order_id, o.user_id, orr.boss_order_id, p.pay_id,
FROM (SELECT o.*
FROM order o
WHERE o.is_delete IN (0, 1)
ORDER BY o.id DESC
LIMIT 10
) o LEFT JOIN
order_record orr
ON o.order_id = orr.order_id AND
orr.is_delete IN (0, 1) LEFT JOIN
pay p
ON o.order_id = p.order_id AND
p.is_delete = 0
WHERE 1=1 AND o.is_delete IN (0, 1)
GROUP BY o.order_id
ORDER BY o.id DESC
LIMIT 0, 10
您错误地使用了GROUP BY
,因为您在SELECT
中的非聚合列不在GROUP BY
中。
答案 1 :(得分:0)
另一种方法是让where子句完成大部分工作:
select ...
from order
left join order_using using (order_id)
...
where
order.order_id < (select max(order_id) from orders order by order_id limit 10) ...
limit 10
最终限制10很奇怪,但是如果您放弃该组,您可能会从订单中获得部分记录。即你可能想放弃它,并且只是放一个限价订单表。通过组,您将获得表b和c中的随机数据,除非您使用聚合函数告诉mysql您想要哪个行值。