【问题标题】:MySQL explain - require index suggestions / feedback [closed]MySQL解释-需要索引建议/反馈[关闭]
【发布时间】:2012-11-27 21:18:57
【问题描述】:

我有一个包含大约 500 万行的数据库,并且在查询需要很长时间(超过一分钟)时遇到问题。我希望这些信息足以让有人给我一些建议,如果我需要发布更多信息,请告诉我。

提前感谢您的任何建议

EXPLAIN SELECT count( * ) AS count
FROM vtiger_time
INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_time.timeid
INNER JOIN vtiger_crmentityrel ON ( vtiger_crmentityrel.relcrmid = vtiger_crmentity.crmid
OR vtiger_crmentityrel.crmid = vtiger_crmentity.crmid )
LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid
LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid
WHERE vtiger_crmentity.deleted =0
AND (
    vtiger_crmentityrel.crmid =211294
    OR vtiger_crmentityrel.relcrmid =211294
)


+----+-------------+---------------------+-------------+-------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+
| id | select_type | table               | type        | possible_keys                                   | key            | key_len | ref                            | rows  | Extra                                    |
+----+-------------+---------------------+-------------+-------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+
|  1 | SIMPLE      | vtiger_crmentityrel | index_merge | crmid,relcrmid                                  | crmid,relcrmid | 4,4     | NULL                           |  5881 | Using union(crmid,relcrmid); Using where | 
|  1 | SIMPLE      | vtiger_crmentity    | ref         | PRIMARY,deleted,deleted_2,crmentity_multi_index | deleted_2      | 4       | const                          | 84424 | Using where; Using index                 | 
|  1 | SIMPLE      | vtiger_users        | eq_ref      | PRIMARY                                         | PRIMARY        | 4       | crm.vtiger_crmentity.smownerid |     1 | Using index                              | 
|  1 | SIMPLE      | vtiger_groups       | eq_ref      | PRIMARY                                         | PRIMARY        | 4       | crm.vtiger_crmentity.smownerid |     1 | Using index                              | 
|  1 | SIMPLE      | vtiger_time      | eq_ref      | PRIMARY,timeid                               | PRIMARY        | 4       | crm.vtiger_crmentity.crmid     |     1 | Using index                              | 
+----+-------------+---------------------+-------------+-------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+

另外这些是我目前设置的索引

+------------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table            | Non_unique | Key_name                   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| vtiger_crmentity |          0 | PRIMARY                    |            1 | crmid       | A         |      755968 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          0 | crmid                      |            1 | crmid       | A         |      755968 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crmentity_IDX0             |            1 | smcreatorid | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crmentity_IDX1             |            1 | smownerid   | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crmentity_IDX2             |            1 | modifiedby  | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | deleted                    |            1 | deleted     | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | deleted                    |            2 | smownerid   | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | smownerid                  |            1 | smownerid   | A         |         199 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | smownerid                  |            2 | deleted     | A         |         199 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | deleted_2                  |            1 | deleted     | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | deleted_2                  |            2 | smownerid   | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | smownerid_2                |            1 | smownerid   | A         |         385 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | smownerid_2                |            2 | deleted     | A         |         758 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crm_ownerid_del_setype_idx |            1 | smownerid   | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crm_ownerid_del_setype_idx |            2 | deleted     | A         |          15 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crm_ownerid_del_setype_idx |            3 | setype      | A         |         613 |     NULL | NULL   | YES  | BTREE      |         | 
| vtiger_crmentity |          1 | crmentity_multi_index      |            1 | crmid       | A         |      755968 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crmentity_multi_index      |            2 | smownerid   | A         |      755968 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentity |          1 | crmentity_multi_index      |            3 | deleted     | A         |      755968 |     NULL | NULL   |      | BTREE      |         | 
+------------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table               | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| vtiger_crmentityrel |          1 | crmid    |            1 | crmid       | A         |      223960 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_crmentityrel |          1 | relcrmid |            1 | relcrmid    | A         |       12442 |     NULL | NULL   |      | BTREE      |         | 
+---------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

+--------------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| Table        | Non_unique | Key_name      | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+
| vtiger_users |          0 | PRIMARY       |            1 | id            | A         |          39 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_users |          1 | idx_user_name |            1 | user_name     | A         |          39 |     NULL | NULL   | YES  | BTREE      |         | 
| vtiger_users |          1 | user_password |            1 | user_password | A         |          39 |     NULL | NULL   | YES  | BTREE      |         | 
+--------------+------------+---------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+

+---------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table         | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+---------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| vtiger_groups |          0 | PRIMARY             |            1 | groupid     | A         |           5 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_groups |          1 | groupname           |            1 | groupname   | A         |           5 |     NULL | NULL   | YES  | BTREE      |         | 
| vtiger_groups |          1 | idx_groups_123group |            1 | groupname   | A         |           5 |     NULL | NULL   | YES  | BTREE      |         | 
+---------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

+----------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table          | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| vtiger_time |          0 | PRIMARY     |            1 | timeid   | A         |      591772 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_time |          0 | timeid   |            1 | timeid   | A         |      591772 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_time |          1 | relatedto   |            1 | relatedto   | A         |        1405 |     NULL | NULL   | YES  | BTREE      |         | 
| vtiger_time |          1 | date_start  |            1 | date_start  | A         |        7129 |     NULL | NULL   | YES  | BTREE      |         | 
| vtiger_time |          1 | relatedto_2 |            1 | relatedto   | A         |        3269 |     NULL | NULL   | YES  | BTREE      |         | 
+----------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

+------------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table            | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| vtiger_timecf |          0 | PRIMARY     |            1 | timeid   | A         |      591324 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_timecf |          0 | timeid   |            1 | timeid   | A         |      591324 |     NULL | NULL   |      | BTREE      |         | 
| vtiger_timecf |          1 | timeid_2 |            1 | timeid   | A         |      591324 |     NULL | NULL   |      | BTREE      |         | 
+------------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

添加以下索引后

ALTER TABLE vtiger_crmentity ADD INDEX TMP_deletion_smownerid_crmid (smownerid, deleted,crmid); 

新的解释:

+----+-------------+---------------------+-------------+--------------------------------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+
| id | select_type | table               | type        | possible_keys                                                            | key            | key_len | ref                            | rows  | Extra                                    |
+----+-------------+---------------------+-------------+--------------------------------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+
|  1 | SIMPLE      | vtiger_crmentityrel | index_merge | crmid,relcrmid                                                           | crmid,relcrmid | 4,4     | NULL                           |  5891 | Using union(crmid,relcrmid); Using where | 
|  1 | SIMPLE      | vtiger_crmentity    | ref         | PRIMARY,crmid,deleted,deleted_2,crmentity_multi_index,_deletion_crmid | deleted        | 4       | const                          | 84424 | Using where; Using index                 | 
|  1 | SIMPLE      | vtiger_users        | eq_ref      | PRIMARY                                                                  | PRIMARY        | 4       | crm.vtiger_crmentity.smownerid |     1 | Using index                              | 
|  1 | SIMPLE      | vtiger_groups       | eq_ref      | PRIMARY                                                                  | PRIMARY        | 4       | crm.vtiger_crmentity.smownerid |     1 | Using index                              | 
|  1 | SIMPLE      | vtiger_time      | eq_ref      | PRIMARY,timeid                                                        | PRIMARY        | 4       | crm.vtiger_crmentity.crmid     |     1 | Using index                              | 
+----+-------------+---------------------+-------------+--------------------------------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+

【问题讨论】:

  • 你有什么索引?我不确定 mySQL 的优化器有多聪明,但是将 WHERE 子句中的两个 id 值放入连接条件可能会有所帮助(在两个连接上)。
  • 不幸的是,我无法更改我的查询(这是现有系统的扩展)。我也必须承认,我不知道什么是“指数”?
  • 我意识到索引是什么,现在问题已更新为这些信息。
  • 所有重复键是怎么回事?
  • 似乎没有清理故障的开发系统。这些不应该以任何显着的方式影响此查询的性能。

标签: mysql sql innodb indexing vtiger


【解决方案1】:

重写查询会有所帮助,但由于您不能这样做,您可能会受益于vtiger_crmentity.crmidvtiger_crmentity.deleted 上的复合索引。但由于它没有使用您当前在vtiger_crmentity.crmid 上拥有的索引之一,它可能不会使用那个新索引。如果没有,请尝试将vtiger_crmentity.crmid 添加到vtiger_crmentity.deleted 索引。由于它已经在使用vtiger_crmentity.deleted 上的索引,这将使索引成为覆盖索引,因此查询不需要从表中读取。

正如我在评论中提到的,有很多重复的索引。不会影响对这个问题的查询,但它们会减慢插入速度并增加表大小。以下索引是多余的:

vtiger_crmentity.crmid
vtiger_crmentity.smownerid
vtiger_crmentity.smownerid_2
vtiger_crmentity.deleted_2

vtiger_groups.idx_groups_123group

vtiger_time.timeid
vtiger_time.relatedto_2

vtiger_timecf.timeid
vtiger_timcfe.timeid_2

您也不应该需要用户密码列上的索引,因为您不应该通过密码查找用户。

【讨论】:

  • 感谢 G-Nugget 的建议,我添加了以下索引,但性能没有任何变化。如果您能确认这实际上是您的意思,那就太好了,我绝不是 mysql 大师。更改表vtiger_crmentity 添加索引TMP_deletion_crmid (crmid, deleted);更改表 vtiger_crmentity 添加索引 TMP_deletion_smownerid (smownerid, deleted);
  • EXPLAIN 有变化吗?我提到的第二个索引应该在deleted,smownerid,crmid
  • 是的,我已将新的解释修改到问题的底部
【解决方案2】:

尝试添加此索引:

ALTER TABLE vtiger_crmentityrel ADD INDEX ix_crmentityrel_crmid_relcrmid (crmid,relcrmid);

这应该允许 crmentityrel 表上的两个索引完全满足说明中的第一行,并且应该为您节省数千次查找。

执行计划的所有其他部分都在使用覆盖索引,所以如果这没有帮助,我不确定在不重组数据或查询的情况下您可以做更多的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 2011-10-28
    相关资源
    最近更新 更多