我在这里阅读了大量的回复,但没有什么能像我想要的那样有效。我目前有一个包含 2 个子查询的工作查询,问题是执行大约需要 10 秒。我想知道是否有任何方法可以使这更快,也许加入。我似乎无法摆脱它所在的盒子。请让我知道你的想法。
这是工作查询:
Select concat(a.emp_firstname, ' ', a.emp_lastname) as names
, if(if (a.emp_gender = 1, 'Male', a.emp_gender)=2, 'Female',
if (a.emp_gender = 1, 'Male', a.emp_gender)) as emp_gender
, c.name
, a.emp_work_telephone
, a.emp_hm_telephone, a.emp_work_email
, a.custom7, a.employee_id
, a.city_code, a.provin_code, d.name as status,
(SELECT cast(concat(DATE_FORMAT(e.app_datetime, '%H:%i'), ' ', e.app_facility) as char(100))
FROM li_appointments.li_appointments as e where e.terp_id = a.employee_id
and e.app_datetime <= str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p')
and date(e.app_datetime) = date(str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p'))
order by e.app_datetime desc limit 1) as prevapp,
(SELECT cast(concat(DATE_FORMAT(e.app_datetime, '%H:%i'), ' ', e.app_facility) as char(100))
FROM li_appointments.li_appointments as e
where e.terp_id = a.employee_id
and e.app_datetime > str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p')
and date(e.app_datetime) = date(str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p'))
order by e.app_datetime desc limit 1) as nextapp
from hs_hr_employee as a
Join hs_hr_emp_skill as b on a.emp_number = b.emp_number
Join ohrm_skill as c on b.skill_id = c.id
Join orangehrm_li.ohrm_employment_status as d on a.emp_status = d.id
where c.name like '%Arabic%'
and d.name = 'Active' order by rand();
EXPLAIN
结果:
+----+--------+--------+--------+------ ---------------+------------+---------+----------- ----------------+--------+---------- ---------------------+ | 编号 | 选择类型 | 表| 类型 | 可能的键 | 关键 | key_len | 参考 | 行 | 额外 | +----+--------+--------+--------+------ ---------------+------------+---------+----------- ----------------+--------+---------- ---------------------+ | 1 | 初级 | d | 全部 | 初级 | | | | 10 | 使用哪里;使用临时的;使用文件排序 | | 1 | 初级 | 一个 | 参考 | PRIMARY,emp_status | emp_status | 5 | orangehrm_li.d.id | 48 | 使用位置 | | 1 | 初级 | 乙 | 参考 | emp_number,skill_id | emp_number | 4 | orangehrm_li.a.emp_number | 1 | | | 1 | 初级 | c | eq_ref | 初级 | 初级 | 4 | orangehrm_li.b.skill_id | 1 | 使用位置 | | 3 | 依赖子查询 | 电子| 全部 | | | | | 28165 | 使用哪里;使用文件排序 | | 2 | 依赖子查询 | 电子| 全部 | | | | | 28165 | 使用哪里;使用文件排序 | +----+--------+--------+--------+------ ---------------+------------+---------+----------- ----------------+--------+---------- ---------------------+