我調試了一個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
@Bamar - 其實我正在調試別人的查詢。我想知道爲什麼它是這樣使用的,因爲在那些列中沒有逗號分隔值。但說了這樣的話,即使使用這個,如果我不使用這個地方,按順序排列,速度也是相當不錯的。 – Pawan
這就是爲什麼我刪除了我的評論,我沒有看到以前的問題的一部分。 – Barmar
如果您只是在沒有ORDER BY的情況下使用GROUP BY,會發生什麼? – Barmar