[英]deleting millions of records in rails
Our RoR 4 app has millions of records which needs to be deleted on a regular basis.我们的 RoR 4 应用程序有数百万条记录需要定期删除。 Currently the deletion happens as a backgound job:
目前,删除是作为后台作业进行的:
while fruit.apples.count > 0
# create the sql query to delete 1000 feeds from the channel
sql = "DELETE FROM apples WHERE fruit_id=#{fruit_id} LIMIT 1000"
# execute the sql query
ActiveRecord::Base.connection.execute(sql)
# wait a bit before the next delete
sleep 0.1
end
because I am doing a count every few seconds, it has spiked the mysql server's cpu time.因为我每隔几秒钟就进行一次计数,它已经增加了 mysql 服务器的 CPU 时间。 So was wondering if I could delete a million records using delete_all/destroy_all or are there any better way of achieving this.
所以想知道我是否可以使用 delete_all/destroy_all 删除一百万条记录,或者有没有更好的方法来实现这一点。
You can use TRUNCATE instead of DELETE.您可以使用 TRUNCATE 而不是 DELETE。 TRUNCATE deletes the whole table and recreates an empty table.
TRUNCATE 删除整个表并重新创建一个空表。 So this is much faster.
所以这要快得多。 TRUNCATE also resets the value of AUTO INCREMENT field in the table
TRUNCATE 还会重置表中 AUTO INCREMENT 字段的值
TRUNCATE TABLE yourTable;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.