对两百万数据group by 的速度问题

liliaipp123 2010-06-08 11:32:41
大家好啊,小女子要对两百万的数据进行group by 查询速度超过了两分钟,而且最终结果没出来,超时。我要group by 的字段是varchar(255),而且需求就是这样,不得不按这个字段分组。有时间的话,大家就帮忙看看吧,给我些建议,这个问题已经困扰我好久了。

数据举例如下:

表名:alyd_out_201006

列名 date refer link linkname hits
20100601 http://7999.com/c/qiche.html http://www.ben-ben.net/main.htm 奔奔 1
20100601 http://www.1616.net/ http://www.gzmama.com/index.php 广州妈妈网 1
20100601 http://www.1616.net/?yaokan http://www.wtscy.cn/ 铁观音叶 1
20100601 http://www3.1616.net/1/gupiao.htm http://www.sse.com.cn/ 上海证券交易所 1
20100601 http://www.wo116.com/1/xiaoshuo.htm http://www.zzzyq.com/ 最言情小说 2
等等一共是两百万条数据

我用到的sql是select sum(hits) as total,link,linkname from alyd_out_201006 group by link order by total limit 0,30


...全文
840 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2010-06-13
  • 打赏
  • 举报
回复
1 SIMPLE alyd_out_201005 index NULL link 767 NULL 30 Using temporary; Using filesort

是不是link总数不多?
除了以上办法,如果方便的话,还可以改下表结构
原表 date refer link linkname hits
改成
A表 link_id date hits refer (key link_id(link_id))
B表 link_id(int primary key auto_increment) link(unique key) linkname
loveflea 2010-06-11
  • 打赏
  • 举报
回复
加上索引吧 (link,linkname,hits)

如果不需要太精确:

建议周期性(如15分钟一次)的将下面的结果,存到一个汇总表中,然后读此汇总表!
select sum(hits) as total,link,linkname from alyd_out_201006 group by link order by total;
hitexam 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liliaipp123 的回复:]
多谢多谢,crc32是mysql的一个函数吗?我都没用过,现在去试试,多谢多谢
[/Quote]
一些加密函数也可以用
liliaipp123 2010-06-08
  • 打赏
  • 举报
回复
好的。。。去试试看
flairsky 2010-06-08
  • 打赏
  • 举报
回复
然后还想快就在这crc32(link)这列上建立一个索引。
liliaipp123 2010-06-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 flairsky 的回复:]
存放crc32(link),这是将字符串变成一串int值,一对一性相当高

多谢多谢,crc32是mysql的一个函数吗?我都没用过,现在去试试,多谢多谢
flairsky 2010-06-08
  • 打赏
  • 举报
回复
那么长的字符串,肯定出问题

教你一招

多一列,存放crc32(link),这是将字符串变成一串int值,一对一性相当高

group by 这一列就是了

我一般不出手的,今天比较郁闷,帮帮人心情好点
liliaipp123 2010-06-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wwwwb 的回复:]
explain sql语句,贴结果
[/Quote]

explain 的结果如上面所示
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE alyd_out_201005 index NULL link 767 NULL 30 Using temporary; Using filesort


liliaipp123 2010-06-08
  • 打赏
  • 举报
回复
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE alyd_out_201005 index NULL link 767 NULL 30 Using temporary; Using filesort


explain 的结果如上面所示
wwwwb 2010-06-08
  • 打赏
  • 举报
回复
explain sql语句,贴结果
ACMAIN_CHM 2010-06-08
  • 打赏
  • 举报
回复
加上索引吧 (link,linkname)

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧