2012-11-27 65 views
3

我有一個數據庫包含大約500萬行,並且在查詢時需要很長時間(超過一分鐘)。我希望這些信息足以讓某些人給我一些建議,如果我需要發佈更多信息,請告訴我。MySQL解釋 - 要求索引建議/反饋

預先感謝您的任何建議

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        | 
+----+-------------+---------------------+-------------+--------------------------------------------------------------------------+----------------+---------+--------------------------------+-------+------------------------------------------+ 
+0

你有什麼指數?我不確定mySQL的優化器是多麼的聰明,但是將兩個id值放入連接條件中的'WHERE'子句可能有助於一些(在兩個連接上)。 –

+0

不幸的是我不能改變我的查詢(這是現有系統的擴展)。我還必須承認,我不知道「指數」是什麼? – rich

+0

我意識到什麼是索引,問題現在用這些信息更新。 – rich

回答

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 

您也不應該需要用戶密碼列上的索引,因爲您不應該通過密碼尋找用戶。

+0

感謝您的建議G-Nugget,我添加了以下索引,並且沒有發現性能發生變化。如果你能確認這實際上是你的意思,那就太好了,我絕不是一個mysql大師。 ALTER TABLE'vtiger_crmentity' ADD INDEX'TMP_deletion_crmid'('crmid','deleted'); ALTER TABLE'vtiger_crmentity' ADD INDEX'TMP_deletion_smownerid'('smownerid','deleted'); – rich

+0

做了'EXPLAIN'改變了嗎?我提到的第二個索引應該是'刪除,smownerid,crmid'。 –

+0

是的,我已經向這個問題的底部提供了新的解釋 – rich

1

嘗試增加這個指數:

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

這應該允許在解釋的第一行完全被這兩個指標上crmentityrel表滿意,應該爲你節省幾千的查找。

執行計劃的所有其他部分都使用覆蓋索引,所以如果這沒有幫助,我不確定沒有重構數據或查詢就可以做更多事情。