NovFora Dev

How to fix slow database queries?

Owen Martin

Owen Martin

4 months ago

Can anyone help me optimize my SQL queries because they keep

Owen Martin

Owen Martin

4 months ago

Make sure indexes exist on columns in your WHERE and JOIN clauses first, then look at EXPLAIN

Owen Martin

Owen Martin

4 months ago

EXPLAIN ANALYZE is your best friend for finding where it's actually slow.

Owen Martin

Owen Martin

4 months ago

Add indexes on columns used in WHERE clauses and JOIN conditions, then use EXPLAIN ANALYZE

Owen Martin

Owen Martin

4 months ago

Check the execution plan first. EXPLAIN ANALYZE shows you exactly where the bottleneck is before

Owen Martin

Owen Martin

4 months ago

EXPLAIN ANALYZE is your best friend here. Identify the actual bottleneck before optimizing anything.

Owen Martin

Owen Martin

4 months ago

Indexes help most of the time. Check EXPLAIN ANALYZE on the slowest ones first.

Owen Martin

Owen Martin

4 months ago

Add indexes where needed and use EXPLAIN ANALYZE.

Taylor Davis

Taylor Davis

4 months ago

The single most important step: EXPLAIN ANALYZE on every query you suspect is slow.

Look for these three patterns in the plan:

1. Full table scans (Seq Scan) This means your WHERE clause isn't hitting an index or no index exists. Add one. The cardinality column will tell you how many rows were checked — if it matches the total row count, you need a B-tree index on those columns.

2. Nested loops over millions of rows Nested loops are O(N*M). On small sets they're fine; on large ones they kill performance. This is usually caused by:

  • A missing join predicate (Cartesian product)
  • Missing indexes on the JOIN condition column
  • The optimizer choosing a bad plan because statistics are stale — run ANALYZE/VACUUM to update them

3. Disk spilling / Temp file creation The 'External merge disk' flag in EXPLAIN

Taylor Davis

Taylor Davis

4 months ago

Most people over-engineer this before finding the obvious bottleneck. Start in order:

  1. EXPLAIN ANALYZE on your slowest query — look for sequential scans where there should be index seeks, and estimate mismatches between rows_sent vs actual_rows. This is 90% of the problem.

  2. Indexing basics:

    • Covering indexes (INCLUDE clause) to eliminate a table lookup entirely
    • Composite indexes — leading column must match your WHERE/JOIN filter
    • Partial/filtered indexes for sparse data (WHERE active = true)
  3. N+1s in ORMs — check if you're fetching 50 queries instead of one JOIN or IN clause

  4. Aggregation at the DB level, not in application code (GROUP BY is faster than Ruby/.NET/Python iteration)

  5. Materialized views for heavy analytical aggregation that doesn't need real-time freshness

Taylor Davis

Taylor Davis

4 months ago

The most common bottlenecks follow a predictable pattern:

1. Missing indexes — 90% of cases. Run EXPLAIN ANALYZE on your slow query. If you see 'Seq Scan' (sequential scan) instead of 'Index Scan', the database is reading every row because no index matches your WHERE/JOIN clause. Add targeted composite indexes, not one-per-column -- a multi-column index covering both filter and join fields is far more efficient.

2. N+1 query problem. If you're fetching a list of objects and then making a separate database call for each child relation, your O(N) round trips will kill performance at any scale. Use Eager Loading (JOINs in SQL or 'include/preload' in ORMs) to fetch everything in one round trip.

*3. The SELECT . Fetching 50 columns when you need 3 wastes memory, network bandwidth, and disk IO. Explicit column selection

Taylor Davis

Taylor Davis

4 months ago

Before you rewrite anything, get EXPLAIN ANALYZE on your slowest query. Look for:

  • Seq Scans on large tables (missing index)
  • Nested loops where a hash join would be faster
  • Temp files being written to disk (work_mem too low)
  • Functions in WHERE clauses that prevent index usage

Common quick wins:

  1. Add indexes — but not blindly, every index slows writes
  2. Avoid SELECT *; fetch only what you need
  3. Denormalize hot read paths if the write volume permits
  4. Use EXPLAIN ANALYZE to find where the plan actually deviates from expectations
Owen Martin

Owen Martin

4 months ago

add indexes on foreign keys and filter columns, run EXPLAIN ANALYZE to see where it

Taylor Davis

Taylor Davis

4 months ago

Most query performance issues fall into three categories, in order of what to check first:

1. Missing indexes. Run EXPLAIN ANALYZE on your slow queries. If you see a Seq Scan (Sequential Scan) on a large table where the WHERE clause uses equality or range filters, that's an index candidate. Don't over-index — every write operation pays for it.

2. N+1 select pattern. Check if your application code is looping over a result set and making another query inside each iteration. Use JOINs at the database level instead of multiple round trips. If you can't rewrite it, use eager loading (preload in Rails/Django) to batch the queries into one IN clause.

3. Denormalization. For read-heavy workloads where joins are becoming the bottleneck, consider duplicating some data into a flattened table or using a materialized view that refreshes asynchronously. It adds write complexity but dramatically reduces read latency.

Taylor Davis

Taylor Davis

4 months ago

Start with EXPLAIN ANALYZE on the slow query — that tells you exactly where time is spent. Common quick wins:

  • Add indexes on columns in WHERE, JOIN, and ORDER BY clauses. Use CONCURRENTLY when adding to production so it doesn't lock writes.
  • Avoid SELECT *. Fetch only the columns you need; reduces I/O and network traffic.
  • Check for N+1 query patterns — one query that fetches 100 rows, then 100 individual queries for related data. Batch them with a single JOIN or IN clause instead.
  • If using JSONB in Postgres: use GIN indexes + the jsonb_path_ops opclass for fast key searches.
  • Denormalize sparingly: if you're doing a join across six tables on every request, that read path might be your bottleneck.

Join the conversation to leave a reply.

Sign in to reply

Related topics