使用Left Outer Join会产生意外的查询运行时结果

时间:2014-07-23 15:34:52

标签: sql performance oracle left-join

我遇到了一个我无法理解的有趣问题。本质上,我正在使用一些Oracle表,并且必须修改一些视图创建语句,特别是where子句中的exists语句。这包括通过将其与另一个表连接并更改QUERY A的where子句来稍微修改exists子句中的QUERY A.我认为有两种方法可以解决这个问题:

  1. 联合基于QUERY A的新查询,附加联接和不同的where子句或

  2. 使用左外连接修改QUERY A(所以旧连接左外连接新表),然后添加一个或到where子句(旧条件或新条件)。

  3. 这是代码的模拟:

    select count(*)
    from table a
    where condition and exists (
        select *
        from table1 join table2 on cond
            join table3 on cond
            join table4 on cond
        where cond
        union all
        select *
        from table1 join table2 on cond
            join table3 on cond
            join table4 on cond
            join table5 on cond
        where cond2);
    

    VS

    select count(*)
    from table a
    where condition and exists (
        select *
        from table1 join table2 on cond
            join table3 on cond
            join table4 on cond
            left outer join table5 on cond
        where cond or cond2);
    

    所以我期望第二个版本运行得更快,因为它没有两次执行前3个连接;然而,事实证明版本2已经永远运行,而版本1相对较快。为什么我会看到这些类型的结果?是否与2相同的错误逻辑有关?或者Oracle查询优化器是否在不同方面处理它们?或者它可能与索引有关?这里没有什么需要解决的,但我想理解为什么结果会这样。任何见解都表示赞赏!

0 个答案:

没有答案
相关问题