SyntaxStudy
Sign Up
MySQL Updating Table Statistics
MySQL Intermediate 3 min read

Updating Table Statistics

Statistics Updates

MySQL auto-updates index statistics, but heavily-written tables may benefit from scheduled ANALYZE TABLE to keep query plans optimal.

Example
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;
Pro Tip

ANALYZE TABLE is non-blocking on InnoDB — safe to run in production without locking.