When was the last time a mysql table was accessed?

后端 未结 5 1232
隐瞒了意图╮
隐瞒了意图╮ 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.

    0 讨论(0)
  • 2020-12-10 15:28

    You can use the OS level stat command. Locate the ibd file for that particular table and run the below command


    stat file_location


    If the Table is being queried by SELECT, You can find the timestamp of when it was accessed with under the Access field.

    0 讨论(0)
  • For a more detailed (db name and table name) plus the period (range), try this query:

    select table_schema as DatabaseName,
      table_name as TableName,
      update_time as LastAccessTime
    from information_schema.tables
    where update_time < 'yyyy-mm-dd'
    group by table_schema
    order by update_time asc 
    
    0 讨论(0)
  • 2020-12-10 15:38

    Use information_schema database to find which table of respective database was updated:

    SELECT UPDATE_TIME
    FROM   information_schema.tables
    WHERE  TABLE_SCHEMA = 'dbname'
    AND TABLE_NAME = 'tablename'
    order by UPDATE_TIME DESC
    
    0 讨论(0)
  • 2020-12-10 15:45

    You can get the last update time of a table.

    SELECT update_time FROM information_schema.tables WHERE table_name='tablename'
    
    0 讨论(0)
提交回复
热议问题