DEV Community

丁久
丁久

Posted on • Originally published at dingjiu1989-hue.github.io

Query Performance Tuning Tools

This article was originally published on AI Study Room. For the full version with working code examples and related articles, visit the original post.

Query Performance Tuning Tools

Query Performance Tuning Tools

Query Performance Tuning Tools

Query Performance Tuning Tools

Query Performance Tuning Tools

Query Performance Tuning Tools

Query Performance Tuning Tools

Query Performance Tuning Tools

The Performance Tuning Process

Database performance tuning is a systematic process of identifying slow queries, understanding why they are slow, and making targeted improvements. It is not about guessing — every change should be informed by data from monitoring and profiling tools.

Step 1: Identify Slow Queries

PostgreSQL Slow Query Log

postgresql.conf

log_min_duration_statement = 1000 # Log queries slower than 1 second

log_Queries = on

log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '

log_checkpoints = on

log_lock_waits = on

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- Query log directly from the database

SELECT

query,

calls,

total_exec_time / calls AS avg_time_ms,

rows / calls AS avg_rows,

mean_exec_time,

max_exec_time,

stddev_exec_time

FROM pg_stat_statements

ORDER BY total_exec_time DESC

LIMIT 20;

MySQL Slow Query Log

my.cnf

slow_query_log = 1

slow_query_log_file = /var/log/mysql/slow.log

long_query_time = 1

log_queries_not_using_indexes = 1

Analyze slow query log

pt-query-digest /var/log/mysql/slow.log

Output includes:

\\\\\\\\\\\\\\\\- Worst queries by execution time

\\\\\\\\\\\\\\\\- Worst queries by frequency

\\\\\\\\\\\\\\\\- Query patterns and response time distribution

Step 2: Analyze Query Plans

EXPLAIN ANALYZE

EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)

SELECT

u.name,

COUNT(o.id) AS order_count,

SUM(o.total) AS total_spent

FROM users u

LEFT JOIN orders o ON u.id = o.user_id

WHERE u.created_at > '2026-01-01'

GROUP BY u.id, u.name

HAVING COUNT(o.id) > 3

ORDER BY total_spent DESC

LIMIT 50;

Key Metrics to Analyze


Read the full article on AI Study Room for complete code examples, comparison tables, and related resources.

Found this useful? Check out more developer guides and tool comparisons on AI Study Room.

Top comments (0)