Skip to content

Instantly share code, notes, and snippets.

@jeffreyroberts
Last active June 11, 2022 10:35
Show Gist options
  • Save jeffreyroberts/6590485 to your computer and use it in GitHub Desktop.
Save jeffreyroberts/6590485 to your computer and use it in GitHub Desktop.
MySQL - Monitor table growth rates in terms of row counts
-- SET GLOBAL event_scheduler = ON;
-- create table growth_history(tbl_name varchar(64), row_count bigint, time_stamp datetime);
DELIMITER $$
CREATE
EVENT `document_growth_history`
ON SCHEDULE EVERY 1 DAY
DO BEGIN
-- Insert row counts for each table in the specified database
INSERT INTO growth_history(tbl_name,row_count,time_stamp)
SELECT TABLE_NAME, TABLE_ROWS, NOW() FROM `information_schema`.`tables`
WHERE `table_schema` = '<database_name>';
-- End Query
END $$
DELIMITER ;
@jeffreyroberts
Copy link
Author

Output Example:

mysql> select * from growth_history;
+-------------------------------------+-----------+---------------------+
| tbl_name                            | row_count | time_stamp          |
+-------------------------------------+-----------+---------------------+
| asn_assignee                        |    206309 | 2013-09-17 01:51:00 |
| asn_assignment                      |     42044 | 2013-09-17 01:51:00 |
| asn_assignor                        |    188421 | 2013-09-17 01:51:00 |

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