SQL中的变量时无法命中索引?

时间:2010-06-21 09:46:24

标签: mysql

以下SQL无法命中idx_user_userid索引,我不知道如何解决它?

SET @q = 'abcd';
EXPLAIN SELECT user_id, mobile_num FROM user WHERE user_id = @q;

用户说明:

CREATE user(
  row_id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  user_id CHAR(20) NOT NULL,
  mobile_num CHAR(15) NOT NULL DEFAULT ''
) ENGINE = InnoDB
CREATE UNIQUE INDEX idx_user_userid ON user(user_id);

MySQL版本是5.1.36

1 个答案:

答案 0 :(得分:1)

这正如我在MySQL 5.0.51a-24中所期望的那样。 “user_id”列中的任何行实际上是否包含@q变量中的值?这是我系统的输出:

CREATE TABLE `user` (
    `row_id` int(11) NOT NULL auto_increment,
    `user_id` char(20) NOT NULL,
    `mobile_num` char(15) NOT NULL default '',
    PRIMARY KEY  (`row_id`),
    UNIQUE KEY `idx_user_userid` (`user_id`)
) ENGINE=InnoDB;

INSERT INTO `user` VALUES
    (1, 'user1', '1234567890'),
    (2, 'user2', '1234567890');

SELECT * FROM `user`;

+--------+---------+------------+
| row_id | user_id | mobile_num |
+--------+---------+------------+
|      1 | user1   | 1234567890 |
|      2 | user2   | 1234567890 |
+--------+---------+------------+

SET @q = 'user1';

EXPLAIN SELECT user_id, mobile_num FROM user WHERE user_id = @q;

+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys   | key             | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+-------+
|  1 | SIMPLE      | user  | const | idx_user_userid | idx_user_userid | 20      | const |    1 |       |
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+-------+

SET @q = 'abcd';

EXPLAIN SELECT user_id, mobile_num FROM user WHERE user_id = @q;
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+

注意当使用值'abcd'时,EXPLAIN SELECT语句如何返回'Impossible WHERE'消息。发生这种情况是因为MySQL无法匹配WHERE语句,因为表中不存在提供的值。但是,当提供有效值时,将选择正确的索引。

如果使用文字值而不是传递用户定义的变量,则会返回相同的结果:

EXPLAIN SELECT user_id, mobile_num FROM user WHERE user_id = 'abcd';
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+

EXPLAIN SELECT user_id, mobile_num FROM user WHERE user_id = 'user1';
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys   | key             | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+-------+
|  1 | SIMPLE      | user  | const | idx_user_userid | idx_user_userid | 20      | const |    1 |       |
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+-------+