What is query optimization and what is a query execution plan?

Answer

The query optimizer is a component of the RDBMS that analyzes SQL queries and determines the most efficient way to execute them — choosing join algorithms, join order, and whether to use indexes. The query execution plan (or explain plan) is the optimizer's chosen strategy. Use EXPLAIN SELECT ... (MySQL/PostgreSQL) to see the plan. Key operators: Seq Scan (full table scan — potentially slow), Index Scan (uses index — fast), Hash Join, Nested Loop Join, Merge Join. Look for high row estimates, missing indexes, and expensive sorts. The optimizer uses table statistics (row counts, column cardinality) — run ANALYZE (PostgreSQL) to update them.