使用where,group by和order by时,MYSQL Query需要花费太多时间

时间:2017-06-10 21:23:32

标签: php mysql

我调试了一个花了太多时间的MYSQL查询。

查询是这样的:

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

如果我要排除部分

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

Query执行如下:

  

显示0到29行(总共27,649行,查询占用0.2339秒)

对于完整查询,它是这样的:

  

显示0到9行(总计10行,查询耗时115.4066秒)

我为查询中使用的所有字段编制了索引。

MYSQL解释如下:

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

更新

我在某处读到使用1 = 1并没有对性能产生影响,但即使添加1 = 1,查询也需要从0.2339秒开始大约70秒。

MYSQL my.cnf设置如下:

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 

更新

当使用group_by而没有order_by时,时间几乎相同:

  

显示0到29行(总共27,530行,查询占用114.9642秒)

更新样品日期: 表 - 导师预期费率

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  

表 - admin_shortlist_master

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

table-category master

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

表级主人

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

更新:

N

1 个答案:

答案 0 :(得分:0)

如果不知道表格结构并拥有一些示例数据(如果您可以编辑原始问题以包含这些问题并帮助),则很难调试。看来你最大的问题是所有牌桌都没有标记。

你应该能够获得这样的输出,其中一切都在使用索引:

+------+-------------+-------+--------+---------------+----------+---------+--------------------------+------+--------------------------------------------------------------+
| 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) |
+------+-------------+-------+--------+---------------+----------+---------+--------------------------+------+--------------------------------------------------------------+

因为您正在使用FIND_IN_SET,您可能无法删除联接缓冲区,而且似乎始终需要对导师进行全表扫描,这不应该是一个问题

这些是我对所有表格的指标:

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表编制索引,但目前很难确定。