MYSQL版本:5.6.24
先创建一个测试表:
CREATE TABLE `tb` ( `id` bigint(20) NOT NULL, `account` varchar(10) DEFAULT NULL, KEY `idx` (`account`,`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
再插入数据:
INSERT INTO tb(id,account) VALUES(1,'1001'); INSERT INTO tb(id,account) VALUES(2,'1000'); INSERT INTO tb(id,account) VALUES(3,'1002'); INSERT INTO tb(id,account) VALUES(4,'1003'); INSERT INTO tb(id,account) VALUES(5,'1000'); INSERT INTO tb(id,account) VALUES(6,'1000'); INSERT INTO tb(id,account) VALUES(7,'1005'); INSERT INTO tb(id,account) VALUES(8,'1000'); INSERT INTO tb(id,account) VALUES(9,'1006');
查看SQL执行解释:
EXPLAIN SELECT * FROM `tb` WHERE account = '1000' AND id > 1 AND id < 10 ORDER BY id ASC
得到如下结果
id select_type table type possible_keys key key_len ref rows Extra ------ ----------- ------ ------ ------------- ------ ------- ------ ------ -------------------------- 1 SIMPLE tb range idx idx 41 (NULL) 3 Using where; Using index
但是这个SQL查询的返回记录数却是4行,而explain出来的rows为3,为什么呢?
SELECT * FROM `tb` WHERE account = '1000' AND id > 1 AND id < 10 ORDER BY id ASC
查询account的值为1000,id大于1且小于10的行本来就是四行啊,查询出来肯定是四行记录啊!不信你自己数一下。