Skip to content

OPTIMIZER_COSTS

SET optimizer_trace="enabled=on";
SELECT * FROM my_table;
SHOW WARNINGS;
SET optimizer_trace="enabled=off";
+-----------+------+----------------------------------------------------+
| Level | Code | Message |
+-----------+------+----------------------------------------------------+
| Warning | 1003 | /*+ OPTIMIZER_COSTS <cost_model>=1.23 /> SELECT... |
+-----------+------+----------------------------------------------------+

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.