【发布时间】:2015-06-16 15:38:31
【问题描述】:
我有以下 MySQL 查询:
SELECT
p.post_id,
p.date_created,
p.description,
p.last_edited,
p.link,
p.link_description,
p.link_image_url,
p.link_title,
p.total_comments,
p.total_votes,
p.type_id,
p.user_id
FROM posts p JOIN posts_to_tribes ptt ON p.post_id=ptt.post_id
WHERE ptt.tribe_id IN (1, 2, 3, 4, 5)
GROUP BY p.post_id
ORDER BY p.last_edited DESC, p.total_votes DESC LIMIT 25
在非并发环境中,此查询运行约 172 毫秒,但在并发环境中运行 1-2 秒(在性能测试期间)。
解释输出:
posts_to_tribes 表上的索引:
有什么办法可以提高性能吗?
【问题讨论】:
-
ptt.tribe_id 上可能有索引
-
基本经验法则:在“决策”上下文(join、where、order by)中使用的任何字段都应该有一个索引。
-
感谢您的回答,我在 posts_to_tribes.tribe_id 上添加了一个索引,但没有任何改变.. 查询现在运行约 188 毫秒.. 可能是我做错了什么..
-
为什么172ms的执行时间可以接受,但1-2秒的执行时间是不可接受的?
-
我在这个功能上有 REST 端点。现在它在性能测试期间在并发环境中运行约 6 秒,而其他端点运行约 2-3 秒
标签: mysql sql performance