[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
文章中所有操作均是在 MySQL 5.7 版本下进行的
<>引入问题
MySQL 普通查询它是按照什么进行排序的?我们稍微讨论下这个问题。我们先引入一个测试表:
drop table if exists tbl_test; create table tbl_test( id int auto_increment
primary key, num varchar(50), info varchar(50) ); -- 插入测试数据 insert into tbl_test
values (null, '20', 'val'), (null, '11', 'val'), (null, '33', 'val'), (null,
'15', 'val'), (null, '2', 'val'), (null, '7', 'val'), (null, '5', 'val'); --
执行查询 select * from tbl_test; +----+------+------+ | id | num | info | +
----+------+------+ | 1 | 20 | val | | 2 | 11 | val | | 3 | 33 | val | | 4 | 15
| val | | 5 | 2 | val | | 6 | 7 | val | | 7 | 5 | val | +----+------+------+
select id, num from tbl_test; +----+------+ | id | num | +----+------+ | 1 | 20
| | 2 | 11 | | 3 | 33 | | 4 | 15 | | 5 | 2 | | 6 | 7 | | 7 | 5 | +----+------+
以上测试表很简单,不管执行怎么样的查询,好像并没有区别。
<>继续讨论
这个时候将这个表稍微做一下修改。
-- 给表的num字段增加了一个很普通的key alter table tbl_test add key(num); -- 执行查询 select *
from tbl_test; +----+------+------+ | id | num | info | +----+------+------+ | 1
| 20 | val | | 2 | 11 | val | | 3 | 33 | val | | 4 | 15 | val | | 5 | 2 | val |
| 6 | 7 | val | | 7 | 5 | val | +----+------+------+ select id, num from
tbl_test; +----+------+ | id | num | +----+------+ | 2 | 11 | | 4 | 15 | | 5 | 2
| | 1 | 20 | | 3 | 33 | | 7 | 5 | | 6 | 7 | +----+------+
以上测试,会发现第二个查询出的结果集明显的出现了顺序的变化,显而易见,是那个新增加的 key 起了某些作用。
<>结语
篇幅很短,首先我们不能断然的说一个查询语句默认是主键进行的排序的,应该是在有前提的情况下(这个表只有主键并没有其它设置等可能是按照主键进行排序的)。其次更不能说按照数据的插入顺序了,这观点很明显没有证据并且很
low。很多因素影响到排序,比如上面的 key,索引方式,物理位置,MySQL 的存储引擎就有可能造成影响,所以一定要有顺序,还是使用 order
by,order by 的列上建上索引更好。