Production Database Engineering · Lesson 9

PostgreSQL and MySQL Query Plans and Statistics Explained

Interpret estimates, runtime plans, statistics, parameters, and index choices using each engine's documented evidence boundaries.

Quick answer

A query plan is an engine decision based on SQL shape, available access paths, statistics, configuration, and parameter values. Diagnose the Spring order service by comparing estimates with representative execution evidence. Do not compare PostgreSQL and MySQL plan fields as though they were one portable format.

Learning objectives

  • Explain how estimates, join order, access paths, sorting, memory, and actual row flow influence an order query.
  • Collect safe PostgreSQL 18 and MySQL 8.4 plan evidence without leaking customer parameters.

Prerequisites

Understand indexes, joins, predicates, transactions, and why production data distribution differs from tiny fixtures.

Shared relational contract

Begin with the user impact and exact query family, not a guessed index. Preserve placeholders, plan identity, application release, and schema version while redacting values. A large estimate error can point to stale statistics, correlated columns, skew, or a parameter-sensitive workload. Runtime instrumentation may execute the query, so use read-only or isolated evidence procedures when effects are possible.

An index that speeds one read adds write and maintenance cost. Validate the entire workload and durable order invariant after a change.

PostgreSQL 18 boundary

PostgreSQL EXPLAIN exposes plan nodes, costs, rows, width, and optional execution instrumentation. ANALYZE actually runs the statement. Statistics targets, extended statistics, prepared-plan choices, buffers, loops, and node timing have PostgreSQL meanings. Use the official PostgreSQL 18 plan and statistics documentation for interpretation.

MySQL 8.4 InnoDB boundary

MySQL offers tabular, JSON, tree, and analyze forms with its own access types, iterator timing, loop counts, cost model, histograms, and optimizer trace boundaries. InnoDB clustered primary keys affect secondary access. Validate 8.4 syntax and execution behavior before collecting evidence.

Production failure scenario

After a data import, the recent-orders endpoint becomes slow. The plan estimates a few rows but processes many. Preserve the plan safely, inspect statistics freshness and data skew, then test a bounded remediation. Do not log customer IDs or paste unredacted statements into a ticket. Verify latency distribution, database load, writes, and correct order results.

Common misconceptions

  • A sequential scan is not automatically wrong.
  • Estimated cost is not elapsed milliseconds.
  • One fast execution is not a benchmark.
  • Similar EXPLAIN labels do not imply identical semantics.

Decision checklist

  • Identify query family and user impact.
  • Redact parameters while preserving shape.
  • Compare estimates, actual rows, loops, and access paths.
  • Check statistics and schema identity.
  • Test write cost and representative correctness.

Review connection pooling, continue to migration and rollback, use the course and topic, deepen with PostgreSQL EXPLAIN, and connect to Spring Data JPA query performance.

Official sources

Knowledge check

Check your understanding

Answer both questions correctly to mark this lesson as mastered. You can retry without penalty.

1. A plan estimate is far below actual order rows; what is a safe next step?

2. Can PostgreSQL EXPLAIN ANALYZE output be compared field-for-field with MySQL EXPLAIN ANALYZE?