【发布时间】:2016-08-29 01:12:26
【问题描述】:
我知道这已被讨论过很多次,但我的研究并没有帮助我解决我的问题。
我有一个包含大约 3k 条记录的表 (innodb)。我需要用一些过滤器随机选择 1 行,我这样做是这样的:
select id, title, topic_id
from posts
where id not in
(select post_id from records where user_id='$my_id' and checked='1')
and topic_id='$topic_id' and status='1'
order by RAND() limit 1
这给了我想要的结果。问题是即使有 3k 条记录,这也需要太多时间。当记录增加时,它会变慢。
我必须为此找到解决方案。有什么建议吗?
更新:两个表都使用 id 列进行索引。
【问题讨论】:
-
看看this
-
我在写到这里之前就试过了。但这对我没有帮助,因为我的 WHERE 过滤器
-
3K 行在哪个表中?另一个表有多少行?
-
posts 表中的 3k 行。记录表有 8k 行。
标签: mysql performance random mysqli