【问题标题】:oracle sql performance in large dataset大型数据集中的 oracle sql 性能
【发布时间】:2018-01-27 22:08:28
【问题描述】:

我有一个这样的查询。

我使用 Jpa 并且只检索前 50 个结果,但在我的表上有 200 万条记录需要很长时间。

如何提高性能?

SELECT * FROM TRANSACTION 
WHERE
   (trunc(REQUEST_TIME,'MI') between to_date('1390/01/01 01:01','YYYY/MM/DD HH24:MI','nls_calendar=persian') 
                                 and to_date('1396/11/01 01:01','YYYY/MM/DD HH24:MI','nls_calendar=persian'))
and  CUSTOMER like '%123%'
and  (case when (ERROR_CODE is not null and ERROR_CODE <> 200) then -1 
           when (ERROR_CODE is not null and ERROR_CODE =200) then 200 
       else 0 end =0)
and (URL = 'url1')
and ( SOURCE like '%123%')
and ( ERROR_CODE=200) 
and ( REQUEST_ID like '%1234%')

【问题讨论】:

  • 您在ERROR_CODE 上的条件是矛盾的。尤其是 CASE 表达式,除非 ERROR_CODENULL,否则永远不会有匹配项。但这会被后面的条件过滤掉。
  • 你的桌子上有什么索引?

标签: sql oracle performance spring-data large-data


【解决方案1】:

从这里删除TRUNC 函数,这是不必要的,但会阻止RDBMS 使用列REQUEST_TIME 上的索引(如果有的话):

   (trunc(REQUEST_TIME,'MI') between .......

从这里删除ERROR_CODE is not null条件,如果ERROR_CODE &lt;&gt; 200ERROR_CODE =200,那么它总是不能为空:

case when (ERROR_CODE is not null  and ERROR_CODE <> 200)
then -1 when (ERROR_CODE is not null and ERROR_CODE =200)
then 200 else 0 end =0)

如果你简化上述条件,你会得到:

case when ERROR_CODE <> 200 then -1 
     when ERROR_CODE =200 then 200 
     else 0 
end =0

如果您检查上面的简化条件,很明显它只检查“else 0”部分,因此可以进一步简化为:

ERROR_CODE IS NULL

但由于您的查询and ( ERROR_CODE=200) 中有另一个条件,第一个条件不包括另一个条件and ( ERROR_CODE=200),所以我认为您没有在问题中向我们展示真正的查询。我会简单地删除这个条件,因为它很可能是一个逻辑错误。


经过上面的简化,你会得到:

SELECT * FROM TRANSACTION 
WHERE
  REQUEST_TIME between to_date('1390/01/01 01:01','YYYY/MM/DD HH24:MI','nls_calendar=persian') 
                   and to_date('1396/11/01 01:01','YYYY/MM/DD HH24:MI','nls_calendar=persian')
  and  CUSTOMER like '%123%'
  and (URL = 'url1')
  and ( SOURCE like '%123%')
  and ( ERROR_CODE=200) and( REQUEST_ID like '%1234%')

现在确保在REQUEST_TIME 上创建了索引,如果没有,则创建它并验证查询的性能。

【讨论】:

    【解决方案2】:

    前提是这不是临时(很少使用)类型的查询。
    trunc(REQUEST_TIME,'MI') 上的功能索引定义为列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 2013-04-20
      • 2018-12-14
      • 2019-06-29
      • 2014-02-15
      • 2017-11-13
      相关资源
      最近更新 更多