加速包含100K +记录的MYSQL左连接查询

时间:2013-08-30 04:28:53

标签: mysql sql database thinking-sphinx

通过思考sphinx生成查询。我正在寻找加快此查询速度的方法。返回结果几乎需要永远。我们有什么方法可以优化它。

SELECT 
    SQL_NO_CACHE `meetings`.`id` * CAST(8 AS SIGNED) + 3 AS `id`, 
    GROUP_CONCAT(DISTINCT IFNULL(CONCAT_WS(' ', `members`.`first_name`, `members`.`last_name`), '0') SEPARATOR ' ') AS `mentor_name`, 
    `meetings`.`id` AS `sphinx_internal_id`, 
    0 AS `sphinx_deleted`, 
    984144037 AS `class_crc`, 
    `meetings`.`program_id` AS `program_id`, 
    UNIX_TIMESTAMP(`meetings`.`start_time`) AS `start_time`, 
    `meetings`.`active` AS `active`, 
    `meetings`.`calendar_time_available` AS `calendar_time_available`, 
    `meetings`.`group_id` AS `group_id` 
FROM `meetings` 
LEFT OUTER JOIN `member_meetings` 
    ON `member_meetings`.`meeting_id` = `meetings`.`id` 
LEFT OUTER JOIN `members` 
    ON `members`.`id` = `member_meetings`.`member_id` 
WHERE 
    `meetings`.`delta` = 0 
GROUP BY 
    `meetings`.`id` 
ORDER BY NULL

计数

select count(*) from meetings : 194685
select count(*) from member_meetings : 522309
select count(*) from members : 112016

修改

我已经找到了问题,我已经正确配置了所有适当的索引

为member_meetings.meeting_id和member_meetings.member_id添加了重复索引键。使用add_index方法时,在rails ActiveRecord :: Migration中存在错误。我现在已经删除了重复的索引键,查询响应时间只有4s

1 个答案:

答案 0 :(得分:2)

确保您拥有索引

`member_meetings`.`meeting_id`
`meetings`.`id` 
`members`.`id`
`member_meetings`.`member_id` 
`meetings`.`delta`