mysql update_time
在 mysql中有一個DB 是 information_schema。顧名思義就是一些schema 的信息,表的結構,字段,佔用大小等等信息都在其中。今天使用到的是其中的table表。
information_schema tables
如下是所有的字段的含義
字段 | 含義 |
---|---|
Table_catalog | 數據表登記目錄 |
Table_schema | 數據表所屬的數據庫名 |
Table_name | 表名稱 |
Table_type | 表類型[system view |
Engine | 使用的數據庫引擎[MyISAM |
Version | 版本,默認值10 |
Row_format | 行格式[Compact |
Table_rows | 表裏所存多少行數據 |
Avg_row_length | 平均行長度 |
Data_length | 數據長度 |
Max_data_length | 最大數據長度 |
Index_length | 索引長度 |
Data_free | 空間碎片 |
Auto_increment | 做自增主鍵的自動增量當前值 |
Create_time | 表的創建時間 |
Update_time | 表的更新時間 |
Check_time | 表的檢查時間 |
Table_collation | 表的字符校驗編碼集 |
Checksum | 校驗和 |
Create_options | 創建選項 |
Table_comment | 表的註釋、備註 |
如果需要知道一個表什麼時候有更新只需要關注update_time 即可。
InnoDB update_time NULL
通過實際測試中發現test環境的update_time 一直是null,這就沒有辦法通過udpate_time 來進行判斷了。那麼爲什麼會出現這個問題?在網上查找了相關資料,發現其他人也碰到了相同的問題,結論是 InnoDB不支持,而MyISAM 支持。建議換引擎。這不是扯淡嗎。。爲了update_time 換引擎,這不是因噎廢食嗎?並且在線上的DB 是可以顯示的。還是看看 stackoverflow 找找資料。最終找到了一下解析:
As MySQL documentation on information_schema.tables says (emphasis added):
Beginning with MySQL 5.7.2, UPDATE_TIME displays a timestamp value for the last UPDATE, INSERT, or DELETE performed on InnoDB tables that are not partitioned. Previously, UPDATE_TIME displayed a NULL value for InnoDB tables. For MVCC, the timestamp value reflects the COMMIT time, which is considered the last update time. Timestamps are not persisted when the server is restarted or when the table is evicted from the InnoDB data dictionary cache.
The UPDATE_TIME column also shows this information for partitioned InnoDB tables in MySQL 5.7.8 and later. Previously this column was always NULL for such tables. (Bug #17299181, Bug #69990)
Probably you are using innodb tables and your MySQL version is earlier than described in the documentation.
簡單的是在mysql 版本5.7.8 之前InnoDB 確實不支持。但是在之後的版本是可以支持的。於是查看了兩者的版本:
test env:
5.5.52-38.3-log
live env:
+---------------+
| version() |
+---------------+
| 5.7.21-20-log |
+---------------+
版本相差甚遠,但是由此看來 線上可以通過此條件來判斷。這是非常重要的一點。