OPTIMIZER_COSTS
OPTIMIZER_COSTS is an Oracle-specific compilation hint used in the SQL language to fine-tune query execution plans. It lets the Oracle database know whether to consider the computational cost (like I/O, CPU usage) when deciding on an execution plan. Based on this hint, the Oracle SQL optimizer prioritizes the decision-making process, either towards cost-based or rule-based execution.
Example
Output
Explanation
In the given SQL code, the optimizer_trace
variable is initially set to "enabled=on"
. After this point, any query run on the server will generate the optimizer cost detail. Once we run the SELECT * FROM my_table;
statement, we can get the optimizer trace details with SHOW WARNINGS;
as the trace details are treated as warnings. Turning off the feature by SET optimizer_trace="enabled=off"
ensures that no further statements will generate these details. The output returns a warning message including the optimizer costs along with execution plan generated by the optimizer for the select query.
Example
Output
Explanation
The “optimizer_costs” configuration option can influence the query planner’s decisions in PostgreSQL. Setting optimizer_cost
to ‘enable’ makes the query planner use cost-based decisions.
The EXPLAIN command is then used to output the plan that the PostgreSQL planner generates for the given query. This output includes the cost of each operation the planner considers.
In the described output, the “Seq Scan on books” represents a full table scan on the Books table. The costs (minimum and estimated maximum) are given, alongside the actual time taken, the amount of rows processed, and some loop info. The filter indicates the condition used to filter the data. The row removal description provides additional information about the rows eliminated by the filter.
Example
Output
Explanation
The specified SQL code first adjusts the optimizer to use the IO/CPU cost model via OPTIMIZER_COSTS = IO_CPU_COST
for the current session. Then, it retrieves all columns for the employee with employee_id
of 100
from the employees
table.