When was the last time a mysql table was accessed?

后端 未结 5 1468
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 14:43

Is there a way to tell the last access time of a mysql table? By access I mean any type of operation in that table including update, alter or even select or any other operat

5条回答
  •  心在旅途
    2020-12-10 15:25

    I don't know how to get the exact time after-the-fact, but you can start dumping logs, do something, and then stop dumping logs. Whichever tables show up in the logs are the ones that were accessed during that time.

    If you care to dig through the log, the queries are shown with timestamps.


    Tell mysql where to put the log file

    Add this line to my.cnf (on some systems it will be mysql.conf.d/mysqld.cnf).

    general_log_file = /path/to/query.log

    Enable the general log

    mysql> SET global general_log = 1;

    (don't forget to turn this off, it can grow very quickly)

    Do the thing

    All mysql queries will be added to /path/to/query.log

    Disable the general log

    mysql> SET global general_log = 0;

    See which tables appeared

    If it's short, you can just scroll through query.log. If not, then you can filter the log for known table names, like so:

    query_words=$(cat mysql_general.log | tr -s [:space:] \\n | tr -c -d '[a-zA-Z0-9][:space:][_\-]' | egrep -v '[0-9]' | sort | uniq)
    
    table_names=$(mysql -uroot -ptest -Dmeta -e"show tables;" | sort | uniq)
    
    comm -12 <(echo $table_names) <(echo $query_words)
    

    From there, you can grep the log file for whatever showed up in table_names. There you will find timestammped queries.

    See also, this utility, which I made.

提交回复
热议问题