在cassandra数据库中,有一个叫做TTL的功能,即插入一条记录时,可以指定某一字段对应的TTL值,比如30s,那么当TTL到达30s后该条记录就会被自动删除。目前MySQL并未直接提供TTL的功能,但是我们可以通过event schedule功能实现。

创建一张测试表

CREATE TABLE `access_token` (
`token_id` varchar(100) COLLATE utf8_bin NOT NULL DEFAULT '',
`created_timestamp` bigint(20) DEFAULT NULL,
`expired_timestamp` bigint(20) DEFAULT NULL,
`grant_type` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`refresh_token` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`scope` varchar(200) COLLATE utf8_bin DEFAULT NULL,
`user_info` varchar(200) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`token_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

首先假设一些记录的expired_timestamp过期时间是“2020-12-31 23:59:59”,将该时间转换成unix时间戳,并乘以1000转换成毫秒存储,其他部分记录的expired_timestamp过期时间设置为当前时间

select unix_timestamp('2020-12-31 23:59:59')*1000;
+--------------------------------------------+
| unix_timestamp('2020-12-31 23:59:59')*1000 |
+--------------------------------------------+
| 1609430399000 |
+--------------------------------------------+
1 row in set (0.00 sec)
select unix_timestamp(current_timestamp)*1000;
+----------------------------------------+
| unix_timestamp(current_timestamp)*1000 |
+----------------------------------------+
| 1576319276000 |
+----------------------------------------+
1 row in set (0.00 sec)

插入测试数据

insert into access_token value('9T8F9EX42G9OXHPIDM98YBK74','','','mannual','6JL351QA38Z9ONOL9PWJ64A4U','test','david');    #该条测试数据的expired_timestamp是上述的 2020-12-31 23:59:59
insert into access_token value('AG8FLP3JQKGNCRZY0OPVTYV2W','','','auto','D7QOBDZPBKEP7TVWUG10XJA0S','ceshi','brain');      #该条测试数据的expired_timestamp的当前时间current_timestamp

查看测试数据

select * from access_token;
+---------------------------+-------------------+-------------------+------------+---------------------------+-------+-----------+
| token_id | created_timestamp | expired_timestamp | grant_type | refresh_token | scope | user_info |
+---------------------------+-------------------+-------------------+------------+---------------------------+-------+-----------+
| 9T8F9EX42G9OXHPIDM98YBK74 | 1576319276000 | 1609430399000 | mannual | 6JL351QA38Z9ONOL9PWJ64A4U | test | david |
| AG8FLP3JQKGNCRZY0OPVTYV2W | 1576319276000 | 1576319276000 | auto | D7QOBDZPBKEP7TVWUG10XJA0S | ceshi | brain |
+---------------------------+-------------------+-------------------+------------+---------------------------+-------+-----------+
2 rows in set (0.00 sec)

将数据转换成timestamp显示

select token_id, from_unixtime(created_timestamp/1000) as created_timestamp, from_unixtime(expired_timestamp/1000) as expired_timestamp, grant_type, refresh_token , scope, user_info from access_token;
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| token_id | created_timestamp | expired_timestamp | grant_type | refresh_token | scope | user_info |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| 9T8F9EX42G9OXHPIDM98YBK74 | 2019-12-14 18:27:56.0000 | 2020-12-31 23:59:59.0000 | mannual | 6JL351QA38Z9ONOL9PWJ64A4U | test | david |
| AG8FLP3JQKGNCRZY0OPVTYV2W | 2019-12-14 18:27:56.0000 | 2019-12-14 18:27:56.0000 | auto | D7QOBDZPBKEP7TVWUG10XJA0S | ceshi | brain |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
2 rows in set (0.01 sec)

创建event schedule,具体含义可参考https://dev.mysql.com/doc/refman/5.7/en/create-event.html

create event testdb.delete_access_token_expired_record
on schedule every 2 minute
do delete from testdb.access_token where expired_timestamp < unix_timestamp(current_timestamp)*1000 ;

查看该event信息

select * from information_schema.events\G
*************************** 1. row ***************************
EVENT_CATALOG: def
EVENT_SCHEMA: testdb
EVENT_NAME: delete_access_token_expired_record
DEFINER: root@localhost
TIME_ZONE: SYSTEM
EVENT_BODY: SQL
EVENT_DEFINITION: delete from testdb.access_token where expired_timestamp < unix_timestamp(current_timestamp)*1000
EVENT_TYPE: RECURRING
EXECUTE_AT: NULL
INTERVAL_VALUE: 2
INTERVAL_FIELD: MINUTE
SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
STARTS: 2019-12-14 18:38:59
ENDS: NULL
STATUS: ENABLED
ON_COMPLETION: NOT PRESERVE
CREATED: 2019-12-14 18:38:59
LAST_ALTERED: 2019-12-14 18:38:59
LAST_EXECUTED: 2019-12-14 10:38:59
EVENT_COMMENT:
ORIGINATOR: 543306
CHARACTER_SET_CLIENT: utf8mb4
COLLATION_CONNECTION: utf8mb4_0900_ai_ci
DATABASE_COLLATION: utf8mb4_0900_ai_ci
1 row in set (0.01 sec)

该event每两分钟运行一次,如果access_token表中字段expired_timestamp的时间戳小于当前时间戳,说明该条记录已经过期,可以删除。

再次查看access_token表时,发现过期数据已经被删除

select token_id, from_unixtime(created_timestamp/1000) as created_timestamp, from_unixtime(expired_timestamp/1000) as expired_timestamp, grant_type, refresh_token , scope, user_info from access_token;
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| token_id | created_timestamp | expired_timestamp | grant_type | refresh_token | scope | user_info |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| 9T8F9EX42G9OXHPIDM98YBK74 | 2019-12-14 18:27:56.0000 | 2020-12-31 23:59:59.0000 | mannual | 6JL351QA38Z9ONOL9PWJ64A4U | test | david |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
1 row in set (0.00 sec)

再次插入两条数据,一条数据的expired_timestamp为当前时间戳,一条数据的expired_timestamp为5分钟后

insert into access_token value('4MXCXCXQRZRY1FVYAO9DG99F9','','','mannual','EN525M7AW448RBR3386ODM9T3','test','tim');

insert into access_token value('19XKLPXCAYGDJKH9VB7KA9V3Y','','','auto','3YBVRXTBW7578SVXMEJ9UK67E','ceshi','jhon');

查看当前数据

select token_id, from_unixtime(created_timestamp/1000) as created_timestamp, from_unixtime(expired_timestamp/1000) as expired_timestamp, grant_type, refresh_token , scope, user_info from access_token;
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| token_id | created_timestamp | expired_timestamp | grant_type | refresh_token | scope | user_info |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| 19XKLPXCAYGDJKH9VB7KA9V3Y | 2019-12-14 19:02:16.0000 | 2019-12-14 19:05:04.0000 | auto | 3YBVRXTBW7578SVXMEJ9UK67E | ceshi | jhon |
| 4MXCXCXQRZRY1FVYAO9DG99F9 | 2019-12-14 18:27:56.0000 | 2019-12-14 19:00:02.0000 | mannual | EN525M7AW448RBR3386ODM9T3 | test | tim |
| 9T8F9EX42G9OXHPIDM98YBK74 | 2019-12-14 18:27:56.0000 | 2020-12-31 23:59:59.0000 | mannual | 6JL351QA38Z9ONOL9PWJ64A4U | test | david |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
3 rows in set (0.00 sec)

过两分钟后,再次查看数据时,发现刚才插入access_token为当前时间戳的数据已经被删除

select token_id, from_unixtime(created_timestamp/1000) as created_timestamp, from_unixtime(expired_timestamp/1000) as expired_timestamp, grant_type, refresh_token , scope, user_info from access_token;
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| token_id | created_timestamp | expired_timestamp | grant_type | refresh_token | scope | user_info |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| 19XKLPXCAYGDJKH9VB7KA9V3Y | 2019-12-14 19:02:16.0000 | 2019-12-14 19:05:04.0000 | auto | 3YBVRXTBW7578SVXMEJ9UK67E | ceshi | jhon |
| 9T8F9EX42G9OXHPIDM98YBK74 | 2019-12-14 18:27:56.0000 | 2020-12-31 23:59:59.0000 | mannual | 6JL351QA38Z9ONOL9PWJ64A4U | test | david |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
2 rows in set (0.00 sec)

五分钟以后再次查看,刚才插入access_token为当前时间五分钟后的时间戳的数据也已经被删除

mysql> select token_id, from_unixtime(created_timestamp/1000) as created_timestamp, from_unixtime(expired_timestamp/1000) as expired_timestamp, grant_type, refresh_token , scope, user_info from access_token;
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| token_id | created_timestamp | expired_timestamp | grant_type | refresh_token | scope | user_info |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
| 9T8F9EX42G9OXHPIDM98YBK74 | 2019-12-14 18:27:56.0000 | 2020-12-31 23:59:59.0000 | mannual | 6JL351QA38Z9ONOL9PWJ64A4U | test | david |
+---------------------------+--------------------------+--------------------------+------------+---------------------------+-------+-----------+
1 row in set (0.00 sec)

禁用该event

alter event testdb.delete_access_token_expired_record disable;

再次查看该event

select * from information_schema.events\G
*************************** 1. row ***************************
EVENT_CATALOG: def
EVENT_SCHEMA: testdb
EVENT_NAME: delete_access_token_expired_record
DEFINER: root@localhost
TIME_ZONE: SYSTEM
EVENT_BODY: SQL
EVENT_DEFINITION: delete from testdb.access_token where expired_timestamp < unix_timestamp(current_timestamp)*1000
EVENT_TYPE: RECURRING
EXECUTE_AT: NULL
INTERVAL_VALUE: 2
INTERVAL_FIELD: MINUTE
SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
STARTS: 2019-12-14 18:38:59
ENDS: NULL
STATUS: DISABLED
ON_COMPLETION: NOT PRESERVE
CREATED: 2019-12-14 18:38:59
LAST_ALTERED: 2019-12-14 18:38:59
LAST_EXECUTED: 2019-12-14 11:14:59
EVENT_COMMENT:
ORIGINATOR: 543306
CHARACTER_SET_CLIENT: utf8mb4
COLLATION_CONNECTION: utf8mb4_0900_ai_ci
DATABASE_COLLATION: utf8mb4_0900_ai_ci
1 row in set (0.01 sec)

创建一个event,每天夜里一点定期你删除过期数据

CREATE EVENT testdb.delete_access_token_expired_record
ON SCHEDULE EVERY 24 HOUR STARTS DATE_ADD(DATE_ADD(CURDATE(), INTERVAL 1 DAY), INTERVAL 1 HOUR)
ON COMPLETION PRESERVE DISABLE ON SLAVE
DO DELETE FROM testdb.access_token WHERE expired_timestamp < unix_timestamp(CURRENT_TIMESTAMP)*1000 ;

Mysql事件调度器学习的更多相关文章

  1. mysql 事件调度器

    1.mysql事件调度器,也就是计划任务,计划做某事,有两种方式: 2.在某个时间点做某事,AT TIMESTAMP [+ INTERVAL INTERVAL] 某个时间点加上偏移. 3.定时地做某事 ...

  2. mysql事件调度器

    #查看mysql事件调度器是否开启 SHOW VARIABLES WHERE Variable_name = 'event_scheduler'; #开启mysql事件调度器功能 SET GLOBAL ...

  3. MySQL事件调度器event的使用

    Q:假设,有一个需求,希望在某一个时刻系统调用一个begin end执行一下:十分钟以后执行一下begin end.亦或有一个需求,每个多长时间周期性执行begin end.那么这个时候该怎么办呢? ...

  4. MySQL事件调度器Event Scheduler

    我们都知道windows的计划任务和linux的crontab都是用来实现一些周期性的任务和固定时间须要运行的任务. 在mysql5.1之前我们完毕数据库的周期性操作都必须借助这些操作系统实现. 在m ...

  5. mysql事件调度器定时删除binlog

    MySQL5.1.6起Mysql增加了事件调度器(Event Scheduler),可以用做定时执行某些特定任务,来取代原先只能由Linux操作系统的计划任务来执行的工作MySQL的事件调度器可以精确 ...

  6. mysql事件调度器功能

    一.前言 自MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务(例如:删除记录.对数据进行汇总等等),来取代原先只能由操作系 ...

  7. mysql事件调度器使用

    --设置事件调度器 开启SET GLOBAL event_scheduler = ON; --查看事件调度器 是否开启 on开  off关SHOW VARIABLES LIKE 'event_sche ...

  8. MySQL的事件调度器

    自MySQL5.1.0起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务,可以看作基于时间的触发器. 一.开启 事件调度默认是关闭的,开启可执行 ...

  9. MySQL计划任务(事件调度器)(Event Scheduler)

    http://www.cnblogs.com/c840136/articles/2388512.html https://dev.mysql.com/doc/refman/5.7/en/events- ...

随机推荐

  1. 我带旅游ERP管理系统开发的经历

    前言 不怎么会写博文的我也不知道怎么写,但是有冲动让我去记录下我做项目的经历. 项目背景 某旅行社需要一款内部业务管理系统,找了一家外包来开发但最终不是她们想要的,上线的时候很多问题,首先酒店和票务资 ...

  2. php+上传超大文件

    demo下载:http://t.cn/Ai9p3CKQ 教程:http://t.cn/Aipg9uUK 一提到大文件上传,首先想到的是啥??? 没错,就是修改php.ini文件里的上传限制,那就是up ...

  3. 「BZOJ 2653」middle「主席树」「二分」

    题意 一个长度为\(n\)的序列\(a\),设其排过序之后为\(b\),其中位数定义为\(b[n/2]\),其中\(a,b\)从\(0\)开始标号,除法取下整.给你一个长度为\(n\)的序列\(s\) ...

  4. Django-自定义web框架

    自定义web框架 1.准备登录的html <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  5. sql到python正则

    import urllib.requestimport re,timeresult=[]for i in range(100):    urls ="http://xxx.com/-1%20 ...

  6. 20191121-5 Scrum立会报告+燃尽图 01

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/10065 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名 ...

  7. CentOS服务器安装部署Java环境(jdk,tomcat)

    第一步:卸载openjdk 用命令 java -version,如有下面的信息说明CentOS自带OpenJdk,没安装跳过这一步: 最好还是先卸载掉openjdk,再安装oracle公司的jdk.先 ...

  8. ssy-publish

    github地址: https://github.com/shangyueyue/ssy-publish 一.安装 npm install ssy-publish -D 二.在process.cwd( ...

  9. 2.3 Go语言基础之数组

    本文主要介绍Go语言中数组(array)及它的基本使用. 一.Array(数组) 数组是同一种数据类型元素的集合. 在Go语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化. 基 ...

  10. linux下如何进入chroot环境?

    1. 假设要chroot的根目录为/mnt 2. 创建必要的目录 mkdir /mnt/{dev,proc,sys,run} 3. 挂载和构建/dev mount -v --bind /dev /mn ...