SQL语句运行缓慢 - 任何想法?

时间:2013-09-25 09:10:01

标签: mysql sql sql-server performance

不确定以下声明发生了什么。运行时间超过5分钟 - 当我将主语句包装在一个选择中并将其作为临时别名时,它才开始显示真正的减速,但这样做从未减慢我的任何其他SQL。有任何想法吗?我在使用MySQL。

select *

from (
  select emp.company 
  , emp.employee
  , emp.last_name "Last Name"
  , emp.first_name "First Name"
  , emp.middle_init "Middle Initial"
  , trim(last_name) || ', ' || trim(first_name) ||  
      decode(trim(middle_init),'','',' '|| trim(middle_init)|| '.') "Full Name"
  , emp.emp_status
  , emp.work_country "Country"
  , (select
    (Case pgs.group_name
      when 'HREMEA' then 'EMEA'
      when 'HRNAMER' then 'NA'
      when 'HRLAMER' then 'LA'
      when 'HRAPAC' then'APAC'
      end)  
      from lawson.pgselect pgs 
      where pgs.begin_value = emp.work_country
        and pgs.company = 1 
        and pgs.group_name in ('HREMEA','HRNAMER','HRLAMER','HRAPAC') ) "Region"
  , emp.department "Department"
  , (select trim(r_name)  
      from lawson.deptcode dpt  
      where company = emp.company  
        and trim(process_level) = trim(emp.process_level) 
        and trim(department) = trim(emp.department)) "Department Description"
  , emp.job_code "Job Code"
  , (select description 
      from lawson.jobcode jbc 
        where company = emp.company 
        and job_code = emp.job_code) "Job Title"
  , emp.supervisor
  , (select trim(last_name) || ', ' || trim(first_name) ||  
      decode(trim(middle_init),'','',' '|| trim(middle_init)|| '.') 
        from lawson.employee supv 
        where supv.company = 1 
          and supv.position = emp.supervisor 
          and term_date = '01-JAN-1700') "Supervisor Name"
  , (select a_field 
      from lawson.hrempusf 
        where company = emp.company and employee = emp.employee 
        and field_key = '99') "Alt Mgr"
  , (select a_field 
      from lawson.hrempusf 
        where company = emp.company and employee = emp.employee 
        and field_key = '79') "TE Proxy Approver"
  , (select a_field 
      from lawson.hrempusf 
        where company = emp.company and employee = emp.employee 
        and field_key = '76') "Time Entry Proxy 1"
  , (select a_field 
      from lawson.hrempusf 
        where company = emp.company and employee = emp.employee
        and field_key = '77') "Time Entry Proxy 2"
  from lawson.employee emp 
  where term_date = '01-JAN-1700'
  ) temp

where temp."TE Proxy Approver" <> ' '
   or temp."Time Entry Proxy 1" <> ' '
   or temp."Time Entry Proxy 2" <> ' '

2 个答案:

答案 0 :(得分:0)

快速查看您的查询:

  • 太多的子查询,用连接替换它们
  • 缺少field_key和term_date
  • 的索引
  • 不需要临时子查询,将其移动到主查询中(联接中的数据较少)

答案 1 :(得分:0)

通过join替换Sub查询,并确保表具有索引