使用 GROUP BY 和 count 优化 MySQL 查询结果中的顺序

发布于 2024-12-18 07:20:40 字数 506 浏览 2 评论 0原文

我有以下疑问:

SELECT vBrowser,iconBrowser, count(iconBrowser) as 'N'
FROM user_ip_tmp  WHERE code='9m9g9tsv2y'
GROUP BY iconBrowser
ORDER BY N DESC
LIMIT 40

并且这工作正常。但神志不清的原因查询却花了很长时间。

Showing rows 0 - 17 ( 18 total, Query took 4.4189 sec)

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

I have the following query:

SELECT vBrowser,iconBrowser, count(iconBrowser) as 'N'
FROM user_ip_tmp  WHERE code='9m9g9tsv2y'
GROUP BY iconBrowser
ORDER BY N DESC
LIMIT 40

And this works properly. But the delirious cause query took a long time.

Showing rows 0 - 17 ( 18 total, Query took 4.4189 sec)

enter image description here

enter image description here

enter image description here

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

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

发布评论

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

评论(1

野侃 2024-12-25 07:20:40

WHERE 语句中的内容应该被索引。

尝试在 SELECT 之前使用 EXPLAIN 语句来查看使用什么以及如何检索您请求的结果。

如果 code 列不是唯一值,我建议将其放在其他表中,因为它是唯一的。然后通过 FOREIGN KEY 使用 JOIN 构建查询。

Things that are in WHERE statement, should be indexed.

Try to use EXPLAIN statement before your SELECT to see what and how is used to retrief your requested results.

And if the column code is not an unique value, i would recommend to put it in some other table, where it is unique. Then build the query using JOIN though the FOREIGN KEY.

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