gpt4 book ai didi

mysql - 查找没有关系的所有记录的查询速度非常慢

转载 作者:行者123 更新时间:2023-11-29 10:10:25 25 4
gpt4 key购买 nike

我正在尝试查找所有没有有任何订单用户

到目前为止,我已尝试以下操作:

select * from `users` where not exists 
(select * from `orders` where `users`.`id` = `orders`.`user_id`)

我还尝试了以下方法:

select users.*, count(distinct orders.reference) as orders_count from `users` 
left join `orders` on `users`.`id` = `orders`.`user_id`
group by `users`.`id` having orders_count = 0

但是,两者的查询运行速度都非常慢。约有5000名客户和30,000+订单。有没有更有效的方法来做到这一点?

最佳答案

您需要将子查询限制为仅查看 user_id。还要确保 user_id 已建立索引。

Alter table orders add index(user_id)

Select * from users where id NOT IN(select user_id from
orders)

关于mysql - 查找没有关系的所有记录的查询速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51062428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com