The following is forMySQL database QPS (Queries Per Second) and TPS (Transactions Per Second) dataRecommended detailed query methods and tools, including specific commands and examples:
1. Direct query through the MySQL command line
1. Query QPS
-- Get the current total number of queries(All types) SHOW STATUS LIKE 'Queries'; -- Calculate the past N In seconds QPS(For example N=5) SELECT (NOW() - INTERVAL 5 SECOND) AS Time difference, (SHOW STATUS LIKE 'Queries') - prev_queries AS The total number of current queries, ROUND((The total number of current queries / Time difference.total_seconds), 2) AS QPS FROM (SELECT SHOW STATUS LIKE 'Queries' AS prev_queries) AS subquery, (SELECT NOW() - INTERVAL 5 SECOND AS time_diff) AS time_diff;
2. Query TPS
-- Get the current transaction commits SHOW STATUS LIKE 'Com_commit'; -- Calculate the past N In seconds TPS(For example N=5) SELECT (NOW() - INTERVAL 5 SECOND) AS Time difference, (SHOW STATUS LIKE 'Com_commit') - prev_commits AS The current total number of submissions, ROUND((The current total number of submissions / Time difference.total_seconds), 2) AS TPS FROM (SELECT SHOW STATUS LIKE 'Com_commit' AS prev_commits) AS subquery, (SELECT NOW() - INTERVAL 5 SECOND AS time_diff) AS time_diff;
2. Use Performance Schema (recommended)
1. Enable Performance Schema
-- Check if it is enabled SHOW VARIABLES LIKE 'performance_schema'; -- If not enabled,exist `` Added in: [mysqld] performance_schema = ON
2. Query QPS (statistics by statement type)
SELECT event_name AS Query Type, COUNT_STAR AS Total number of executions, AVG_TIMER_WAIT AS Average delay(Microseconds), SUM_TIMER_WAIT AS Total delay(Microseconds) FROM performance_schema.events_statements_summary_by_digest WHERE event_name IN ('SELECT', 'INSERT', 'UPDATE', 'DELETE') ORDER BY COUNT_STAR DESC;
3. Query TPS (Statistics by transaction)
SELECT event_name AS Transaction type, COUNT_STAR AS Number of submissions, AVG_TIMER_WAIT AS Average delay(Microseconds) FROM performance_schema.events_statements_summary_by_digest WHERE event_name = 'COMMIT';
3. Use mysqladmin tool
1. Check the real-time status
# View all key state variables (including QPS and TPS)mysqladmin -u root -p status # View only QPS, TPS, and connection countmysqladmin -u root -p extended-status | grep -E 'Queries|Com_commit|Threads_connected'
4. Use Prometheus + mysql_exporter
1. Deploy mysql_exporter
# Download and run mysql_exporterwget /prometheus/mysqld_exporter/releases/download/v1.16.0/mysqld_exporter-1.16. tar xvfz mysqld_exporter-*. cd mysqld_exporter-*/ ./mysqld_exporter # Prometheus configuration (``)scrape_configs: - job_name: 'mysql' static_configs: - targets: ['localhost:9104']
2. Query QPS and TPS
# QPS: All query rates (number of executions per second)rate(mysql_statements_total{statement_type=~"SELECT|INSERT|UPDATE|DELETE"}[5m]) # TPS: Transaction commit rate (number of commits per second)rate(mysql_statements_total{statement_type="COMMIT"}[5m])
3. Visual dashboard
- Add a MySQL monitoring panel to Grafana to display metrics such as real-time QPS, TPS, slow query, etc.
5. Automation scripts (Python example)
import import time def get_mysql_stats(host, user, password, db): conn = ( host=host, user=user, password=password, database=db ) cursor = () # Query QPS ("SHOW STATUS LIKE 'Queries'") queries_total = int(()[1]) # Query TPS ("SHOW STATUS LIKE 'Com_commit'") commits_total = int(()[1]) () () return queries_total, commits_total if __name__ == "__main__": host = 'localhost' user = 'root' password = 'password' db = 'test' prev_queries, prev_commits = get_mysql_stats(host, user, password, db) while True: (5) current_queries, current_commits = get_mysql_stats(host, user, password, db) qps = (current_queries - prev_queries) / 5 tps = (current_commits - prev_commits) / 5 print(f"[{('%Y-%m-%d %H:%M:%S')}] QPS={qps:.2f}, TPS={tps:.2f}") prev_queries, prev_commits = current_queries, current_commits
VI. Other tools
1. Percona Toolkit's pt-query-digest
# Analyze slow query logs and count QPS/TPSpt-query-digest --slow-log=/var/log/mysql/ --output=report
2. MySQL Workbench monitoring panel
- OpenServer StatusTab, view directlyQueries per secondandTransactions per second。
7. Optimization suggestions
- Index optimization: Missing indexes will cause full table scans, significantly reducing QPS.
-
SQL Tuning: Avoid complex subqueries and use
EXPLAIN
Analyze the execution plan. - Transaction control: Reduce long transactions to occupy lock resources and adjust the isolation level appropriately.
-
Configuration Tuning:Revise
innodb_buffer_pool_size
、query_cache_size
etc.
Summarize
-
Lightweight monitoring: Use directly
SHOW STATUS
ormysqladmin
。 -
Fine analysis:Enable
Performance Schema
Or integrate Prometheus. -
Long-term optimization: Combined with slow query logs and index analysis tools (such as
EXPLAIN
) Improve performance.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.