Glossary

Query optimization

The process, largely automated by the database, of choosing the fastest execution plan for a given query.

Query optimization is the process of choosing an efficient way to execute a query out of the many equivalent execution plans that could produce the same result — which table to scan first, which join algorithm to use, whether to use an index or a partition to skip data. In most systems this is handled automatically by a query optimizer inside the query engine, based on statistics about the data's size and distribution.

The optimizer typically estimates the cost of several candidate plans and picks the cheapest one, but its estimates depend on accurate, up-to-date statistics; stale statistics after a large data change are a common cause of the optimizer choosing a poor plan. Analysts and engineers influence optimization indirectly, largely through schema design choices such as partitioning, clustering keys, or well-chosen materialized views, rather than by hand-specifying the execution plan itself.

Query optimization matters because the same logical query can run in milliseconds or hours depending on the plan chosen, and understanding a query's execution plan, usually available via an EXPLAIN command, is the standard way to diagnose a slow query. A frequent pitfall is optimizing SQL syntax under the assumption it directly controls execution order — a declarative query's phrasing rarely matters as much as the underlying data layout and available statistics the optimizer actually works from.

Last reviewed September 22, 2026

In the index now

Related terms

Related tools

Related guides