Skip to content

Instantly share code, notes, and snippets.

@jeffreyroberts
Last active December 23, 2015 05:59
Show Gist options
  • Save jeffreyroberts/6590635 to your computer and use it in GitHub Desktop.
Save jeffreyroberts/6590635 to your computer and use it in GitHub Desktop.
MySQL - Monitor growth history of all databases and tables hosted on a MySQL Server
-- SET GLOBAL event_scheduler = ON;
-- create table db_growth_history(db_name varchar(64), tbl_name varchar(64), avg_row_length int, row_count bigint, time_stamp datetime);
DELIMITER $$
CREATE
EVENT `document_db_growth_history`
ON SCHEDULE EVERY 1 DAY
DO BEGIN
-- Grab db name, table name, avg row length, row count, add timestamp = growth_history
INSERT INTO db_growth_history(db_name,tbl_name,avg_row_length,row_count,time_stamp)
SELECT TABLE_SCHEMA, TABLE_NAME, AVG_ROW_LENGTH, TABLE_ROWS, NOW() FROM `information_schema`.`tables`;
-- End Query
END $$
DELIMITER ;
@jeffreyroberts
Copy link
Author

Output Example:

mysql> select * from db_growth_history limit 1;

+--------------------+----------------+----------------+-----------+---------------------+
| db_name | tbl_name | avg_row_length | row_count | time_stamp |
+--------------------+----------------+----------------+-----------+---------------------+
| information_schema | CHARACTER_SETS | 384 | NULL | 2013-09-17 02:11:16 |
+--------------------+----------------+----------------+-----------+---------------------+
1 row in set (0.00 sec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment