4

我在 UNION ALL 视图上遇到性能问题。这个问题可以通过在两个单独的视图中重写视图来解决,但这违背了创建视图的目的。

这是一个简单的测试用例(Oracle 11.2.0.3.0)。真正的查询使用大约 10 个不同的表,而不仅仅是 3 个。

CREATE TABLE t0 (id number, ref_id number);
CREATE INDEX i0 on t0(id);
CREATE TABLE t1 (id number, amount number);
CREATE INDEX i1 on t1(id);
CREATE TABLE t2 (id number, amount number);
CREATE INDEX i2 on t2(id);


insert into t0 select rownum, rownum * 10 from dual connect by rownum <= 100000;
insert into t1 select rownum, rownum * 10 from dual connect by rownum <= 100000;
insert into t2 select rownum, rownum * 10 from dual connect by rownum <= 100000;



CREATE OR REPLACE VIEW v2
AS
SELECT id, sum(amount) AS total_amount
FROM t1
GROUP BY id;

CREATE OR REPLACE VIEW v3
AS
SELECT id
      ,sum(amount) as total_amount
FROM (SELECT id, amount
      FROM t1
      UNION ALL
      SELECT id, amount
      FROM t2)
GROUP BY id
HAVING sum(amount) <> 0
;

CREATE OR REPLACE view v1
AS
SELECT *
FROM v2
UNION ALL
SELECT *
FROM v3;

以下查询使用 766 次获取。添加 push_pred(a) 对它没有任何作用。

select --+ first_rows
*
from t0, v1 a
where t0.ref_id = a.id
  and t0.id = 1;

下一个查询在 16 get 处触底,尽管它与第一个查询做同样的事情,只是扫描 t0 两次而不是一次。

select --+ first_rows
*
from t0, v2
where  t0.ref_id = v2.id
  and t0.id = 1
union all
select *
from t0, v3
where  t0.ref_id = v3.id
  and t0.id = 1;

我错过了什么?

4

0 回答 0