24小时后自动从数据库中删除行 [英] Delete row from database automatically after 24 hours

查看:143
本文介绍了24小时后自动从数据库中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建类似Snapchat的东西,它将在php/mysql中24小时后自动删除行. 我已经阅读过有关cron作业的信息,但是它会降低应用程序的速度,所以我想知道是否还有其他方法可以做到这一点?

I want to create something like Snapchat that will automatically delete row after 24 hours in php/mysql. I have read about cron jobs,but it slows down apps,so i was wondering if there is other way to do it?

推荐答案

您正在查看的内容实际上是这样的:

What you're looking at is something effectively like this:

DELETE FROM snaps WHERE created_at<=DATE_SUB(NOW(), INTERVAL 1 DAY)

只要您填充created_at并为其编制索引,这应该会很快运行.如果您有很多记录,并且我的意思是说超过1亿,则需要将它们隔开:

So long as you populate created_at and index it, this should run fairly quickly. If you have lots of records, and by that I mean over 100 million, you'll need to space that out:

DELETE FROM snaps ... LIMIT 10000

您可以在其中以10K的代码块或其他最有效的方法来完成此任务.

Where you can do it in chunks of 10K or whatever works best.

这篇关于24小时后自动从数据库中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆