gpt4 book ai didi

mysql - 按主键和索引排序

转载 作者:行者123 更新时间:2023-11-29 02:29:23 24 4
gpt4 key购买 nike

对于许多 SQL 查询,“order by id desc”是获取最新行的必需组件。一般查询将类似于

select * from table X where some_condition order by id desc

一般来说,“order by id”会大大降低查询速度吗?

具体来说,对于这样的查询:

select * from table X where some_col = some_value order by id desc

我们真的需要加索引(some_col, id)来加速查询吗?

在所有情况下,都假定 ID 是自动递增的。


我的困惑来自以下关于排序优化的指南:

http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

最佳答案

select * from table X where some_col = some_value order by id desc

在这样的查询中,您肯定需要在 some_col 上建立索引。根据我对 (some_col) 的索引应该比 (some_col, id) 足够好。

MySQL merge index应该可以帮助您使用 id 上的索引进行排序。

此外,当您在 some_col 上搜索时,将永远不会使用 (id,some_Col) 索引,而 (some_col, id) 会。

关于mysql - 按主键和索引排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15463344/

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