启用mysql中的slow_query_log。我发现下面的查询是15秒或更长时间内最慢的查询之一。如何优化此查询。所有表都是Innodb和 服务器:服务器版本:5.6谢谢
EXPLAIN EXTENDED SELECT
ntv_staffs.fname,
ntv_staffs.id,
ntv_staffs.mname,
ntv_staffs.profile_pic,
ntv_staffs.lname,
ntv_staff_office.cug,
ntv_staff_office.extension,
ntv_staff_office.off_email,
ntv_staff_office.job_title,
ntv_designations.`name` as designation_name,
ntv_branches.`name` as branch_name,
ntv_departments.`name` as department_name,
ntv_departments.id as department_id
FROM
ntv_staffs
INNER JOIN ntv_staff_office ON ntv_staffs.id = ntv_staff_office.pid
INNER JOIN ntv_departments ON ntv_staff_office.department_id = ntv_departments.id
INNER JOIN ntv_branches ON ntv_staff_office.branch_id = ntv_branches.id
INNER JOIN ntv_designations ON ntv_staff_office.designation = ntv_designations.id
where ntv_staffs.id='662';
+----+-------------+------------------+--------+---------------+---------+---------+----------------------------------------------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------------+--------+---------------+---------+---------+----------------------------------------------+------+----------+-------------+
| 1 | SIMPLE | ntv_staffs | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | |
| 1 | SIMPLE | ntv_staff_office | ALL | NULL | NULL | NULL | NULL | 247 | 100.00 | Using where |
| 1 | SIMPLE | ntv_branches | eq_ref | PRIMARY | PRIMARY | 4 | abc_portal_new.sks_staff_office.branch_id | 1 | 100.00 | |
| 1 | SIMPLE | ntv_designations | eq_ref | PRIMARY | PRIMARY | 4 | abc_portal_new.sks_staff_office.designation | 1 | 100.00 | Using where |
| 1 | SIMPLE | ntv_departments | eq_ref | PRIMARY | PRIMARY | 4 | abc_portal_new.sks_staff_office.department_id | 1 | 100.00 | |
+----+-------------+------------------+--------+---------------+---------+---------+----------------------------------------------+------+----------+-------------+
答案 0 :(得分:2)
尝试为ntv_staff_office.pid
设置btree-index。