Statistics Updates
MySQL auto-updates index statistics, but heavily-written tables may benefit from scheduled ANALYZE TABLE to keep query plans optimal.
MySQL auto-updates index statistics, but heavily-written tables may benefit from scheduled ANALYZE TABLE to keep query plans optimal.
CREATE EVENT update_statistics
ON SCHEDULE EVERY 1 WEEK STARTS "2024-01-07 03:00:00"
DO
BEGIN
ANALYZE TABLE orders, order_items, products, users;
ANALYZE TABLE inventory, shipments, payments;
INSERT INTO maintenance_log (task, run_at) VALUES ("analyze_tables", NOW());
END;
-- Check when statistics were last updated:
SELECT table_name, update_time FROM information_schema.tables
WHERE table_schema = "mydb" ORDER BY update_time;
ANALYZE TABLE is non-blocking on InnoDB — safe to run in production without locking.