繁体   English   中英

大型数据集中的oracle sql性能

[英]oracle sql performance in large dataset

我有这样的查询。

我使用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%')

从此处删除TRUNC函数,这是不必要的,但可以防止RDBMS在REQUEST_TIME列上使用索引(如果有):

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

从此处删除ERROR_CODE is not null条件,如果ERROR_CODE <> 200ERROR_CODE =200 ,则它必须始终为NOT NULL:

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创建了一个索引,如果没有,请创建该索引并验证查询的性能。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM