SQL statements merely specify the results that are desired. They do not contain any information as to how the database system should search for the data. The database system can often choose from multiple options for finding the data required by the SQL statement.
The SQL Optimizer has the task of determining the best search strategy for accessing the data.
A database user or a database application sends an SQL statement with a search condition to the database.
Example
SELECT * FROM hotel.customer
WHERE name BETWEEN 'B' AND 'P' AND firstname = 'Greg'
The database system transforms the SQL statement to the most suitable form.
The database system starts the SQL Optimizer.
The SQL Optimizer determines all the search strategies with which the database can access the data specified in the SQL statement.
The SQL Optimizer determines the costs for every one of these search strategies. For this the SQL Optimizer uses statistics on the database tables.
The SQL Optimizer chooses the search strategy that incurs the lowest costs when executing the SQL statement.
The database system executes the SQL statement with this search strategy.