2017-07-22 70 views
0

我有后续查询,我提交给MySQL服务器的麻烦。它需要25s ...为(COUNT(*)< 20k) - 表 - 只有功能有600k行。但是,索引是在它应该在的地方创建的(也就是说,对于ON子句中的相关列)。我试图删除GROUP BY,这改善了一些情况。但是这些查询仍然给出了一个慢速回应的一般规则。我做了那篇文章,因为我找不到找到到stackoverflow的各种案件的解决方案。任何建议?非常慢的MySQL查询(左连接,GROUP BY等?)

SELECT 
    doctor.id as doctor_id, 
    doctor.uuid as doctor_uuid, 
    doctor.firstname as doctor_firstname, 
    doctor.lastname as doctor_lastname, 
    doctor.cloudRdvMask as doctor_cloudRdvMask, 

    GROUP_CONCAT(recommendation.id SEPARATOR ' ') as recommendation_ids, 
    GROUP_CONCAT(recommendation.uuid SEPARATOR ' ') as recommendation_uuids, 
    GROUP_CONCAT(recommendation.disponibility SEPARATOR ' ') as recommendation_disponibilities, 
    GROUP_CONCAT(recommendation.user_id SEPARATOR ' ') as recommendation_user_ids, 
    GROUP_CONCAT(recommendation.user_uuid SEPARATOR ' ') as recommendation_user_uuids, 

    location.id as location_id, 
    location.uuid as location_uuid, 
    location.lat as location_lat, 
    location.lng as location_lng, 

    profession.id as profession_id, 
    profession.uuid as profession_uuid, 
    profession.name as profession_name 
FROM featured as doctor 
LEFT JOIN location as location 
    ON doctor.location_id = location.id 
LEFT JOIN profession as profession 
    ON doctor.profession_id = profession.id 
LEFT JOIN 
    (
     SELECT 
      featured.id as id, 
      featured.uuid as uuid, 
      featured.doctor_id as doctor_id, 
      featured.disponibility as disponibility, 

      user.id as user_id, 
      user.uuid as user_uuid 
     FROM featured as featured 
      LEFT JOIN user as user 
       ON featured.user_id = user.id 
      WHERE discr = 'recommendation' 
     ) as recommendation 
     ON recommendation.doctor_id = doctor.id 
WHERE 
    doctor.discr = 'doctor' 
    AND 
    doctor.state = 'Publié' 
GROUP BY doctor.uuid 

这里谈到的EXPLAIN结果:

id | select_type | table  | partitions | type | possible_keys    | key     | key_len | ref       | rows | filtered | Extra  | 

1 | SIMPLE  | doctor  | NULL  | ref | discr,state    | discr    | 767  | const       | 194653 | 50.00 | Using where | 
1 | SIMPLE  | location | NULL  | eq_ref | PRIMARY     | PRIMARY    | 4  | doctoome.doctor.location_id |  1 | 100.00 | NULL  | 
1 | SIMPLE  | profession | NULL  | eq_ref | PRIMARY     | PRIMARY    | 4  | doctoome.doctor.profession_id |  1 | 100.00 | NULL  | 
1 | SIMPLE  | featured | NULL  | ref | IDX_3C1359D487F4FB17,discr | IDX_3C1359D487F4FB17 | 5  | doctoome.doctor.id   | 196 | 100.00 | Using where | 
1 | SIMPLE  | user  | NULL  | eq_ref | PRIMARY     | PRIMARY    | 4  | doctoome.featured.user_id  |  1 | 100.00 | Using index | 

编辑此链接帮助我,现在去与787-8。 https://www.percona.com/blog/2016/10/12/mysql-5-7-performance-tuning-immediately-after-installation/。但我仍然觉得它很慢,我只是让它在任何人都知道什么可以改进的情况下。由于

+0

'索引应该在哪里创建'。究竟在哪里?你的硬件配置是什么? EXPLAIN查询结果在哪里? –

+0

以下字段建立索引: doctor.location_id,location.id doctor.profession_id,profession.id featured.user_id,USER.ID recommendation.doctor_id,doctor.id doctor.discr doctor.uuid 什么信息可能在硬件部分有用? 谢谢! –

+0

复合指标在某些情况下也非常重要。你用过吗?附:此外,你的数据库的转储会很好,所以我们可以测试查询更改和其他东西与真正的数据库。 –

回答

0

我想取出子查询可能会帮助,有一些指标一起:

SELECT . . . -- you need to fix the `GROUP_CONCAT()` column references 
FROM featured doctor LEFT JOIN 
    location location 
    ON doctor.location_id = location.id LEFT JOIN 
    profession profession 
    ON doctor.profession_id = profession.id LEFT JOIN 
    featured featured 
    ON featured.doctor_id = doctor.doctor_id LEFT JOIN 
    user user 
    ON featured.user_id = user.id 
WHERE doctor.discr = 'doctor' AND 
     doctor.state = 'Publié' AND 
     featured.discr = 'recommendation' 
GROUP BY doctor.uuid; 

然后你想在featured(discr, state, doctor_id, location_id, profession_id)featured(doctor_id, discr, user_id)的索引。

+0

就像一个测试,我完全删除了GROUP BY&GROUP_CONCAT的东西,并且也删除了子查询。我的查询在验证了所有索引创建之后,花了7s。我将编辑我的文章给EXPLAIN结果 –

+0

我这样做,非常感谢 –

+0

我不明白为什么它仍然无法正常工作..我会在我的本地机器上使用相同的数据库大小进行测试 –