我有一张桌子,上面有类似状态的帖子。表列是:
ID //which is unique incremental
Post_id //liked post
user_id //user who gave like or dislike
type //"1","0" or "2" which stands for Liked, neutral or disliked.
这是示例数据
+--------+---------+---------+------+
| id | post_id | user_id | type |
+--------+---------+---------+------+
| 938300 | 347298 | 661 | 0 |
| 938299 | 346185 | 0 | 1 |
| 938298 | 347286 | 2645 | 0 |
| 938297 | 346924 | 374 | 1 |
| 938296 | 347261 | 1523 | 1 |
| 938295 | 347313 | 3233 | 1 |
| 938294 | 346323 | 1375 | 1 |
| 938293 | 347022 | 1779 | 1 |
| 938292 | 347278 | 2645 | 1 |
| 938291 | 347300 | 109 | 1 |
+--------+---------+---------+------+
10 rows in set (0.01 sec)
但是这个查询运行完美,但是你们可以看到这个表中有数百个数据。我需要的是:
SELECT post_id,
count(post_id)
FROM 'table'
WHERE type = '1'
GROUP BY post_id
ORDER BY count(post_id)
LIMIT 300;
此查询选择最喜欢的 300 个帖子,php 代码从中随机选择一个。但是,此查询具有全表扫描,它会持续 5 秒以上。我怎样才能加快速度,或者我必须更改表格方案吗?