mysql索引问题

SELECT `invite_invited`.`id`, `invite_invited`.`user_id`, `invite_invited`.`account`, `invite_invited`.`invited_datetime`, `invite_invited`.`register_user_id`, `invite_invited`.`category` FROM `invite_invited` WHERE (`invite_invited`.`category` = 1  AND `invite_invited`.`account` = '[email protected]'  AND `invite_invited`.`user_id` = 938950 );
# Thread_id: 947886790  Schema: zcwdb  Last_errno: 0  Killed: 0
# Query_time: 1.248157  Lock_time: 0.000065  Rows_sent: 0  Rows_examined: 391491  Rows_affected: 0  Rows_read: 391491

 这个条总数据量也就391491,

Rows_read: 391491

explain :

+----+-------------+----------------+------+-------------------------+-------------------------+---------+-------+--------+-------------+
| id | select_type | table          | type | possible_keys           | key                     | key_len | ref   | rows   | Extra       |
+----+-------------+----------------+------+-------------------------+-------------------------+---------+-------+--------+-------------+
|  1 | SIMPLE      | invite_invited | ref  | invite_invited_fbfc09f1 | invite_invited_fbfc09f1 | 4       | const | 198793 | Using where |
+----+-------------+----------------+------+-------------------------+-------------------------+---------+-------+--------+-------------+
1 row in set (0.00 sec)
 

说明没有走索引.show table:

| invite_invited | CREATE TABLE `invite_invited` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `account` varchar(120) NOT NULL,
  `invited_datetime` datetime NOT NULL,
  `register_user_id` int(11) DEFAULT NULL,
  `category` smallint(6) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `invite_invited_fbfc09f1` (`user_id`),
  KEY `invite_invited_66211c56` (`register_user_id`),
  CONSTRAINT `register_user_id_refs_id_f76a7793` FOREIGN KEY (`register_user_id`) REFERENCES `auth_user` (`id`),
  CONSTRAINT `user_id_refs_id_f76a7793` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=401478 DEFAULT CHARSET=utf8 |

account字段上没有建索引,在account增加索引问题解决。

+----+-------------+----------------+------+-------------------------------------+-------------+---------+-------+------+-------------+
| id | select_type | table          | type | possible_keys                       | key         | key_len | ref   | rows | Extra       |
+----+-------------+----------------+------+-------------------------------------+-------------+---------+-------+------+-------------+
|  1 | SIMPLE      | invite_invited | ref  | invite_invited_fbfc09f1,idx_account | idx_account | 362     | const |    1 | Using where |
+----+-------------+----------------+------+-------------------------------------+-------------+---------+-------+------+-------------+
 

猜你喜欢

转载自san-yun.iteye.com/blog/1716548