【问题标题】:Optimize query with group by, inner query and count使用 group by、inner query 和 count 优化查询
【发布时间】:2020-05-22 15:54:14
【问题描述】:

我正在尝试获取相关主题标签。

主题标签和帖子表之间存在多对多关系。

例如,对于主题标签“爱”,我尝试获取他们发布的所有主题标签都有 Love 主题标签。

这是我的查询(主题标签 67 代表“爱”)

SELECT hashtag_id, count(hashtag_id) as count
from post_hashtag 
where 
   # where posts has hashtag '67' 
   post_id in ( SELECT post_id FROM post_hashtag WHERE hashtag_id = 67 ) 
   # remove hashtag 67 from result
   and hashtag_id != 67
# group them and sort by count, so the must repeated hashtag is the best relative hashtag
GROUP by hashtag_id
ORDER by count desc
limit 4

我尝试优化我的查询,但我无法对其进行更多优化(目前基于帖子数量需要 2 - 12 秒)

还有什么可以优化的吗?

解释查询

+----+-------------+-----------------+------------+--------+---------------+---------------------------------------------------+---------+-------------------------------------------+---------+----------+-------------+
| id | select_type | table           | partitions | type   | possible_keys | key                                               | key_len | ref                                       | rows    | filtered | Extra       |
+----+-------------+-----------------+------------+--------+---------------+---------------------------------------------------+---------+-------------------------------------------+---------+----------+-------------+
|  1 | SIMPLE      | post_hashtag    | NULL       | index  | NULL          | fk_np_account_post_has_np_hashtag_np_hashtag1_idx | 4       | NULL                                      | 4623584 |   100.00 | Using index |
|  1 | SIMPLE      | hashtag         | NULL       | eq_ref | PRIMARY       | PRIMARY                                           | 4       | graphicj_novin.np_post_hashtag.hashtag_id |       1 |   100.00 | NULL        |
+----+-------------+-----------------+------------+--------+---------------+---------------------------------------------------+---------+-------------------------------------------+---------+----------+-------------+

post_hashtag 有这些字段

post_id,hashtag_id

两个字段都是外键

【问题讨论】:

  • 我们需要知道你的表是如何定义的,包括你的索引。请edit您的问题。并且,检查您的 EXPLAIN 输出:您问题中的查询未提及 hashtag 表。

标签: mysql optimization query-performance


【解决方案1】:

MySQL 经常优化 WHERE IN (SELECT ...) 很差。请改用JOIN。

SELECT p1.hashtag_id, count(*) as count
from post_hashtag AS p1
JOIN post_hashtag AS p2 ON p1.post_id = p2.post_id
WHERE p1.hashtag_id != 67
AND p2.hashtag_id = 67
GROUP by p1.hashtag_id
ORDER by count desc
limit 4

【讨论】:

    猜你喜欢
    • 2015-08-08
    • 2015-11-10
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多