使用 GROUP BY 和 count 优化 MySQL 查询结果中的顺序
我有以下疑问:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WHERE
语句中的内容应该被索引。尝试在
SELECT
之前使用EXPLAIN
语句来查看使用什么以及如何检索您请求的结果。如果
code
列不是唯一值,我建议将其放在其他表中,因为它是唯一的。然后通过FOREIGN KEY
使用JOIN
构建查询。Things that are in
WHERE
statement, should be indexed.Try to use
EXPLAIN
statement before yourSELECT
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 usingJOIN
though theFOREIGN KEY
.