3

我有一个包含大约 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                              | 
+----+-------------+---------------------+-------------+--------------------------------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+
4

2 回答 2

1

重写查询会有所帮助,但由于您不能这样做,您可能会从 and 上的复合索引中vtiger_crmentity.crmid受益vtiger_crmentity.deleted。但由于它没有使用您当前拥有的索引之一vtiger_crmentity.crmid,它可能不会使用新的索引。如果没有,请尝试添加vtiger_crmentity.crmidvtiger_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

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

于 2012-11-28T00:35:41.653 回答
1

尝试添加此索引:

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

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

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

于 2012-11-28T02:09:57.027 回答