Mysql 当SQL中存在变量时无法命中索引?
下面的SQL无法命中索引idx\u user\u userid,我不知道如何解决它Mysql 当SQL中存在变量时无法命中索引?,mysql,Mysql,下面的SQL无法命中索引idx\u user\u 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 N
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这与我在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语句如何返回“不可能在哪里”消息。这是因为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 | |
+----+-------------+-------+-------+-----------------+-----------------+---------+-------+------+-------+
但是在mysql 5.1.36中,下面的SQL显示了类型=ALL。解释从用户中选择用户id,mobild_num,其中用户id=@qYes,这很奇怪。您可以发布SHOW CREATE TABLE user和EXPLAIN SELECT的完整输出吗?另外,SELECT查询返回多少行?我发现了一个问题:1。在bash中的mysql客户端中,以下SQL无法命中索引。设置@q='abcd';选择user_id,mobile_num FROM user WHERE user_id=@q;但是下面的内容将被点击,从用户中选择用户id,mobile_num,其中用户id='abcd';2.如果在一个过程中,所有这些SQL都将同时命中索引。