mysql in 子句 vs 一次提交多次删除

发布于 2024-11-07 18:11:03 字数 262 浏览 0 评论 0原文

我有大约 100K 条记录,我必须运行以下查询:

delete from users where name in #{String}

其中字符串可以是这种形式的 100K 字符串:Joe,Kate 等。

为了提高性能,最好运行上述语句或在循环中删除一条记录session.commit();到底是什么?

已编辑

每个值只能有一条记录

I have about 100K records that I have to run for the following query:

delete from users where name in #{String}

where the string could be 100K strings of this form: Joe,Kate etc.

For performance is it better to run the above statement or delete one record in a loop with one session.commit(); in the end?

EDITED

There could be only one record for each value

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

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

发布评论

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

评论(2

追我者格杀勿论 2024-11-14 18:11:03

如果您可以创建要运行的批量查询,那么将其分成多个批次很可能是最快的:

delete from users where name in ('name1','name2','name3',.....'nameX');

delete from users where name in ('nameX+1','nameX+2','nameX+3',.....'nameX+X');

等等。

如果表中已有名称,您可以这样做:

delete from users where name in (select name from table_with_names_to_be_deleted)

If you can create batches of queries to run then breaking it up into batches would most likely be the fastest:

delete from users where name in ('name1','name2','name3',.....'nameX');

delete from users where name in ('nameX+1','nameX+2','nameX+3',.....'nameX+X');

etc..

If you have the names in a table already you can just do this:

delete from users where name in (select name from table_with_names_to_be_deleted)
两相知 2024-11-14 18:11:03

最好不要使用这样的语句!
查询优化器将花一天的时间来解析这样的查询。我建议使用一些临时表来连接,或者要删除的记录有共同点的其他 WHERE 子句?

It would be better not to use such statements!
The query optimizer will have a field day parsing such a query. I suggest using some temporary table to join against, or some other WHERE-clause that the records to be deleted have in common?

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