类似的查询具有不同的执行时间

时间:2012-07-19 14:48:36

标签: oracle

我有以下问题:

SELECT nvl(sum(adjust1),0)
FROM (
  SELECT
    ManyOperationsOnFieldX adjust1,
    a, b, c, d, e
  FROM (
    SELECT
      a, b, c, d, e,
      SubStr(balance, INSTR(balance, '[&&2~', 1, 1)) X
    FROM
      table
    WHERE
      a >= To_Date('&&1','YYYYMMDD')
      AND a < To_Date('&&1','YYYYMMDD')+1
  )
)
WHERE
  b LIKE ...
  AND e IS NULL
  AND adjust1>0
  AND (b NOT IN ('...','...','...'))
  OR  (b = '... AND c <> NULL)

我试图将其更改为:

SELECT nvl(sum(adjust1),0)
FROM (
  SELECT
    ManyOperationsOnFieldX adjust1
  FROM (
    SELECT
      SubStr(balance, INSTR(balance, '[&&2~', 1, 1)) X
    FROM
      table
    WHERE
      a >= To_Date('&&1','YYYYMMDD')
      AND a < To_Date('&&1','YYYYMMDD')+1
      AND b LIKE '..'
      AND e IS NULL
      AND (b NOT IN ('..','..','..'))
      OR  (b='..' AND c <> NULL)
  )
)
WHERE
  adjust1>0

Mi的意图是在最里面的查询中进行所有过滤,并且只给外部的X,这是我必须经常操作的那个。但是,firts(原始)查询执行需要几秒钟,而第二个查询甚至不会完成。我等了差不多20分钟,但我仍然得不到答案。

是否有明显的原因导致我可能会忽略这一点?

这些是他们每个人的计划:

SELECT STATEMENT optimizer=all_rows (cost = 973 Card = 1 bytes = 288)
  SORT (aggregate)
    PARTITION RANGE (single) (cost=973 Card = 3 bytes = 864)
      TABLE ACCESS (full) OF "table" #3 TABLE Optimizer = analyzed(cost=973 Card = 3 bytes=564)


SELECT STATEMENT optimizer=all_rows (cost = 750.354 Card = 1 bytes = 288)
  SORT (aggregate)
    PARTITION RANGE (ALL) (cost=759.354 Cart = 64.339 bytes = 18.529.632)
      TABLE ACCESS (full) OF "table" #3 TABLE Optimizer = analyzed(cost=750.354 Card = 64.339 bytes=18.529.632)

2 个答案:

答案 0 :(得分:3)

您的两个查询不完全相同。

logical operator AND is evaluated before the operator OR

SQL> WITH data AS
  2          (SELECT rownum id
  3             FROM dual
  4           CONNECT BY level <= 10)
  5  SELECT *
  6    FROM data
  7   WHERE id = 2
  8     AND id = 3
  9      OR  id = 5;

        ID
----------
         5

所以你的第一个查询意味着:当数据是这样时,在这个分区上给我一个大的SUM

你的第二个查询意味着:给我一个大的SUM结束(当数据是这样时这个分区)或(当数据是这种方式时[没有分区消除因此大全扫描])

混合逻辑运算符ANDOR时要小心。我的建议是使用括号以避免混淆。

答案 1 :(得分:2)

这是关于你的OR ...试试这个:

SELECT nvl(sum(adjust1),0)
FROM (
  SELECT
    ManyOperationsOnFieldX adjust1
  FROM (
    SELECT
      SubStr(balance, INSTR(balance, '[&&2~', 1, 1)) X
    FROM
      table
    WHERE
      a >= To_Date('&&1','YYYYMMDD')
      AND a < To_Date('&&1','YYYYMMDD')+1
      AND (
          b LIKE '..'
          AND e IS NULL
          AND (b NOT IN ('..','..','..'))
          OR  (b='..' AND c <> NULL)
      )
  )
)
WHERE
  adjust1>0

因为你的AND语句的OR内联没有括号,所以第二个版本不会将检查的数据限制为只包含在日期过滤器中的行。有关详细信息,请参阅Condition Precedence

的文档