Query optimization is the process of finding the most efficient way to execute a database query.
When you write a SQL query, you’re basically telling the database what data you want, but the database has to figure out how to actually retrieve it. That’s the main job of the query optimizer. The query optimizer is a dedicated component of the database management system (DBMS) that evaluates various possible execution paths and selects the most efficient one.
But there are also things that we can do to help the query optimizer, such as writing efficient SQL, properly indexing tables, maintaining up-to-date statistics, etc.
Understanding how the optimizer works and knowing how to steer it toward better execution plans is what we mean by query optimization.