gpt4 book ai didi

mysql - 大咨询使用时速度慢 "ORDER BY"

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

在论坛结构中排序一系列“主题”时遇到问题,当我执行查询时,会非常快(0.12秒)生成一个列表(在数百万条记录中),但是当我添加“ORDER BY”时ordered DESC”查询需要1分钟。

        SELECT 
forum_topics.id AS id,
forum_topics.slug AS slug,
forum_topics.user_id AS user_id,
forum_topics.forum_id AS forum_id,
forum_topics.title AS title,
forum_topics.total_post AS total_post,
forum_topics.total_view AS total_view,
forum_topics.last_post_id AS last_post_id,
forum_topics.created AS created,
forum_topics.modified AS modified,
forum_topics.ordered AS ordered,
forum_topics.status_close AS status_close,
forum_topics.status_view AS status_view,
t_users.id AS topic_user_id,
t_users.nick AS user_nick,
p_users.id AS post_user_id,
p_users.nick AS post_user_nick,
forum_posts.title AS post_title,
forum_posts.created AS post_created
FROM forum_topics
LEFT JOIN forum_posts ON forum_topics.last_post_id = forum_posts.id
LEFT JOIN users AS t_users ON forum_topics.user_id = t_users.id
LEFT JOIN users AS p_users ON forum_posts.user_id = p_users.id
WHERE forum_topics.forum_id = '9' AND forum_topics.status_highlight = '0'
LIMIT 0,100

关于如何在不损失性能的情况下订购这个 super 表有什么想法吗?注意:所有“id”(PRIMARY)和“*_id”都是索引,作为“ordered”字段,它是unix时间戳INT(15)

谢谢

最佳答案

使用 LIMIT 而不使用 ORDER BY 允许系统获取任意 100 条记录,因此它会获取找到的第一个记录。

添加 ORDER BY 表示您需要大约 100 条特定记录 - 因此它必须精确搜索这些记录。

如何解决?

尝试在FORUM_TOPICS(forum_id,status_highlight,ordered)上建立索引,该索引已经按照ORDER BY所需的顺序排列 - 因此,第一个(或最后一个) ) 索引中的记录就是您所请求的记录,因此数据库可以立即使用它们。

关于mysql - 大咨询使用时速度慢 "ORDER BY",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4700497/

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