今天遇到一个经常忽略的问题,就是关于mysql的默认排序问题:
不加任何条件,按照默认插入顺序
select id,activityId,createTime,updateTime from table limit 100
后面在一个activityId=13282的条件(前提是有update/delete操作),就出现无须了
当我不带任何条件去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,会根据主键来排序,从小到大