gpt4 book ai didi

mysql - SQL通过条件和排序限制查询优化

转载 作者:行者123 更新时间:2023-11-29 06:26:50 25 4
gpt4 key购买 nike

有一条 SQL:

select * from table where A=cid order by B desc, C desc limit lower_bound, 10;

每一列都有自己的索引。

lower_bound很大时,查询需要很长时间。因为表中有十亿条记录,而有些cid的记录超过一千万条。

我尝试添加联合索引(A, B, C)。我的数据库基于MySQL,但不支持联合索引。

是否有任何解决方案可以优化此查询?

最佳答案

您可以创建综合索引,如下所示:

CREATE INDEX index_name 
ON table_name(A,B,C);

来自manual :

The index can also be used even if the ORDER BY does not match the index exactly, as long as all of the unused portions of the index and all the extra ORDER BY columns are constants in the WHERE clause.

MySQL Composite Index

关于mysql - SQL通过条件和排序限制查询优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58989712/

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