简体   繁体   English

MySQL使用LIMIT或WHERE删除记录

[英]MySQL delete records using LIMIT or using WHERE

I'm trying to find the best way to delete millions of records in MySQL DB. 我试图找到删除MySQL DB中数百万条记录的最佳方法。 I have a table with a PK on ID and an index on 'date' column and my delete queries are like: 我有一个带有ID PK和“ date”列索引的表,我的删除查询如下:

DELETE FROM table WHERE date < '<today - 6 months>';

It's generating a lot of delay on the slave. 它在从站上产生大量延迟。

I had 2 options: 我有2个选择:

DELETE FROM table WHERE date < '<today - 6 months>' LIMIT 1000;

or 要么

Include further Indexing or using PK for deleting. 包括进一步的索引编制或使用PK进行删除。

I would like to hear your opinions. 我想听听您的意见。 If using LIMIT won't change the workload or if using PK (in combination with LIMIT) is better. 如果使用LIMIT不会改变工作量,或者使用PK(与LIMIT结合使用)更好。

The best way to delete lots of rows, especially in a replication setup, is to walk through the table via the PRIMARY KEY in chunks of 1000 rows. 删除大量行的最佳方法(尤其是在复制设置中)是通过PRIMARY KEY在1000行的块中遍历表。

See this for details. 请参阅了解详情。

If this is a recurring task, then a 'time series' PARTITIONing is even better. 如果这是一项经常性的任务,那么“时间序列” PARTITIONing会更好。 (Though it won't help until you set up partitioning.) See this . (虽然它不会帮助,直到你设置了分区功能。)请参阅

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM