Go get last access time of a table in mysql - mysql

Is there any way to know the time when last select statement has been performed on a table? I am using the InnoDB storage engine.
I have tried with the following query:
select update_time,table_name from information_schema.tables where table_schema='databasename';
..but I'm receiving NULL in the update_time column.

Unless you manually update a last_accessed-field on the table, my best bet would be to add query logging and parse the log-files.
I googled and found these relates questions:
When was the last time a mysql table was accessed?
How do you get the last access (and/or write) time of a MySQL database?

SELECT UPDATE_TIME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tabname'`

Related

MySQL information_schema.tables UPDATE_TIME refresh on specific column change with ANALYZE TABLE

Is it possible to update the INFORMATION_SCHEMA.TABLES UPDATE_TIME for a specific table when a specific column is changed in this table?
I know that MySQL saves its cache to the TABLES on a timeout specified in the information_schema_stats_expiry variable and the timeout is 24h by default which is ok for me. Also I know I can query ANALYZE TABLE 'table_name_1', ... 'table_name_N' to force MySQL update the TABLES. But it will cause the update with no conditions. So is it possible to call ANALYZE TABLE only for specific columns? Thanks

How to see the create time of a MySql user?

How to see the create time of a MySql user?
I want to know the create time of a MySql user called "local".
Therefore, I have queried the mysql.user table, but that does not display the creation time for the user. Furthremore, there are no MySql logs in /var/log.
I need the information for doing forensics analysis on the system.
Is there any way to obtain the create time for MySql users?
You would query the information_schema for the create_time of the table.
For instance:
SELECT create_time FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
Reference: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
If you want to get the user creation date then you can add a timestamp column in mysql.user table and set it to CURRENT_TIMESTAMP as default. So next time when a user is created, you will get the timestamp.

mysql query to search a table name from multiple databases

I am facing a problem in searching a particular table_name.
I have around 50 databases on the server and i wish to search a table_name say X is created in which all databases.
Is there any straight-forward to find the exact databases in which X table is found in MYSQL through phpMyAdmin.
Any help will be appreciated.
Thanks
You can query the information_schema database for this. The below query will return the names of all the databases, which contains the table your_table_name.
SELECT `TABLE_SCHEMA`
FROM `information_schema`.`TABLES`
WHERE `TABLE_NAME` = 'your_table_name'
I hope this is what you are looking for.
According to the MySQL documentation about information_schema database,
INFORMATION_SCHEMA provides access to database metadata.
Metadata is data about the data, such as the name of a database or
table, the data type of a column, or access privileges. Other terms
that sometimes are used for this information are data dictionary and
system catalog.
Please query the information_schema.TABLES with table_type='BASE TABLE' and providing the name of the table(table_name).
This query will give us all the tables with '<your_table_name>' in all the databases that are currently in our server.
Please change '<your_table_name>' as per your requirement.
SELECT TABLE_SCHEMA, TABLE_NAME
FROM `information_schema`.`TABLES`
where table_type='BASE TABLE'
and table_name = '<your_table_name>'
limit 100;

information_schema tables and update_time behaviour

I'd like to have some explanation about information_schema and its behaviours.
Let's say I want to know when a myisam table has been modified.
I write this query
select update_time from information_schema.tables
where table_schema = 'my_db' and table_name = 'my_table'
Even though I apply some change to my table nothing happens in it until I run a flush tables.
Unluckily it seems to me that update_time stores date and time of the moment that I run flush tables, not the one when table changes really occur. Is it true?
Thanks in advance.
I personally prefer to add a last_change timestamp to each of my tables, then define it as "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", which means that mysql will automatically update it each time the row is modified.
With this, you could just query the table directly and know not only that the table was touched, but also which rows were touched.
It doesn't work to detect deleted rows though.

MYSQL - count number of rows in each table

I would like to know how many rows are in each table in my database. I've come so far as to having
select count(*) _tablename_;
However i would need to do that on each and every table - and there are a lot. What would me the best way to get a print-out with the table name and it's row count?
SELECT table_name, table_rows
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<your db>';
I also hope you realise there's an error in your query: it's missing a FROM.
This following query will return number of rows in each table but it doesn't seem to return exact value all the time
SELECT table_name, table_rows
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<your db>';
TABLE_ROWS: The number of table rows in the partition. For partitioned InnoDB tables, the row count given in the TABLE_ROWS column is only an estimated value used in SQL optimization, and may not always be exact...
for more https://dev.mysql.com/doc/mysql-infoschema-excerpt/5.5/en/partitions-table.html
In addition to SQL queries by others, one can also use Workbench GUI to get the row-counts of each table. To do this, Launch Workbench -> Connect to Db -> right click Db and select "Schema Inspector" (as in Screenshot below - I have highlighted the "Rows" column):
HTH.