ここでたくさんの回答を読みましたが、思ったほどうまく機能しているものはありません。現在、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
結果:
+----+--------------------+-------+--------+------ ---------------+------------+---------+----------- ----+---------------------+----------------------------- ----------------------+ | | ID | select_type | テーブル | タイプ | 可能な_キー | キー | key_len | 参照 | 行 | 行 エクストラ | +----+--------------------+-------+--------+------ ---------------+------------+---------+----------- ----+---------------------+----------------------------- ----------------------+ | | 1 | プライマリ | d | すべて | プライマリ | | | | | | | 10 | where を使用します。一時的な使用; ファイルソートの使用 | | | 1 | プライマリ | | | 参照 | PRIMARY,emp_status | emp_status | 5 | Orangehrm_li.d.id | 48 | where | の使用 | | 1 | プライマリ | b | 参照 | emp_number,skill_id | 従業員番号 | 4 | Orangehrm_li.a.emp_number | 1 | | | | | 1 | プライマリ | c | eq_ref | プライマリ | プライマリ | 4 | orangehrm_li.b.skill_id | 1 | where | の使用 | | 3 | 従属サブクエリ | e | すべて | | | | | | | | | 28165 | where を使用します。ファイルソートの使用 | | | 2 | 従属サブクエリ | e | すべて | | | | | | | | | 28165 | where を使用します。ファイルソートの使用 | +----+--------------------+-------+--------+------ ---------------+------------+---------+----------- ----+---------------------+----------------------------- ----------------------+