偶来说一下数据写入的跟进过程,可以在数据库test中创建一张表test_data
create table test_data(id int primary key,name varchar(30)) engine=InnoDB;
大家写入一条数据:
mysql> insert into test_data values(1,’aa’);
Query OK, 1 row affected (0.00 sec)
查看数据字典informationschema.tables的字段update_time可以看到发生了变化。
mysql> select * from information_schema.tables where table_schema=’test’ and table_name=’test_data’\G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_data
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 1
AVG_ROW_LENGTH: 16384
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 0
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-10-08 12:29:05
UPDATE_TIME: 2019-10-08 12:29:40
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
如果继续写入一条数据:
mysql> insert into test_data values(2,’bb’);
Query OK, 1 row affected (0.00 sec)
查看字典里的数据,就会发现时间戳开始变化(递增),当然你可以在写入前记录下时间戳。
mysql> select * from information_schema.tables where table_schema=’test’ and table_name=’test_data’\G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: test
TABLE_NAME: test_data
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 2
AVG_ROW_LENGTH: 8192
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 0
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2019-10-08 12:29:05
UPDATE_TIME: 2019-10-08 12:29:58
CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
所以你的问题可以转换一个思路来实现,即一段时间内没有变化的表,可以通过information_schema.tables的字段来进行查询,这样就可以得到数据库里的热表和冷表了。
比如查看2019-10-08之前没有DML数据变化的表,可以使用如下的SQL:
select table_name,update_time from information_schema.tables where update_time <=’2019-10-08′ ;
xxxx1 | 2019-09-02 23:49:42
xxxx2 | 2019-09-03 23:45:21
xxxx3 | 2019-09-04 23:59:31
xxxx4 | 2019-09-05 23:41:25
xxxx5 | 2019-09-06 21:44:40
xxxx6 | 2019-09-07 23:46:17
+————————-+———————+
1019 rows in set (1.66 sec)
补充下,数据库版本建议是在MySQL 5.7+