在同一查询中应用条件之前应用OracleSTART WITH ... CONNECT BY
子句。因此, WHERE 约束无助于优化。WHERE
CONNECT BY
例如,以下查询可能会执行全表扫描(忽略 上的选择性dept_id
):
SELECT * FROM employees
WHERE dept_id = 'SALE'
START WITH manager_id is null
CONNECT BY PRIOR employee_id = manager_id
我尝试通过两种方式提高性能:
查询一:
SELECT * FROM employees
START WITH manager_id is null AND dept_id = 'SALE'
CONNECT BY PRIOR employee_id = manager_id
查询 B:
SELECT * FROM (
SELECT * FROM employees
WHERE dept_id = 'SALE'
)
START WITH manager_id is null
CONNECT BY PRIOR employee_id = manager_id
虽然这两个查询的性能都比原始查询好得多,但在 Oracle 10g 第 2 版中,查询 B 的性能确实比 A 好得多。
CONNECT BY
对于andWHERE
子句,您是否有类似的性能优化来处理?您如何解释查询 B 比查询 A 做得更好?