mysql 按计数排序性能

发布于 2024-12-25 17:48:55 字数 590 浏览 0 评论 0原文

我发现以下内容有点令人困惑......如果我执行以下查询,当按索引值“关键字”排序时,需要 0.0008 秒,但当按“计数”排序时,需要 3 秒以上。

以下大约需要 0.0008 秒:

SELECT keyword, COUNT(DISTINCT pmid) as count 
    FROM keywords 
    WHERE (collection_id = 13262022107433) 
    GROUP BY keyword 
    order by keyword desc limit 1;

这需要 3 秒以上:

SELECT keyword, COUNT(DISTINCT pmid) as count 
    FROM keywords 
    WHERE (collection_id = 13262022107433) 
    GROUP BY keyword 
    order by count desc limit 1;

按计数排序时,有没有办法加快结果集的排序速度?真的需要那么长的时间吗?还有其他选择吗?引擎是InnoDB。

非常感谢您的投入!

I'm finding the following a little perplexing... if I perform the below queries, when sorting by the indexed value 'keyword' it takes 0.0008 seconds, but when sorting by 'count' it takes over 3 seconds.

The following takes approx 0.0008 seconds:

SELECT keyword, COUNT(DISTINCT pmid) as count 
    FROM keywords 
    WHERE (collection_id = 13262022107433) 
    GROUP BY keyword 
    order by keyword desc limit 1;

This takes over 3 seconds:

SELECT keyword, COUNT(DISTINCT pmid) as count 
    FROM keywords 
    WHERE (collection_id = 13262022107433) 
    GROUP BY keyword 
    order by count desc limit 1;

Is there a way of speeding up a sort on a result set when sorting by count? Should it really take that much longer? Are there any alternatives? The engine is InnoDB.

Many thanks for your input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2025-01-01 17:48:55

您可能需要添加额外的索引来协助计数阶段。

ALTER TABLE keywords ADD INDEX ckp_index (collection_id,keyword,pmid);

如果您已经有一个仅包含 collection_id 和关键字的复合索引,查询优化器仍将包含对表中 pmid 字段的查找。

通过添加这个新索引,这将删除任何表扫描并仅执行索引扫描。

这将加快查询的 count(distinct pmid) 部分。

尝试一下!

You may want to add an additional index to assist the in the counting phase.

ALTER TABLE keywords ADD INDEX ckp_index (collection_id,keyword,pmid);

If you already have a compound index with collection_id and keyword only, the Query Optimizer will still include a lookup for the pmid field from the table.

By adding this new index, this will remove any table scans and perform index scans only.

This will speed the count(distinct pmid) portion of the query.

Give it a Try !!!

初相遇 2025-01-01 17:48:55

并非意外,也无法避免。当这个查询按关键字排序时,MySQL可以查看最后出现的关键字,挑选出具有该关键字的行,并对它们进行计数。但是,当您按 count 排序时,它必须计算每个关键字的行数才能找出最高的一个。还有很多工作要做!

Not unexpected, not avoidable. When this query is ordered by keyword, MySQL can just look at what keyword comes last, pick out the rows with that keyword, and count them. When you order by count, though, it has to count the rows for every keyword to figure out which one is highest. That's a lot more work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文