[英]Optimize MySQL query order in the results using GROUP BY and count
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)
Things that are in WHERE
statement, should be indexed. WHERE
语句中的内容应建立索引。
Try to use EXPLAIN
statement before your SELECT
to see what and how is used to retrief your requested results. 尝试在
SELECT
之前使用EXPLAIN
语句,以查看用于检索所需结果的内容和方式。
And if the column code
is not an unique value, i would recommend to put it in some other table, where it is unique. 而且,如果列
code
不是唯一值,我建议将其放在其他表中,使其具有唯一性。 Then build the query using JOIN
though the FOREIGN KEY
. 然后通过
FOREIGN KEY
使用JOIN
构建查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.