Mysql 默认排序

今天遇到一个经常忽略的问题,就是关于mysql的默认排序问题:

不加任何条件,按照默认插入顺序

select id,activityId,createTime,updateTime from table  limit 100

222827_xuIq_2431191.png

后面在一个activityId=13282的条件(前提是有update/delete操作),就出现无须了

222907_UYFd_2431191.png

当我不带任何条件去select时,mysql是按照默认插入顺序进行排序的,即ID自增排序的;

但是当我加一个where 条件时,在没有update/delete时,顺序不会有任何变化的;如果有update/delete操作后,这个顺序就打乱了,以一种肉眼乱序的方式排序的!这可真是醉了,自己之前一直以为是按照默认排序进行,真是囧?了!

mysql官方的回答

当你的表示myisam时:
SELECT * FROM tbl -- this will do a "table scan". If the table has never had any DELETEs/REPLACEs/UPDATEs, the records will happen to be in the insertion order, hence what you observed.
大致意思为,一个myisam引擎表在没有任何的删除,修改操作下,执行 select 不带order by,那么会按照插入顺序进行排序。

If you had done the same statement with an InnoDB table, they would have been delivered in PRIMARY KEY order, not INSERT order. Again, this is an artifact of the underlying implementation, not something to depend on.
对于innodb引擎表来说,在相同的情况下,select 不带order by,会根据主键来排序,从小到大

转载于:https://my.oschina.net/alarm1673/blog/1814508

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值