2017-06-10 167 views
2

我調試了一個MYSQL查詢,這花費了太多的時間。MYSQL查詢需要花費太多時間,使用where,group by和order by

查詢是這樣的:

SELECT *, TM.tutor_id as tutor_id, 
    TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) AS age 
FROM tutor_master as TM 
LEFT JOIN category_master as CM on CM.category_id=TM.category 
LEFT JOIN tutor_expected_rate TER ON FIND_IN_SET(TER.tutor_id, TM.tutor_id) > 0 
LEFT JOIN admin_shortlist_master SHM ON TM.tutor_id = SHM.tutor_id 
    AND (SHM.user_auth_id = 'c84258e9c39059a89ab77d846ddab909') 
LEFT JOIN level_master LM ON FIND_IN_SET(LM.level_id, TER.level_id) > 0 
WHERE 1=1 
GROUP BY TM.tutor_id 
order by TM.is_priority DESC, TM.tutor_id DESC LIMIT 0, 10 

如果我不包括

WHERE 1=1 
GROUP BY TM.tutor_id 
ORDER BY TM.is_priority DESC, TM.tutor_id DESC LIMIT 0, 10 

查詢是這樣執行的部分:

顯示行0 - 29(27,649查詢花了0.2339秒)

對於完全查詢它是這樣的:

顯示行0 - 9(10總計,查詢花費115.4066秒)

我有它編入索引的查詢中使用的所有字段。

MySQL的解釋是這樣的:

id select_type  table type possible_keys key  key_len  ref  rows Extra 
1 SIMPLE   TM  ALL  NULL NULL NULL NULL 27530 Using temporary; Using filesort 
1 SIMPLE   CM  eq_ref PRIMARY  PRIMARY  4 toprecru_portal_db.TM.category 1 
1 SIMPLE   TER  ALL  NULL NULL NULL NULL 13223 
1 SIMPLE   SHM  ref  tutor_id,user_auth_id user_auth_id 257  const 1 
1 SIMPLE   LM  ALL  NULL NULL NULL NULL 11 

更新:

我讀的地方,使用1 = 1沒有性能影響,但添加1 = 1,即使查詢約需從0.2339秒開始70秒。

MYSQL的my.cnf設置是這樣的:

read_buffer_size = 512M 
join_buffer_size = 512M 
sort_buffer_size = 256M 

更新:

使用GROUP_BY沒有ORDER_BY的時候幾乎是一樣的,如:

顯示行0 - 29 (總計27,530,查詢花了114.9642秒)

upda TE樣品日期: 表 - 導師預期速率

id  tutor_id level_id exp_rate 
1  27597   4 $30-35/hr 
2  27597  10 $40-45/hr 
99  27598   5 35-40/hr minimum 
124  27602   4 25-30/hr or 30-40/hr minimum 1.5hrs per session 
125  27602   0 

表 - admin_shortlist_master

admin_shortlist_id job_ad_id request_profile_id tutor_id user_auth_id added_date 
143693     0    0    22692 4ef44ea2203114a3e27eaff31a1bf3be 2015-09-17 
143694     0    0    11653 4ef44ea2203114a3e27eaff31a1bf3be 2015-09-17 
143695     0    0    27611 4ef44ea2203114a3e27eaff31a1bf3be 2015-09-17 
143696     0    0    27610 4ef44ea2203114a3e27eaff31a1bf3be 2015-09-17 
296793     0    13    0 21232f297a57a5a743894a0e4a801fc3 2015-10-05 

表 - 類別主

category_id  category_name   disp_order status 0-Not Published, 1-Published 
1    Polytechnic Student  0    1 
2    Diploma Grad Part Time 1    1 
3    Diploma Grad Full Time 2    1 
4    JC Student    3    0 
5    A Level Grad Part Time 4    1 

表 - 水平主

level_id level_name   class_title class_ids   subject_ids status 0-Not Published, 1-Published 
4   Primary    Primary  4,5,6,7,8,9,10  5,6,7,8,9,10,11,12,14,15 1 
5   O Level (Secondary) Secondary  12,13,14,15,16 
4,5,6,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24... 1 
6   A Level (JC)   JC    18,19,20 6,17,18,19,23,24,25,26,30,31,32,33,34,35,36,37,38,... 1 
7   International Baccalaureate    17,18,19,23,24,34,40,41,42,43,44,45,46,47,48,49,50... 1 
8   Diploma    26,34,47,54,55,56,57,59,60,61,62,63,64,65,66,67,68... 1 

UPDATE:

select tutor_id, category, is_priority, birthdate from tutor_master order by tutor_id limit 5 
tutor_id Ascending 1 category is_priority  birthdate 
4       8    0   1989-01-01 
7       8    0   1987-01-01 
8       8    0   1964-01-01 
9       2    0   1987-01-01 
10       8    0   1983-01-01 
+0

@Bamar - 其實我正在調試別人的查詢。我想知道爲什麼它是這樣使用的,因爲在那些列中沒有逗號分隔值。但說了這樣的話,即使使用這個,如果我不使用這個地方,按順序排列,速度也是相當不錯的。 – Pawan

+1

這就是爲什麼我刪除了我的評論,我沒有看到以前的問題的一部分。 – Barmar

+0

如果您只是在沒有ORDER BY的情況下使用GROUP BY,會發生什麼? – Barmar

回答

0

這是一種很難不知道表的結構,並具有一些樣本數據(如果你可以編輯你原來的問題,包括這些它會幫助)調試。看來你最大的問題是所有桌子上缺乏指示。

你應該能夠得到這樣的輸出這裏的一切是使用索引:因爲你使用FIND_IN_SET你可能就無法去除加入緩衝

+------+-------------+-------+--------+---------------+----------+---------+--------------------------+------+--------------------------------------------------------------+ 
| id | select_type | table | type | possible_keys | key  | key_len | ref      | rows | Extra              | 
+------+-------------+-------+--------+---------------+----------+---------+--------------------------+------+--------------------------------------------------------------+ 
| 1 | SIMPLE  | TM | ALL | NULL   | NULL  | NULL | NULL      | 2 | Using temporary; Using filesort        | 
| 1 | SIMPLE  | CM | eq_ref | PRIMARY  | PRIMARY | 4  | tutors.TM.category  | 1 | Using index             | 
| 1 | SIMPLE  | TER | index | NULL   | tutor_id | 8  | NULL      | 1 | Using where; Using index; Using join buffer (flat, BNL join) | 
| 1 | SIMPLE  | SHM | eq_ref | tutor_id  | tutor_id | 606  | tutors.TM.tutor_id,const | 1 | Using where; Using index          | 
| 1 | SIMPLE  | LM | index | NULL   | PRIMARY | 4  | NULL      | 1 | Using where; Using index; Using join buffer (flat, BNL join) | 
+------+-------------+-------+--------+---------------+----------+---------+--------------------------+------+--------------------------------------------------------------+ 

,它似乎總是需要一個全表掃描的導師,這不應該是一個問題。

這些都是indicies我對所有表:

MariaDB [tutors]> show indexes from admin_shortlist_master; 
+------------------------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table     | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+------------------------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| admin_shortlist_master |   0 | PRIMARY |   1 | id   | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
| admin_shortlist_master |   0 | tutor_id |   1 | tutor_id  | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
| admin_shortlist_master |   0 | tutor_id |   2 | user_auth_id | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
+------------------------+------------+----------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
3 rows in set (0.00 sec) 

MariaDB [tutors]> show indexes from category_master; 
+-----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+-----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| category_master |   0 | PRIMARY |   1 | category_id | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
+-----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
1 row in set (0.00 sec) 

MariaDB [tutors]> show indexes from level_master; 
+--------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+--------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| level_master |   0 | PRIMARY |   1 | level_id | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
+--------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
1 row in set (0.00 sec) 

MariaDB [tutors]> show indexes from tutor_expected_rate; 
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| tutor_expected_rate |   0 | PRIMARY |   1 | id   | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
| tutor_expected_rate |   0 | tutor_id |   1 | tutor_id | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
| tutor_expected_rate |   0 | tutor_id |   2 | level_id | A   |   0 |  NULL | NULL |  | BTREE  |   |    | 
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
3 rows in set (0.00 sec) 

如果你要提供你的整體結構和一些樣本數據,那麼很可能有辦法來索引tutor_master表爲好,但很難以確定此刻。

+0

更新一些表格數據。 – Pawan

+0

沒有關於'tutor_master'的數據,這是唯一值得懷疑的表 – sjdaws

+0

該表'tutor_master'在這裏很難再現,它包含大約150個字段。 – Pawan