How would you expire (or update) a MySQL record with precision to the expiry time?

怎甘沉沦 提交于 2019-12-06 13:32:25

Option 3:

At insert, have a trigger that creates a dynamic event specific to the record that will expire it precisely when it should expire. After the expiry, delete the event. I feel like this would be a great method of doing it, but I'm not too sure if having so many events running at once would impact on the performance of the database (imagine a database that has even 60 inserts an hour - that's 60 events all running simultaneously for just one hour. Over time, depending on how long the expiration is, this would add up).

If you know the expiry time on insert just put it in the table..

  • library_record - id, ..., create_at, expire_at

And query live records with the condition:

expire_at > NOW()

Same with publishing:

  • library_record - id, ..., create_at, publish_at, expire_at

Where:

publish_at <= NOW() AND expire_at > NOW()

You can set publish_at = create_at for immediate publication or just drop create_at if you don't need it.

Each of these, with the correct indexing, will have performance comparable to an is_live = 1 flag in the table and save you a lot of event related headache.

Also you will be able to see exactly why a record isn't live and when it expired/should be published easily. You can also query things such as records that expire soon and send reminders with ease.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!