MySQL中對于DML(數(shù)據(jù)寫入,數(shù)據(jù)修改和數(shù)據(jù)刪除操作是可以有效跟蹤的,對于select目前沒有找到好的方法,只能通過構(gòu)建生命周期體系來完善了。
我來說一下數(shù)據(jù)寫入的跟進過程,可以在數(shù)據(jù)庫test中創(chuàng)建一張表test_data
create table test_data(id int primary key,name varchar(30)) engine=InnoDB;
我們寫入一條數(shù)據(jù):
mysql> insert into test_data values(1,'aa');
Query OK, 1 row affected (0.00 sec)
查看數(shù)據(jù)字典informationschema.tables的字段update_time可以看到發(fā)生了變化。
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)
如果繼續(xù)寫入一條數(shù)據(jù):
mysql> insert into test_data values(2,'bb');
Query OK, 1 row affected (0.00 sec)
查看字典里的數(shù)據(jù),就會發(fā)現(xiàn)時間戳開始變化(遞增),當然你可以在寫入前記錄下時間戳。
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)
所以你的問題可以轉(zhuǎn)換一個思路來實現(xiàn),即一段時間內(nèi)沒有變化的表,可以通過information_schema.tables的字段來進行查詢,這樣就可以得到數(shù)據(jù)庫里的熱表和冷表了。
比如查看2019-10-08之前沒有DML數(shù)據(jù)變化的表,可以使用如下的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)
補充下,數(shù)據(jù)庫版本建議是在MySQL 5.7+