What is Query Optimization?
Analyzing and restructuring database queries — via execution plans, indexes, and rewrites — so they run in less time with fewer resources.
Definition
Query optimization is the practice of making database queries cheaper, guided by the execution plan the database's planner chooses. EXPLAIN ANALYZE in PostgreSQL (or MySQL's EXPLAIN) reveals whether a query uses an index scan or a sequential scan, how joins are executed, and where row estimates go wrong. Typical fixes include adding a covering index, rewriting a correlated subquery as a join, keeping predicates sargable (avoiding functions on indexed columns), and refreshing planner statistics with ANALYZE. Tools like pg_stat_statements surface the queries consuming the most total time, so effort lands where it matters.

