简体   繁体   English

有效地从一个表中删除与另一个表不匹配的行 [MySQL]

[英]Efficiently deleting rows from one table where not matching another [MySQL]

So, I have two tables, users and points.所以,我有两个表,用户和点。 A lot of users no longer have points (literally and by table) -- so I wish to delete them.很多用户不再有积分(字面意思和表格)——所以我希望删除它们。 I try and achieve this by doing this, but it basically times out (about one hundred thousand rows in both tables).我尝试通过这样做来实现这一点,但它基本上会超时(两个表中大约有十万行)。 I'm doing select rather than delete because I want to verify the data before issuing the same query as a delete:我正在做 select 而不是删除,因为我想在发出与删除相同的查询之前验证数据:

SELECT WWW t1 
FROM users t1
JOIN points t2 ON t1.id != t2.user_id;

Both tables have an ID that matches each other.两个表都有一个相互匹配的 ID。 If a user has a point, there will be a matching ID.如果用户有一个点,就会有一个匹配的 ID。 If there is no longer a point for that user, there is no matching ID.如果该用户不再有积分,则没有匹配的 ID。 Therefor, .= therorhetically should delete all the no longer needed users.因此,.=理论上应该删除所有不再需要的用户。

How can I do this without timing out my database?如何在不使数据库超时的情况下做到这一点?

I would recommend not exists :我建议not exists

delete from users
where not exists (select 1 from points p where p.user_id = users.id)

This should be an efficient option, if you have the right index in place, that is: points(user_id) .如果您有正确的索引,这应该是一个有效的选择,即: points(user_id)

On the other hand, if you are deleting a great proportion of the table, it might be simpler to move that rows that you want to keep to a temporary table, then truncate and recreate the table:另一方面,如果您要删除表的很大一部分,将要保留的行移动到临时表中,然后截断并重新创建表可能会更简单:

create table tmp_users as 
select u.*
from users
where exists(select 1 from points p where p.user_id = u.id)

truncate table users;  -- back it up first!

insert into users select * from tmp_users;

drop table tmp_users;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果MySQL中的另一个表中存在行,则从一个表中删除行 - Deleting rows from one table if row exists in another table in MySQL 从一个表删除行并添加到另一个表 - Deleting rows from one table and adding to another 将数据从一个mySQL表字段移动到另一个表字段,其中每个表都有一个匹配的ID - Moving data from one mySQL Table field to another Table field where each table has a matching id MySQL:从另一个表中具有匹配键的行更新表中的行 - MySQL: Update rows in table, from rows with matching key in another table 如何在一个表中对MYSQL中的一系列行求和,匹配另一个表中的信息以获得结果 - How to Sum a series of rows in MYSQL from one Table, matching the info from another to get the result MySQL:根据条件删除行,并带有来自另一个表的数据,并且不进行联接 - MySQL: deleting rows based on a condition with data from another table and NO JOIN MySQL有效地将所有记录从一张表复制到另一张表 - MySQL efficiently copy all records from one table to another Mysql查询选择其中一个表中的总和与另一个表中的行总和进行比较 - Mysql Query select where the sum from one table is compared to the sum of rows in another table MySQL:如何从一个表中选择和显示所有行,并计算另一个表中where子句的总和? - MySQL: How to select and display ALL rows from one table, and calculate the sum of a where clause on another table? 将一个MySQL表中的行分组到另一个MySQL表中 - Grouping rows from one MySQL table in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM