title: SQL Statement Running Slow - Any Ideas?
body: <p>Not sure what's going on with the below statement. It's taking upwards of 5 minutes to run - it only started to show a real slowdown when I wrapped the main statement in a select and aliased it as temp, but doing that has never slowed down any of my other SQL. Any ideas? I'm on MySQL.</p>
  
  <pre><code>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" &lt;&gt; ' '
     or temp."Time Entry Proxy 1" &lt;&gt; ' '
     or temp."Time Entry Proxy 2" &lt;&gt; ' '
  </code></pre>