[英]What is the best way to execute a bulk update of all records in a large table
[英]What is the best way to filter out records from a large dataset
我在一个表中有大约500,000个用户记录,另一个表包含被其他用户专门阻止的用户记录(即哪个用户阻止了哪个用户)。 我使用以下查询来过滤当前用户的用户记录,该用户记录不包含被阻止的表中的被阻止用户。
SELECT * from demouser where id not in (select blockedid from demoblock where userid = 1 )
它很慢,大约需要2秒才能执行查询。 是否有更有效的方法来过滤记录而不将500,000个用户记录与阻止表进行比较以从结果中删除被阻止的用户?
分别在id
和userid
列上创建索引,因此子查询和查询都将运行得更快。
另外,我建议稍作修改:
select * from demouser du
where not exists(select 1 from demouser
where blockedid = du.id
and userid = 1)
我会写这个not exists
:
select du.*
from demouser du
where not exists (select 1
from demoblock db
where db.blockedid = du.id and
db.userid = 1
);
对于此查询,您需要有关demoblock(blockedid, userid)
的索引demoblock(blockedid, userid)
。
假设您的demoblock
表不包含大量数据,您可以尝试使用INNER JOIN
。 以下示例查询:
SELECT * FROM demouser INNER JOIN demoblock ON demouser.id=demoblock.id WHERE demoblock.id=1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.