10

UNION在某些情况下,UNION ALL查询可以胜过使用OR-connected 谓词的等效查询。据我所知,这部分是因为UNION子选择可以并行执行,因此它们可以有自己的“子计划”,特定于OR-connected 谓词的每个部分,由于更简单的适用查询转换,这可能更加优化。

但是OR,即使将子查询分解应用于UNION ALL解决方案,编写连接的谓词通常也更具可读性和简洁性。我的问题是:有没有办法向 Oracle 表明,一个单一的、昂贵的OR连接谓词应该转换为一个UNION ALL操作?如果有这样的提示/方法,在什么情况下可以应用(例如,谓词所涉及的列上是否需要存在任何约束等)?一个例子:

CREATE TABLE a AS
  SELECT 1 x, 2 y FROM DUAL UNION ALL
  SELECT 2 x, 1 y FROM DUAL;

-- This query...
SELECT * FROM a
WHERE x = 1 OR y = 1

-- Is sometimes outperformed by this one, for more complex table sources...
-- Note: in my case, I can safely apply UNION ALL. I know the two predicates to
-- be mutually exclusive.
SELECT * FROM a
WHERE x = 1
UNION ALL
SELECT * FROM a
WHERE y = 1

请注意,我知道/*+ USE_CONCAT */提示:

SELECT /*+ USE_CONCAT */ * FROM a
WHERE x = 1 OR y = 1

但它似乎并没有产生我需要UNION ALL的东西(执行计划中没有强制操作):

-------------------------------------------
| Id  | Operation         | Name | E-Rows |
-------------------------------------------
|   0 | SELECT STATEMENT  |      |        |
|*  1 |  TABLE ACCESS FULL| A    |      2 |
-------------------------------------------

也许,这个提示有一些限制?我为此提供了 Oracle 11g2。

4

2 回答 2

3

我相信这可能与您在OR谓词中使用的列上存在的索引有关。

我在 11gR2 中使用以下内容进行了测试。

create table scott.test as 
select level l, 
       decode(mod(level,2), 1, 1, 2) x, 
       decode(mod(level,2), 1, 2, 1) y, 
       dbms_random.value(1, 3) z from dual 
connect by level < 1000;
/

begin
   dbms_stats.gather_table_stats('scott', 'test');
end;
/

然后我在 TOAD 中解释了以下查询,( EXPLAIN PLAN FOR)

select x, y, z from scott.test
    where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
    ;

SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          4                                
  TABLE ACCESS FULL COS_DM.TEST 10      280     4   

select /*+ USE_CONCAT */ x, y, z from scott.test
where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
;

SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          4                                
  TABLE ACCESS FULL COS_DM.TEST 10      280     4                                


select x, y, z from test where (floor(z) = 1 and x = 1)
union all
select x, y, z from test where (floor(z) = 2 and y = 1)
;

SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          8                                
  UNION-ALL                                              
    TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
    TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                

所以看起来提示不起作用。然后我在 x & y 列中添加了一个索引:

create index test_x on test (x, y);

begin
   dbms_stats.gather_table_stats('scott', 'test');
end;
/

现在重新运行查询:

select x, y, z from scott.test
    where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
    ;

SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          4                                
  TABLE ACCESS FULL COS_DM.TEST 10      280     4   

select /*+ USE_CONCAT */ x, y, z from scott.test
where (floor(z) = 1 and x = 1) or (floor(z) = 2 and y = 1)
;

SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          8                                
  CONCATENATION                                              
    TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
    TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                

select x, y, z from test where (floor(z) = 1 and x = 1)
union all
select x, y, z from test where (floor(z) = 2 and y = 1)
;

SELECT STATEMENT Optimizer Mode=ALL_ROWS        10          8                                
  UNION-ALL                                              
    TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                
    TABLE ACCESS FULL   COS_DM.TEST 5   140     4                                

似乎在添加索引之后(即使它没有被使用)优化器决定使用提示!

也许你可以试试这个?

于 2012-05-08T14:41:14.700 回答
0

在某些情况下,优化器会忽略提示,您必须以不同的方式编写查询。使用 UNION ALL 重写所有查询的替代方法,您可以只重写子句:

SELECT * FROM a /* ... you can put here more joins with many tables ... */
WHERE a.rowid in (
    select innerQry.rowid from a innerQry where /*your first clause of OR*/innerQry.x = 1
    union all
    select innerQry.rowid from a innerQry where /*your second clause of OR*/innerQry.y = 1
)
于 2018-07-23T09:56:40.417 回答