您不应依赖执行成本来优化查询。重要的是执行时间(在某些情况下是资源使用情况)。
从概念指南:
成本是与使用特定计划执行语句所需的预期资源使用成比例的估计值。
当估计关闭时,通常是因为优化器可用的统计信息具有误导性。您可以通过为优化器提供更准确的统计信息来纠正此问题。检查统计信息是否是最新的。如果是这样,您可以收集其他统计信息,例如通过启用在数据倾斜列上手动创建直方图的动态统计信息收集。
另一个可以解释相对成本和执行时间之间差异的因素是优化器建立在简单的假设之上。例如:
- 没有直方图,列中的每个值都是均匀分布的
- 相等运算符将选择 5% 的行(没有直方图或动态统计信息)
- 每列中的数据独立于其他每列中的数据
- Furthermore, for queries with bind variables, a single cost is computed for further executions (even if the bind value change, possibly modifying the cardinality of the query)
- ...
These assumptions are made so that the optimizer can return an execution cost that is a single figure (and not an interval). For most queries these approximation don't matter much and the result is good enough.
However, you may find that sometimes the situation is simply too complex for the optimizer and even gathering extra statistics doesn't help. In that case you'll have to manually optimize the query, either by adding hints yourself, by rewriting the query or by using Oracle tools (such as SQL profiles).
If Oracle could devise a way to accurately determine the execution cost, we would never need to optimize a query manually in the first place !