查询需要很长时间
Query taking a very long time
我对 return 不在 message_log table
中的用户 ID 进行了以下查询
select * from likes where userid not in(select to_id from message_log)
我在 likes table 的 userid 列上有一个索引,在 message_log table 的 to_id 列上有一个索引,但是索引没有被根据 EXPLAIN 使用。我的查询有问题吗?我的查询已经 运行 了 20 分钟,但仍然没有结果。
select *
from likes
left join message_log ml on ml.to_id=likes.userid
where ml.to_id is null
尝试使用 LEFT JOIN 进行查询,只保留没有消息的用户 ID
你可以试试这个
select * from likes lk where not exists (select 1 from message_log where to_id = lk.userid )
您应该考虑的第一件事是将子查询转换为连接。像这样:select like.col1, like.col2
from likes like
left join message_log mlog
on like.userid = mlog.to_id
where mlog.to_id is null
虽然优化器很可能会为你做这件事。
您应该尝试的另一件事是从 select 子句中删除星号(如在我的示例中),因为它可能会影响优化器使用的索引。
我对 return 不在 message_log table
中的用户 ID 进行了以下查询select * from likes where userid not in(select to_id from message_log)
我在 likes table 的 userid 列上有一个索引,在 message_log table 的 to_id 列上有一个索引,但是索引没有被根据 EXPLAIN 使用。我的查询有问题吗?我的查询已经 运行 了 20 分钟,但仍然没有结果。
select *
from likes
left join message_log ml on ml.to_id=likes.userid
where ml.to_id is null
尝试使用 LEFT JOIN 进行查询,只保留没有消息的用户 ID
你可以试试这个
select * from likes lk where not exists (select 1 from message_log where to_id = lk.userid )
您应该考虑的第一件事是将子查询转换为连接。像这样:select like.col1, like.col2
from likes like
left join message_log mlog
on like.userid = mlog.to_id
where mlog.to_id is null
虽然优化器很可能会为你做这件事。
您应该尝试的另一件事是从 select 子句中删除星号(如在我的示例中),因为它可能会影响优化器使用的索引。