在使用同一查询中的WHERE条件之前,应用Oracle START WITH … CONNECT BY子句.因此,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
我试图以两种方式提高性能:
查询A:
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