具有类似子选择的SQL查询的性能问题

时间:2018-07-02 10:01:51

标签: mysql performance

我有一个可以运行的复杂SQL查询,但是它的性能不理想(执行需要近3秒钟)。我想我已经优化了所有我能想到的东西,但是也许还有一些我仍然无法捕捉到的东西。

这里是:

SELECT DISTINCT doctors.doc_id, doctors.doc_title, doctors.doc_last, 
    doctors.doc_first, doctors.doc_email, doctors.doc_birthday,
    doctors.doc_mobile, doctors.doc_occasional, doctors.doc_fiscal_code, 
    doctors.doc_register_number, doctors.doc_register_province, 
    doctors.doc_agreement, doctors.doc_notes, doctors.doc_timestamp,
    doctors.doc_deleted, specializations.spe_id, specializations.spe_name,
    activities.act_id, activities.act_name,
    users.use_id, users.use_last, users.use_first, users.use_active,
    (SELECT COUNT(*)
    FROM congress
    INNER JOIN participants ON participants.par_congress = congress.cng_id
    WHERE par_doctor = doc_id
    AND congress.cng_from >= '2018-01-01'
    AND congress.cng_from <= '2018-07-02')
    AS cng_count,
    (SELECT COUNT(*)
    FROM visits
    INNER JOIN reports ON reports.rep_id = visits.vis_report
    INNER JOIN locations ON locations.loc_id = visits.vis_location
    WHERE visits.vis_doctor = doctors.doc_id 
    AND locations.loc_structure LIKE '%' 
    AND reports.rep_dated >= '2018-01-01' 
    AND reports.rep_dated <= '2018-07-02')
    AS vis_count_all,
    (SELECT COUNT(*)
    FROM visits
    INNER JOIN reports ON reports.rep_id = visits.vis_report
    INNER JOIN locations ON locations.loc_id = visits.vis_location 
    WHERE visits.vis_doctor = doctors.doc_id 
    AND reports.rep_user = users.use_id 
    AND locations.loc_structure LIKE '%' 
    AND reports.rep_dated >= '2018-01-01' 
    AND reports.rep_dated <= '2018-07-02')
    AS vis_count_user,
    (SELECT COUNT(*)
    FROM locations
    WHERE locations.loc_doctor = doctors.doc_id )
    AS loc_count
    FROM doctors
    LEFT JOIN locations ON locations.loc_doctor = doctors.doc_id
    INNER JOIN specializations ON specializations.spe_id = doctors.doc_specialization
    INNER JOIN activities ON activities.act_id = doctors.doc_activity
    INNER JOIN users ON users.use_id = doctors.doc_user 
    WHERE doctors.doc_last IS NOT NULL 
    AND doctors.doc_id LIKE '%' 
    AND (locations.loc_province IS NULL OR locations.loc_province LIKE '%') 
    AND (locations.loc_structure IS NULL OR locations.loc_structure LIKE '%') 
    AND DATE(doctors.doc_timestamp) <= '2018-07-02' 
    AND doctors.doc_occasional LIKE '%' 
    AND doctors.doc_deleted LIKE '0' 
    AND doctors.doc_agreement LIKE '%' 
    AND doctors.doc_active 
    AND users.use_id LIKE '%' 
    GROUP BY doctors.doc_id 
    HAVING vis_count_user <> - 1 
    ORDER BY doctors.doc_last, doctors.doc_first, doctors.doc_id

真正的瓶颈在vis_count_allvis_count_user子选择上(它们仅在附加的AND reports.rep_user = users.use_id语句上有所不同):删除它们可以加快查询速度

由于它们仅在语句中有所不同,所以我不知道是否可以重用其中一个以更简单的方式获取另一个值

无论如何,我创建了所有我能想到的键,这是EXPLAIN结果

enter image description here

请问,有什么改进建议吗? 谢谢

1 个答案:

答案 0 :(得分:0)

此“覆盖”索引可能会有所帮助:

visits:  (vis_doctor, vis_report, vis_location)

最好删除无用的子句,例如包含LIKE '%'的子句。与此相关,

          AND  (locations.loc_province IS NULL
              OR  locations.loc_province LIKE '%')

可能优化得很差。计划A:将其删除。方案B:避免使用NULL

同时拥有JOINGROUP BY会产生一个红色标记。添加DISTINCT只是简单的“错误”。请决定您是否需要GROUP BYDISTINCT。我也不认为你也不需要。当然不要同时使用两者。

什么?

 ( SELECT COUNT(*) ... ) AS vis_count_user
 HAVING vis_count_user <> -1

HAVING只会降低查询速度。

AND  DATE(doctors.doc_timestamp) <= '2018-07-02'

在函数中隐藏列可防止使用索引。顺便说一句,指数是什么?请提供SHOW CREATE TABLE。这具有相同的语义:

AND  doctors.doc_timestamp < '2018-07-02' + INTERVAL 1 DAY

您的意思是说真的有6个月,加上1(或2?)天吗?

AND reports.rep_dated >= '2018-01-01' 
AND reports.rep_dated <= '2018-07-02'

考虑

AND reports.rep_dated >= '2018-01-01' 
AND reports.rep_dated  < '2018-01-01' + INTERVAL 6 MONTH