建了一张表,主键为自增id,name可以为空,插入6条数据create table t(id int unsigned not null primary key auto_increment,name varchar(20));insert into t select 0,'1';
insert into t select 0,null;
insert into t select 0,'2';
insert into t select 0,'3';
insert into t select 0,null;
insert into t select 0,'4';第一行查询全表扫描,这没有问题加了order by id limit 3之后居然只扫描了3行
我的问题是,扫描的时候limit 3,而这里只有2条null应该也是从6开始扫描到1才停止呀,mysql究竟是怎么对这条SQL进行优化的?求大神解答
insert into t select 0,null;
insert into t select 0,'2';
insert into t select 0,'3';
insert into t select 0,null;
insert into t select 0,'4';第一行查询全表扫描,这没有问题加了order by id limit 3之后居然只扫描了3行
我的问题是,扫描的时候limit 3,而这里只有2条null应该也是从6开始扫描到1才停止呀,mysql究竟是怎么对这条SQL进行优化的?求大神解答
解决方案 »
- MYSQL编译时出现的问题
- 直接把frm、MYI、MYD这几个文件放到装mysql的文件夹data/数据库名 目录下,怎么数据文件加载不上来呀?
- 关于mysql中的limit的问题
- 怎样知道一个表已经被lock?
- 请各位大侠指点指点---老总
- mysql是否支持事务处理,如支持从第几版支持,怎样做?
- 这样将数据插入到已经建好的基本表里?
- SQL修改字段类型........在线等候
- 为什么直接在别的机器上拷过来的MYSQL(约60M)在我WIN2000上不能用,非要安装吗?请高手路过指点!
- org.springframework.jdbc.BadSqlGrammarException:
- mysql如何进行批量更新?
- 我是没做出第二步来 而且这是写报表 要求效率 挑战性题 求大神帮忙
执行顺序:where,group,order,limit
1。你告诉他,name=null
他找出2条,
2。order 一下,
他也是2条,
3。limit
他是3知,虽然有空行。
如果是where id = 1这种的很好理解,这个order by 索引 limit n怎么优化的不解
请注意,EXPLAIN 结果中的 rows 字段的值也是MySQL的连接优化程序大致猜测的估计要看MYSQL的源码才能知道原因
楼主的疑问到底是什么? 是返回结果不对? 还是对EXPLAIN中这个 ROW=3 有疑问?
这里SQL语句查询出的结果是2条null,分析执行计划预估查询6条
而下面这条在后面加上order by id desc之后查询结果是2条null,预估却为3条,是怎么仅仅分析3条数据就得到2条null的?我觉得order by id desc limit 3的话应该会从后面倒着查询到匹配行后才停止检索
show index from t;
create table t(id int unsigned not null primary key auto_increment,name varchar(20));