删除两个MySQL大表中的匹配记录

发布于 2024-11-05 07:59:28 字数 459 浏览 0 评论 0原文

我有 2 个 MySQL 表:t1 和 t2,分别是 1M 和 15M 行。表 t1 只有 1 个字段:“tel”,而 t2 有很多字段,但也有一个“tel”字段。我想要做的非常简单:删除 t1 中存在于 t2 中的所有行:

DELETE FROM t1 WHERE t1.tel IN (SELECT tel FROM t2)

问题是这个查询似乎没有完成。我让它在 8 核 Xeon 工作站上运行,两天后我决定停止它并寻找替代方案。我还尝试创建一个新表(tt1)并使用 LEFT OUTER JOIN 仅插入 t2 中不在 t1 中的行,但似乎花费了相同的时间。 t1 中的“tel”字段是主键,它是 t2 中的唯一键(我还尝试了 CREATE INDEX t2tel ON t2(tel) 但没有帮助)。

有什么建议吗?我正在考虑编写一个 C# 程序来将两个表加载到有序数组或哈希中,并通过代码来完成...提前致谢。

I have 2 MySQL tables: t1 and t2 which are 1M and 15M rows respectively. Table t1 only has 1 field: 'tel' and t2 has a lot of fields but also has a 'tel' field. What I want to do is quite simple: delete all the rows in t1 that exists in t2:

DELETE FROM t1 WHERE t1.tel IN (SELECT tel FROM t2)

The problem is that this query seems not to finish. I let it running in an 8 core Xeon workstation and after 2 days I decided to stop it and look for alternatives. I also tried to create a new table (tt1) and use LEFT OUTER JOIN to insert only the rows from t2 that are not in t1 but it seems to take the same amount of time. The 'tel' field in t1 is primary key and it's unique key in t2 (I also tried a CREATE INDEX t2tel ON t2(tel) but it didn't help).

Any suggestion? I'm considering writing a C# program to load both tables into ordered arrays or hashes and do it by code... Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-11-12 07:59:28
DELETE t1 
  FROM t1
 INNER 
  JOIN t2 
    ON t1.tel = t2.tel;

这应该比使用子查询快得多。如果尚未针对大型表优化 MySQL 实例,您可以采取很多步骤来优化它。充足的密钥缓冲区是一个良好的开端。还有很多其他步骤,您最好使用 Google 进行 MySQL 性能调整。

DELETE t1 
  FROM t1
 INNER 
  JOIN t2 
    ON t1.tel = t2.tel;

That should be significantly faster than using a subquery. There are quite a lot of steps you could take to optimize your MySQL instance, if it's not already optimized, for large tables. Ample key buffers are a good start. There are plenty of other steps, you'd be best off hitting the Google for MySQL performance tuning.

無處可尋 2024-11-12 07:59:28

你遇到的性能问题我认为这是因为你在查询中使用查询,你最好使用联接,我用2个简单的小表进行了测试,我使用了这个:

DELETE t1 FROM t1 inner join t2 on t1.id = t2.t1_id;

它对我有用,我希望这可以帮助你。

the problem you have with performance I think it is because you are using a query inside a query, you better use joins, I made a test with 2 simple and small tables, and I used this:

DELETE t1 FROM t1 inner join t2 on t1.id = t2.t1_id;

It worked for me, I hope this could help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文