大量数据 group by 求 优化

militala2 2010-12-25 08:46:17
一个表普通表, 就单个表 例如有1000W条数据

表名domain 表字段:
domainid ,
domain,
url,
date,
userid,



select count(*) from domain group by domain order by domain asc

domain 加索引了,这个group by 要时间过长,怎能优化???

有什么好的方法??

...全文
561 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
leeo816 2012-04-25
  • 打赏
  • 举报
回复
楼主,这个问题太明显了,group by 跟 order by 是同个字段都没用到索引,就是你的select有问题,

select count(domain) from domain group by domain order by domain asc

这样select、group by、order by都是同个字段,数据库应该不会笨到连索引都不用吧,再不行就强制索引,再不行就只能 explain + 语句 进行分析了
cuidenghong123 2010-12-28
  • 打赏
  • 举报
回复
count(*) 改为 count(字段名)
ihefe 2010-12-28
  • 打赏
  • 举报
回复
select count(*) from domain group by domain order by domain asc LIMIT 0,10000;

几万的查询不可?
xiexiaokang 2010-12-28
  • 打赏
  • 举报
回复
select count(*) from domain group by domain 这样直接统计出domain的个数
skyaspnet 2010-12-27
  • 打赏
  • 举报
回复
domain加的索引是非聚集索引吗?
skyaspnet 2010-12-27
  • 打赏
  • 举报
回复
首先有一点比较奇怪,为什么表名domain会和其中一个字段名相同?
quzhongxiong 2010-12-26
  • 打赏
  • 举报
回复
你先explain分析 一下有没有用到索引

然后你试着把order by条件去掉,因为既然你已经索引了那个字段,那就默认是已经排好序了 的。
fsocket 2010-12-26
  • 打赏
  • 举报
回复
没有WHERE条件?你可以EXPLAIN看下。
bupabupa166 2010-12-25
  • 打赏
  • 举报
回复
bang 顶,我也不知道

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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