-- awr_setup.sql: create the awr schema that keeps snapshots of MySQL statement digests -- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/) -- https://hashjoin.com/kb/mysql/awr-statement-digest-snapshots/ -- -- Usage: mysql < awr_setup.sql (safe to run again; it never drops anything) -- -- awr.snap one row per snapshot: when, the host's CPU if you pass it, digest-table health -- awr.digest_stat the cumulative counters of every digest at that snapshot -- awr.digest_text the normalized statement text, stored once per digest -- awr.snap_interval each snapshot with the one before it and the seconds between them -- awr.digest_delta what each digest did between consecutive snapshots: the numbers you report on CREATE DATABASE IF NOT EXISTS awr; CREATE TABLE IF NOT EXISTS awr.snap ( snap_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, taken_at DATETIME(6) NOT NULL, cpu_pct DECIMAL(5,2) NULL, -- from your monitoring (CloudWatch, node exporter); NULL if not passed digests INT UNSIGNED NOT NULL DEFAULT 0, digest_lost BIGINT UNSIGNED NULL, -- Performance_schema_digest_lost: statements that found the digest table full KEY snap_taken (taken_at) ) ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS awr.digest_stat ( snap_id INT UNSIGNED NOT NULL, schema_name VARCHAR(64) NOT NULL DEFAULT '', -- '' = no default database digest VARCHAR(64) NOT NULL DEFAULT '', -- '' = the catch-all row used once the digest table is full count_star BIGINT UNSIGNED NOT NULL, sum_timer_wait BIGINT UNSIGNED NOT NULL, -- picoseconds sum_lock_time BIGINT UNSIGNED NOT NULL, sum_errors BIGINT UNSIGNED NOT NULL, sum_rows_sent BIGINT UNSIGNED NOT NULL, sum_rows_examined BIGINT UNSIGNED NOT NULL, sum_rows_affected BIGINT UNSIGNED NOT NULL, sum_created_tmp_disk_tables BIGINT UNSIGNED NOT NULL, sum_no_index_used BIGINT UNSIGNED NOT NULL, first_seen DATETIME(6) NULL, last_seen DATETIME(6) NULL, PRIMARY KEY (snap_id, schema_name, digest), KEY digest_stat_digest (digest) ) ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS awr.digest_text ( digest VARCHAR(64) NOT NULL PRIMARY KEY, digest_text LONGTEXT NULL, first_snap_id INT UNSIGNED NOT NULL ) ENGINE=InnoDB; CREATE OR REPLACE VIEW awr.snap_interval AS SELECT s.snap_id, p.snap_id AS prev_snap_id, s.taken_at, TIMESTAMPDIFF(MICROSECOND, p.taken_at, s.taken_at) / 1000000 AS interval_sec, s.cpu_pct FROM awr.snap s JOIN awr.snap p ON p.snap_id = (SELECT MAX(x.snap_id) FROM awr.snap x WHERE x.snap_id < s.snap_id); -- The counters only ever grow, except when someone truncates the digest table or the server -- restarts. A digest with no row in the previous snapshot, or with a smaller count than before, -- started counting again since then, so its current values are the delta. CREATE OR REPLACE VIEW awr.digest_delta AS SELECT i.snap_id, i.prev_snap_id, i.taken_at, i.interval_sec, i.cpu_pct, c.schema_name, c.digest, IF(p.digest IS NULL OR c.count_star < p.count_star, 1, 0) AS new_or_reset, c.count_star - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.count_star) AS calls, c.sum_timer_wait - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_timer_wait) AS time_ps, c.sum_lock_time - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_lock_time) AS lock_ps, c.sum_errors - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_errors) AS errors, c.sum_rows_sent - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_rows_sent) AS rows_sent, c.sum_rows_examined - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_rows_examined) AS rows_examined, c.sum_rows_affected - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_rows_affected) AS rows_affected, c.sum_created_tmp_disk_tables - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_created_tmp_disk_tables) AS tmp_disk_tables, c.sum_no_index_used - IF(p.digest IS NULL OR c.count_star < p.count_star, 0, p.sum_no_index_used) AS no_index_used FROM awr.snap_interval i JOIN awr.digest_stat c ON c.snap_id = i.snap_id LEFT JOIN awr.digest_stat p ON p.snap_id = i.prev_snap_id AND p.schema_name = c.schema_name AND p.digest = c.digest;