我有一个很大的实体属性值,比如一张表。我尝试使用子查询从该表中选择一些行,然后用行过滤。在这种情况下如何防止合并子查询和主查询?
例如:
EMP:
EMPNO | ENAME | SAL
---------------------
1000 | KING | 10000
1001 | BLAKE | 7500
CREATE VIEW EAV(ID,ATTR,VALUE) AS
select empno, 'name'||ename, ename from emp -- subquery 1
union
select empno, 'sal'||ename, ename from emp -- subquery 2
union
select empno, 'mgr'||ename, ename from emp -- subquery 3
注意:||ename
添加只是为了防止 Oracle 通过向子查询 1 和 3 添加过滤器“(null is not null)”来优化下一个查询
在子查询中,我选择具有属性“sal%”的所有行,然后在主查询中对其进行过滤:
select *
FROM (select id,value from EAV where attr like 'sal%')
WHERE to_number(value) > 5000;
此查询失败导致优化器将子查询与外部查询合并。合并数据库后,尝试将 to_number 应用于“值”列中的所有值,但其中一些具有字符串值。Witch HINT 阻止这种优化?
ps我想得到相同的结果
WITH t as (
select /*+ materialize */ id,value
from eav
where attr like 'sal%')
select * from t where to_number(value) > 5000;
但是,没有 CTE。