Services and products mentioned in this article.
Get in touch with our expert team for professional cloud architecture and operations support.
Kaihu Cloud TeamDatabase Reliability Engineers
Kaihu Cloud Team tunes managed MySQL and PostgreSQL fleets, from slow-query triage to buffer pool and connection pool architecture.
All factual claims are cross-referenced against authoritative sources before publication. Data comes from verified primary references.
Most slowness is query slowness: missing indexes, N+1 patterns, exhausted connections and undersized buffer pools.
Diagnose with slow query logs, pg_stat_statements and EXPLAIN before changing anything.
Tune configuration and connection pooling by workload shape — one parameter at a time.
In most incidents the database is not slow in general — specific queries are. Missing indexes forcing full table scans, N+1 patterns from ORMs, exhausted connections and an undersized buffer pool account for the overwhelming majority of real-world slowdowns. On managed engines like RDS and PolarDB the provider handles OS-level tuning, which leaves query, index and configuration work to you.
Throughput (QPS) and latency move independently: a cache-friendly workload can hold high QPS while one new full-scan report query drags p99 latency up. The buffer pool keeps hot pages in memory; once the working set exceeds it, every cold read becomes a disk round trip and latency multiplies.
An orders table with 50 million rows serves checkout in 5 ms via its primary-key index. A new dashboard query filters by customer_id with no index, reads the whole table with EXPLAIN type=ALL, and connection pools fill with copies of the same slow query — checkout now times out even though nothing about checkout changed.
| Metric | Healthy Signal | What to Look For |
|---|---|---|
| QPS | Stable at expected load | Sudden drops indicate bottlenecks |
| Slow queries | Near zero above 1s threshold | Queries over 1s need optimization |
| Connection count | Well below max_connections | Approaching the limit means pool or leak issues |
| Buffer pool hit rate | Above 99% for InnoDB | Lower values mean memory pressure |
| Lock waits | Minimal | High values indicate blocking queries |
An index lookup touches a handful of pages: O(log n) in index depth plus row fetches. A full table scan touches every page, so cost grows with table size forever. N+1 is the same disease at the application layer — one parent query plus one child query per row turns 1 round trip into 101, and network latency stacks on top of query time.
The same missing index can be harmless on a 10-thousand-row table and fatal on 50 million rows. What scales the pain: table size, column selectivity (an index on gender helps nobody, one on order_id helps everything), composite index column order, how many queries the ORM hides behind accessors, and how often the query runs.
| Signal | Before Fix | After Fix |
|---|---|---|
| EXPLAIN type | ALL (full scan) | ref / eq_ref (index lookup) |
| Rows examined | Whole table | Few matching rows |
| Query latency | Seconds, growing with data | Milliseconds, stable |
| Connection pool | Saturated by slow queries | Healthy reuse |
| Buffer pool hit rate | Dropping from scan churn | Above 99% |
Enable the slow query log and rank offenders with pg_stat_statements, then read execution plans. In EXPLAIN output, type=ALL means full scan (bad), ref or eq_ref means index lookup (good), high rows estimates mean inefficiency, and Using filesort or Using temporary marks queries that need restructuring.
# MySQL: enable slow query log
SET GLOBAL slow_query_log = ON;
SET GLOBAL long_query_time = 1;
# PostgreSQL: rank the most expensive queries
SELECT query, calls, total_exec_time, mean_exec_time, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;The classic errors: adding an index for every slow query until writes crawl (unused indexes slow every INSERT); building composite indexes in the wrong column order so they never get used; optimizing without profiling first; and shipping index changes straight to production without testing on production-like data volume.
# Confirm the plan before and after
EXPLAIN SELECT * FROM orders WHERE customer_id = 100;
# PostgreSQL: verify actual timings
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 100;Fix order: add missing indexes on filtered columns, rebuild composite indexes in selectivity order, drop unused indexes (check usage statistics first), and re-run the plan to confirm type moved from ALL to ref. Always test in staging with production-like data volume.
On managed engines, configuration lives in a parameter group, not in my.cnf. The settings that move the needle: innodb_buffer_pool_size around 70% of available RAM for MySQL, shared_buffers at 25% and effective_cache_size at 75% for PostgreSQL, work_mem between 4 and 64 MB per operation, and max_connections sized to real concurrency instead of a big round number.
# MySQL key parameters (cloud-managed: set via parameter group)
innodb_buffer_pool_size = 70% of available RAM
innodb_log_file_size = 1-2GB
max_connections = based on workload
query_cache_size = 0 (disabled in MySQL 8.0+)
# PostgreSQL key parameters
shared_buffers = 25% of available RAM
effective_cache_size = 75% of available RAM
work_mem = 4-64MB (per operation)
maintenance_work_mem = 256MB-1GBApplication servers open far more connections than the database can usefully serve. Put PgBouncer in front of PostgreSQL or ProxySQL in front of MySQL, and prefer the built-in pooling that providers offer for engines like PolarDB. Size pools to the workload, not to the instance count of the app tier.
Read-heavy reporting: raise the buffer pool and add read replicas for heavy SELECT traffic. Write-heavy ingest: increase innodb_log_file_size to reduce checkpoint pressure. High-concurrency OLTP: connection pooling first, then index work. Analytics with big sorts: raise work_mem for the reporting user only, not globally. Tuning is iterative — monitor metrics continuously, change one parameter at a time, and re-measure before touching the next.
Use Cases
Use Cases