我有一个可以工作的查询,但它至少需要3秒钟才能运行,所以我认为它可能会更快。它用于填充新线程的列表,并显示每个线程中有多少未读的帖子。在将查询字符串放入$db->query_read()之前,我生成了查询字符串。为了仅从有效论坛获取结果,$ids是一个字符串,其中最多包含50个用逗号分隔的值。
userthreadviews表已经存在了一周,其中大约有9,500行。我不确定我是否需要设置一个cron作业来定期清除超过一周的线程视图,或者我是否可以让它增长。
以下是当前的查询:
SELECT
`thread`.`title` AS 'r_title',
`thread`.`threadid` AS 'r_threadid',
`thread`.`forumid` AS 'r_forumid',
`thread`.`lastposter` AS 'r_lastposter',
`thread`.`lastposterid` AS 'r_lastposterid',
`forum`.`title` AS 'f_title',
`thread`.`replycount` AS 'r_replycount',
`thread`.`lastpost` AS 'r_lastpost',
`userthreadviews`.`replycount` AS 'u_replycount',
`userthreadviews`.`id` AS 'u_id',
`thread`.`postusername` AS 'r_postusername',
`thread`.`postuserid` AS 'r_postuserid'
FROM
`thread`
INNER JOIN
`forum`
ON (`thread`.`forumid` = `forum`.`forumid`)
LEFT JOIN
(`userthreadviews`)
ON (`thread`.`threadid` = `userthreadviews`.`threadid`
AND `userthreadviews`.`userid`=$userid)
WHERE
`thread`.`forumid` IN($ids)
AND `thread`.`visible`=1
AND `thread`.`lastpost`> time() - 604800
ORDER BY `thread`.`lastpost` DESC LIMIT 0, 30另一个连接post表的查询(只显示用户发布的线程)的速度实际上是原来的两倍,所以我认为这里一定有一些可以修改的东西来加快速度。有没有人能给我一些建议?
编辑:对不起,我把解释放在了备用查询的前面。下面是正确的输出:根据请求,下面是由EXPLAIN SELECT生成的输出:

发布于 2012-09-17 01:27:44
如果表未编制索引,请尝试为表forumid编制索引
https://stackoverflow.com/questions/12449026
复制相似问题