MySQL查询速度慢

时间:2010-10-11 05:16:50

标签: mysql performance

我已经尝试了一切,以便更快地完成此查询。每个表的相同引擎,用于连接的所有字段的索引,order by或where子句。

问题似乎是表aamag尽管有可用的索引但没有使用索引。

查询:

    SELECT DISTINCT `a`.`id`, `a`.`full_name`, `a`.`rating`, `a`.`licence`, `a`.`licence_issued`, `ag`.`name` as agency_name
FROM (`property_suburb_map` psm)
JOIN `campaign_property_map` cpm ON `psm`.`property_id` = `cpm`.`property_id`
JOIN `campaign` c ON `cpm`.`campaign_id` = `c`.`id`
JOIN `campaign_agent_map` cam ON `cpm`.`campaign_id` = `cam`.`campaign_id`
JOIN `agent` a ON `cam`.`agent_id` = `a`.`id`
JOIN `agency_agent_map` aam ON `aam`.`agent_id` = `a`.`id`
JOIN `agency` ag ON `aam`.`agency_id` = `ag`.`id`
WHERE `c`.`closing_date` >= '2009-10-12'
AND `psm`.`suburb_id` = '5911'
AND `a`.`status` = 'Active'
ORDER BY `a`.`rating` DESC, `a`.`full_name`
LIMIT 12

解释(抱歉,格式搞砸了):使用此图片代替http://imgur.com/UzSpC.jpg

id  select_type     table           type    possible_keys   key     key_len     ref     rows    Extra

1   SIMPLE          a               ref     PRIMARY,status  status  1   const   790     Using where; Using temporary; Using filesort
1   SIMPLE          aam             ref     agency_id_2,
                                            agent_id,
                                            agency_id       agent_id      4     hindsight.a.id  1    
1   SIMPLE          ag              eq_ref  PRIMARY     PRIMARY     4   hindsight.aam.agency_id     1    
1   SIMPLE          cam             ref     agent_id,
                                            campaign_id     agent_id    4   hindsight.a.id  9   Distinct
1   SIMPLE          c               eq_ref  PRIMARY,closing_date    PRIMARY     4   hindsight.cam.campaign_id   1   Using where; Distinct
1   SIMPLE          cpm             ref     campaign_id     campaign_id     4   hindsight.c.id  1   Using where; Using index; Distinct
1   SIMPLE          psm             ref     property_id,suburb_id   property_id     4   hindsight.cpm.property_id   1   Using where; Distinct

以下是数据库http://pastebin.com/Rbyrj6x3

的相关结构

修改我在查询中完成了个人资料:Copying to tmp table非常慢。

mysql> show profile for query 6;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000000 |
| Opening tables       | 0.000000 |
| System lock          | 0.000000 |
| Table lock           | 0.000000 |
| init                 | 0.000000 |
| optimizing           | 0.000000 |
| statistics           | 0.000000 |
| preparing            | 0.000000 |
| Creating tmp table   | 0.000000 |
| executing            | 0.000000 |
| Copying to tmp table | 0.112000 |  <- WTF!
| Sorting result       | 0.004000 |
| Sending data         | 0.000000 |
| end                  | 0.000000 |
| removing tmp table   | 0.000000 |
| end                  | 0.000000 |
| query end            | 0.000000 |
| freeing items        | 0.000000 |
| logging slow query   | 0.000000 |
| cleaning up          | 0.000000 |
+----------------------+----------+
20 rows in set (0.00 sec)

2 个答案:

答案 0 :(得分:1)

您是否在所有表的所有字段上都有外键? 请描述你的表格

答案 1 :(得分:1)

尝试通过添加agent_id作为第二个组件,将agent.status上的索引转换为覆盖索引:

create index idx2 on agent( status, id)

这可能使查询在必须从数据表中检索记录之前消除更多代理行。