【问题标题】:Select query works too long选择查询工作时间过长
【发布时间】:2021-05-24 08:59:14
【问题描述】:

我有选择查询,它使用子查询来产生结果:

SELECT 
  fu.user_name       "User name"
  ,  frt.responsibility_name "Responsibility Name" 
  , furg.start_date     "Start Date" 
  , furg.end_date      "End Date" 
  , fu.last_logon_date    "Last Logon Date"
  , fr.responsibility_key  "Responsibility key"
  , fu.email_address     "Email Address"
  , fu.description      "Description" 
  , m.MANAGER_NAME
  , m.MANAGER_EMAIL   
FROM
  fnd_user_resp_groups_direct   furg
, fnd_user        fu
, applsys.fnd_responsibility_tl  frt
, applsys.fnd_responsibility   fr
---------------- this subquery ------------------------
, (select distinct e.employee_number employee_id, e.full_name employee,e.EMAIL_ADDRESS employee_email, 
  m.employee_number manager_id, m.full_name manager_name,  m.EMAIL_ADDRESS as manager_email 
  from EMPDATA_IMPORT_STG e
      ,EMPDATA_IMPORT_STG m
where m.employee_number=e.supervisor_name
) m
---------------------------------------
WHERE  furg.user_id = fu.user_id
  AND furg.responsibility_id = frt.responsibility_id
  AND fr.responsibility_id = frt.responsibility_id 
  AND (to_char(fu.END_DATE) is null  OR fu.END_DATE > sysdate)
 and fu.email_address=m.EMPLOYEE_EMAIL
 and not (frt.responsibility_name like '%iExpenses%' and frt.responsibility_name not like '%iExpenses Setup and Admin%')
  and frt.responsibility_name not like '%Expenses Auditor%' 
  and frt.responsibility_name not like '%Notifications%'
  and frt.responsibility_name not like '%Inquiry%' and frt.responsibility_name not like '%INQUIRY%'
  and frt.responsibility_name not like '%Self-Service%'  and frt.responsibility_name not like '%Self Service%'
  and frt.responsibility_name not like '%(Read Only)%'  and frt.responsibility_name not like '%Internet Expenses Help Desk%'
  and frt.responsibility_name not like '%Employee Opportunities%'

我的问题是 EMPDATA_IMPORT_STG 表有超过 200 万条记录,并且在其列 EMPLOYEE_NUMBER、FILE_TIMESTAMP 上创建了非唯一索引。我试图在生产中执行它并开始执行,我等了 4 分钟并终止了我的会话。顺便提一下,在测试环境中,相同的查询会在 10 秒内执行。

如何在生产环境中加快查询执行速度?

每天都会在 prod 中收集统计信息,我也在考虑在电子邮件列上创建索引,这样可以让我更快地响应吗?

谢谢

【问题讨论】:

  • 影响查询性能的因素有很多。因此,仅查看 SELECT 语句并说“这样做”是不可能回答您的问题的。请阅读 this post 关于询问 Oracle 调优问题:它将帮助您了解您需要提供的信息,然后我们才能回答您的问题。
  • 首先,你为什么不使用ANSI连接语法?其次,检查查询计划是否使用了正确的索引,然后您可以相应地修改列索引,并检查查询优化器是否使用哈希连接或排序合并连接或嵌套循环连接。
  • 不管怎样,column NOT LIKE '%constant%' 是一个臭名昭著的性能反模式。它破坏了在column 上使用索引。你有很多这样的。您可能会遇到性能不佳的问题。
  • EMPDATA_IMPORT_STG .FILE_TIMESTAMP 的意义是什么?您应该使用它来过滤内联视图还是限制其连接?否则,您可能会将多个员工实例加入多个主管实例,而该产品是导致执行时间延长的原因。
  • edit 向我们展示您的EMPDATA_IMPORT_STG 表上的表定义和所有索引。

标签: oracle plsql query-optimization


【解决方案1】:

因为这个查询是每月按时执行的,所以我创建了临时表,通过使用与原始表不同的employee_number 来保存这些员工:

  create table xx_employees as select DISTINCT employee_number, 
       full_name, 
       supervisor_name,
       EMAIL_ADDRESS 
from EMPDATA_IMPORT_STG ;

这解决了我的问题,因为不幸的是我有非常小的时间窗口来提供这个查询的结果。

再次向大家道歉。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2018-12-07
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多