Fix a database that is slow
We'll find slow queries with the slow query log, run EXPLAIN to spot missing indexes, add indexes, and rule out disk or memory pressure—or tell you when to escalate.
What you'll need
- Database admin access (or sudo)
- Ability to restart or reload the database service
Step-by-step diagnostic
Quick triage — pick your path
Get started
Choose the option that matches what you see. You can jump straight to that section.
- Follow this guide Work through the full procedure from slow query log to indexes.
- Enable slow query log You want to identify which queries are slow first.
- Run EXPLAIN and add indexes You already know the slow queries and want to optimize them.
- Check disk and memory You suspect disk or memory pressure.
- When to escalate Indexes and tuning did not help, or you need schema or hardware changes.
Show full guide
Steps
Goal: Find slow queries, run EXPLAIN to spot missing indexes, add indexes, and rule out disk or memory pressure.
- Run a simple query (SELECT 1) to confirm the database responds. Check application logs for which operations lag.
- Good: Database responds but specific queries are slow. Proceed to Enable slow query log.
- Bad: Database does not respond—different problem. See When to escalate.
Enable slow query log
Goal: Identify which queries are slow.
- MySQL:
SET GLOBAL slow_query_log=1; SET GLOBAL long_query_time=2;(or in my.cnf). PostgreSQL:ALTER SYSTEM SET log_min_duration_statement=2000;thenSELECT pg_reload_conf(); - Run typical workloads. Inspect the log (MySQL:
SHOW VARIABLES LIKE 'slow_query_log_file';then read that file; PostgreSQL: check log_destination). - Good: Slow queries listed. Proceed to Run EXPLAIN and add indexes.
- Bad: No slow queries—check if threshold is too high or workload differs.
Run EXPLAIN and add indexes
Goal: See why queries are slow and add indexes.
- Copy a slow query from the log. Run EXPLAIN (MySQL) or
EXPLAIN ANALYZE(PostgreSQL) on it. - Look for “full table scan”, “Seq Scan”, or “type: ALL”—these indicate missing indexes.
- Add indexes on columns in WHERE, JOIN, and ORDER BY:
CREATE INDEX idx_name ON table(column); - Retest. The query plan should show index scan instead of full scan.
- Good: Queries use indexes and are fast.
- Bad: Still slow—check disk and memory; escalate if needed.
Check disk and memory
Goal: Rule out disk or memory pressure.
- Run
df -handdf -i. Checkiostatandtopfor I/O wait and memory usage. - If disk is full, see fix-linux-runs-out-of-disk. If memory is low, see fix-linux-server-runs-out-of-memory.
- Good: Disk and memory are fine—focus on query optimization.
- Bad: Resource pressure—fix disk or memory first.
When to escalate
Escalate if:
- The database does not respond (connection or crash).
- You have added indexes and tuned settings but the database is still slow.
- You need schema redesign or hardware upgrades.
Provide EXPLAIN output, slow query samples, df -h, df -i, and memory stats.
Verification
- Slow queries no longer appear in the slow query log (or appear less often).
- EXPLAIN shows index scans instead of full table scans for the fixed queries.
- Application response times improve.
- df -h and df -i show adequate disk space; memory is not exhausted.
Escalation ladder
Work from the device outward. Stop when the problem is fixed.
- Slow query log Enable and inspect the slow query log to find long-running queries.
- EXPLAIN and indexes Run EXPLAIN on slow queries; add indexes on filtered and joined columns.
- Locks and contention Check for lock contention and long-running transactions.
- Disk and memory Check disk space, I/O wait, and memory; rule out resource pressure.
- Escalate Provide EXPLAIN output, slow query samples, df -h, df -i, memory stats.
What to capture if you need help
Before calling support or posting for help, have these ready. It speeds everything up.
- Slow query log samples
- EXPLAIN output for slow queries
- df -h and df -i output
- Memory and I/O stats
- Steps already tried
Does the database respond but specific queries are slow?
Run a simple query (SELECT 1) to confirm the database is up. Check application logs for which operations lag.
You can change your answer later.
Are slow queries logged?
Enable the slow query log. MySQL: slow_query_log=1, long_query_time=2. PostgreSQL: log_min_duration_statement=2000.
You can change your answer later.
Does EXPLAIN show full table scans or missing indexes?
Run EXPLAIN (MySQL) or EXPLAIN ANALYZE (PostgreSQL) on slow queries. Look for Seq Scan, type: ALL, or full table scan.
You can change your answer later.
Add indexes and retest
Is disk full or memory low?
Run df -h, df -i. Check iostat, top for I/O wait and memory.
Is disk or memory the bottleneck?
You can change your answer later.
Escalate
Reviewed by Blackbox Atlas
Frequently asked questions
- Why is my database slow?
- Common causes: missing indexes (full table scans), slow queries, disk I/O saturation, or low memory. Use EXPLAIN and the slow query log to identify the cause.
- How do I find slow queries?
- Enable the slow query log (MySQL: slow_query_log; PostgreSQL: log_min_duration_statement). Run EXPLAIN on the logged queries to see execution plans.
- When should I escalate a slow database?
- If you have added indexes and optimized queries but the database is still slow, or if you need schema changes or hardware upgrades.
Rate this guide
Was this helpful?
Thanks for your feedback.