mysql连接查询不使用索引

时间:2016-09-05 07:44:06

标签: mysql indexing query-optimization

我坚持使用超过13秒的查询来返回输出 我确实创建了索引,但执行计划似乎并没有使用它们。以下是有关

的一些信息

查询

SELECT  userinfo.username,userinfo.firstname,userinfo.lastname,userinfo.email,
radcheck.attribute,radcheck.value,radusergroup.groupname, userinfo.id,
userinfo.workphone,userinfo.homephone,
userinfo.mobilephone,userinfo.address, userinfo.zone,
userinfo.account_state,userinfo.device_owner,userinfo.link_type,userinfo.account_type 
FROM userinfo  
INNER JOIN radcheck ON userinfo.username = radcheck.username  
INNER JOIN radusergroup ON userinfo.username = radusergroup.username 
WHERE  radcheck.attribute='Expiration'  and 
STR_TO_DATE(radcheck.value, '%d %M %Y') < CURDATE() 
and radusergroup.groupname='Customer30M';

解释输出

+----+-------------+--------------+------+--------------------+-----------+---------+--------------------------------+------+-----------------------------------+
| id | select_type | table        | type | possible_keys      | key       | key_len | ref                            | rows | Extra                             |
+----+-------------+--------------+------+--------------------+-----------+---------+--------------------------------+------+-----------------------------------+
|  1 | SIMPLE      | radusergroup | ref  | username,groupname | groupname | 66      | const                          |    5 | Using where with pushed condition |
|  1 | SIMPLE      | radcheck     | ref  | username,attribute | attribute | 34      | const                          |    9 | Using where                       |
|  1 | SIMPLE      | userinfo     | ref  | username           | username  | 131     | ctradius.radusergroup.username |   10 | Using where                       |
+----+-------------+--------------+------+--------------------+-----------+---------+--------------------------------+------+-----------------------------------+
3 rows in set (0.00 sec)

表格信息

表:radusergroup

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| username  | varchar(64) | NO   | MUL |         |                |
| groupname | varchar(64) | NO   | MUL |         |                |
| priority  | int(11)     | NO   |     | 1       |                |
+-----------+-------------+------+-----+---------+----------------+

radusergroup表索引

+--------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table        | Non_unique | Key_name          | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| radusergroup |          0 | PRIMARY           |            1 | id          | A         |       11292 |     NULL | NULL   |      | BTREE      |         |               |
| radusergroup |          1 | username          |            1 | username    | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radusergroup |          1 | groupname         |            1 | groupname   | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radusergroup |          1 | indexradusergroup |            1 | username    | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radusergroup |          1 | indexradusergroup |            2 | groupname   | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

表:userinfo

---------------------+------------------+------+-----+---------------------+----------------+
| Field               | Type             | Null | Key | Default             | Extra          |
+---------------------+------------------+------+-----+---------------------+----------------+
| id                  | int(11) unsigned | NO   | PRI | NULL                | auto_increment |
| username            | varchar(128)     | YES  | MUL | NULL                |                |
| firstname           | varchar(200)     | YES  |     | NULL                |                |
| lastname            | varchar(200)     | YES  |     | NULL                |                |
| email               | varchar(200)     | YES  |     | NULL                |                |
| workphone           | varchar(200)     | YES  |     | NULL                |                |
| homephone           | varchar(200)     | YES  |     | NULL                |                |
| mobilephone         | varchar(200)     | YES  |     | NULL                |                |
| address             | varchar(200)     | YES  |     | NULL                |                |
| zone                | varchar(20)      | YES  |     | NULL                |                |
| account_state       | varchar(10)      | YES  |     | active              |                |
| change_commit       | tinyint(1)       | YES  |     | NULL                |                |
| link_type           | varchar(8)       | YES  |     | NULL                |                |
| account_type        | varchar(255)     | YES  |     | NULL                |                |
| device_owner        | varchar(255)     | YES  |     | NULL                |                |
| account_name        | varchar(255)     | YES  |     | NULL                |                |
| account_email       | varchar(255)     | YES  |     | NULL                |                |
| account_mobile      | varchar(255)     | YES  |     | NULL                |                |
| groupname           | varchar(255)     | YES  |     | NULL                |                |
+---------------------+------------------+------+-----+---------------------+----------------+

userinfo上的索引

----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| userinfo |          0 | PRIMARY  |            1 | id          | A         |       11282 |     NULL | NULL   |      | BTREE      |         |               |
| userinfo |          1 | username |            1 | username    | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

表:Radcheck

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| username  | varchar(64)      | NO   | MUL |         |                |
| attribute | varchar(32)      | NO   | MUL |         |                |
| op        | char(2)          | NO   |     | ==      |                |
| value     | varchar(253)     | NO   | MUL |         |                |
+-----------+------------------+------+-----+---------+----------------+
radcheck上的

索引

+----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| radcheck |          0 | PRIMARY       |            1 | id          | A         |       33425 |     NULL | NULL   |      | BTREE      |         |               |
| radcheck |          1 | username      |            1 | username    | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radcheck |          1 | value         |            1 | value       | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| radcheck |          1 | attribute     |            1 | attribute   | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

1 个答案:

答案 0 :(得分:1)

该函数使索引的使用无效。尽管你的基数显示了11k行,但是表格扫描随之而来,需要13秒。

虽然您在Radcheckuserinfo之间有联接,但是where子句中的STR_TO_DATE函数会终止索引的使用。