gpt4 book ai didi

mysql - 为什么在 MySQL 中 'select *' 和 'select id' 之间的 id 不同?

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:19 26 4
gpt4 key购买 nike

我有一个名为account_info 的表。当我运行 select id from account_info limit 0, 10 时,结果如下。

ids

但是当我运行select * from acccount_info limit 0, 10时,结果如下(忽略其他列)。

ids

为什么id不一样?当我将 order by id 添加到这两个 sql 时,结果是相同的。结果的顺序似乎很重要。但我认为当没有 order by id 时,select id from account_info limit 0, 10select * from account_info 共享相同的默认值排序策略,结果应该是一样的。

谁能解释一下为什么?我的 MySQL 版本是 5.7.14。

最佳答案

没有“默认排序策略”,因为排序总是需要一些(通常非常昂贵的)计算,所以如果您没有指定任何顺序,MySQL 可以自由地以它认为最快/最便宜的方式返回行。

当您只查询 id 时,您看到值被排序的原因是,在这种情况下,MySQL 只需要读取存储为(排序的)B 树的主键。您可以通过查看查询执行计划来确认这一点 - 它应该显示 Using index 描述为:

The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.

当您查询 * 时,MySQL 必须自己遍历可以按任何顺序存储的表数据。

请注意,以上解释不保证结果排序。重复一遍:如果没有明确的 ORDER BY,所有关于数据返回方式的赌注都将落空。

关于mysql - 为什么在 MySQL 中 'select *' 和 'select id' 之间的 id 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45730956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com