mysql 排序结果不一致的原因是啥? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
a33291
V2EX    问与答

mysql 排序结果不一致的原因是啥?

  •  
  •   a33291 2024-05-25 14:28:28 +08:00 1300 次点击
    这是一个创建于 571 天前的主题,其中的信息可能已经有所发展或是发生改变。
    相同版本数据库:mysql 8.0.28
    部署方式: a:win server 2012 r2 b:rocky 9 docker
    有表结构如下,表名: test,字符集 utf8mb4 排序规则 utf8mb4_general_ci
    Id(int) Name(varchar) Order(int)

    数据特性:
    插入默认 10 条随机数据,但是 Order 均为 0

    在数据库和表的字符集和排序规则均一致的情况,下列语句在 a/b 下输出顺序不同
    select Id,Name,Order from test order by Order;

    现象如下:
    1. a 和 b 都执行这个语句,输出结果顺序不一致;
    2. 分别在 a 或 b 上多次执行,结果相同,也就是说在实例内部结果是稳定的;

    疑问如下
    1. 首先,由于排序字段是 int,所以应和字符集以及排序规则无关吧?
    2. 除了排序规则还有什么配置或者方式可以影响对一个 int 字段的排序结果吗?
    3 条回复    2024-05-26 11:23:54 +08:00
    Tacks
        1
    Tacks  
       2024-05-25 15:15:31 +08:00
    改成这个呢 `SELECT Id, Name, Order FROM test ORDER BY Order, Id;`

    所有行的 Order 值都是 0 时,排序是不确定的。另外是你换个名字吧 order 这关键词被你当字段用。
    a33291
        2
    a33291  
    OP
       2024-05-25 19:56:43 +08:00
    @Tacks 是的,解决方案我们也是这样处理.只是好奇为啥不一致,我意思他排序算法跨平台不稳定而且不可配置好像

    现在我们没有特别在意字段和关键字重复的情况,因为大部分时候都是 orm 自动生成的标准 sql,不会造成错误或者冲突
    Tacks
        3
    Tacks  
       2024-05-26 11:23:54 +08:00
    ```
    If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.

    One factor that affects the execution plan is LIMIT, so an ORDER BY query with and without LIMIT may return rows in different orders. Consider this query, which is sorted by the category column but nondeterministic with respect to the id and rating columns:
    ```

    来自 https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html


    简单来说 :如果在 ORDER BY 列中有多个行具有相同的值,则服务器可以自由以任何顺序返回这些行,并且根据整体执行计划的不同,返回值可能会有所不同。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     4519 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 09:53 PVG 17:53 LAX 01:53 JFK 04:53
    Do have faith in what you're oing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86