如何为多个联接表找到正确的索引

时间:2019-04-11 12:24:35

标签: mysql

我有以下查询。主表分别是company_reportsproject_con_messages,分别有770,000和1,040,000条记录。 运行此查询时,大约需要20秒,而删除最后一个表company_par_user_settings时,则需要不到0.5秒。 company_par_user_settings(简称:pus)大约有200,000条记录,旨在显示每个company_partner的用户设置。在company_par_user_settings表上,我们在partner_iduser_id字段上具有复合唯一索引键。我还删除了索引,并用partner_iduser_id上的简单索引替换,但是最后,它对运行时间没有太大影响。

SELECT * 

FROM company_reports rep 
LEFT JOIN system_users usr ON rep.user_id=usr.id 
LEFT JOIN company_rep_subjects sbj ON rep.subject_id=sbj.id 
INNER JOIN company_partners par ON rep.partner_id=par.id 
LEFT JOIN project_con_messages mes ON rep.message_id=mes.id 
LEFT JOIN company_par_user_settings pus ON par.id=pus.partner_id AND 1=pus.user_id

WHERE 1=1 
ORDER BY rep.id DESC 
LIMIT 0,50

在下面,我在上面的查询中添加了解释:

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| id   | select_type | table | partitions | type   | possible_keys | key     | key_len | ref                           | rows   | filtered | Extra                                              | 
| 1    | SIMPLE      | rep   | NULL       | ALL    | partner_id    | NULL    | NULL    | NULL                          | 772236 | 100.00   | Using temporary; Using filesort                    |
| 1    | SIMPLE      | par   | NULL       | eq_ref | PRIMARY       | PRIMARY | 3       | portal_ebrahim.rep.partner_id | 1      | 100.00   | NULL                                               |
| 1    | SIMPLE      | usr   | NULL       | eq_ref | PRIMARY       | PRIMARY | 2       | portal_ebrahim.rep.user_id    | 1      | 100.00   | Using where                                        |
| 1    | SIMPLE      | sbj   | NULL       | eq_ref | PRIMARY       | PRIMARY | 2       | portal_ebrahim.rep.subject_id | 1      | 100.00   | NULL                                               |
| 1    | SIMPLE      | mes   | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | portal_ebrahim.rep.message_id | 1      | 100.00   | NULL                                               |
| 1    | SIMPLE      | pus   | NULL       | ALL    | NULL          | NULL    | NULL    | NULL                          | 191643 | 100.00   | Using where; Using join buffer (Block Nested Loop) |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

如果有人为我提供正确的索引或任何其他使查询运行更快的解决方案的帮助,我将不胜感激。

编辑:

这是company_par_user_settings的演出表

CREATE TABLE `company_par_user_settings` (
 `id` mediumint(9) NOT NULL AUTO_INCREMENT,
 `partner_id` mediumint(8) unsigned NOT NULL,
 `user_id` smallint(5) unsigned NOT NULL,
 `access` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '0-Not specified',
 `access_category` tinyint(1) unsigned NOT NULL DEFAULT '0',
 `notify` tinyint(1) unsigned NOT NULL DEFAULT '0',
 `stars` tinyint(1) unsigned NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 UNIQUE KEY `partner_id` (`partner_id`,`user_id`),
 KEY `stars` (`stars`)
) ENGINE=MyISAM AUTO_INCREMENT=198729 DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci

0 个答案:

没有答案