| 发表于:2007-12-14 20:20:11 楼主 |
系统中有张表二千多万的数据,要进行一些类型分类汇总 本人不才,写出语句运行无法成功......望高手指点指点.急. 表结构如下 主键 部门 状态 类型 进入时间 发出时间 id dept state type indate outdate 本人汇总的语句如下: select dept,type ,sum(t.autousecount) autousecount,sum(t.handusecount) handusecount,sum(t.cancellusecount) cancellusecount,sum(t.blankusecount) blankusecount,sum(t.lossusecount) lossusecount,sum(t.indatecount) indatecount,sum(t.outdatecount) outdatecount,sum(t.fdatecount) fdatecount,sum(t.bdatecount) bdatecount from (select dept,type ,count(state) autousecount, 0 handusecount,0 cancellusecount,0 blankusecount,0 lossusecount,0 indatecount,0 outdatecount,0 fdatecount,0 bdatecount from tk_dzinfo where state= '2' group by dept,type union all select dept,type ,0 autousecount, count(state) handusecount,0 cancellusecount,0 blankusecount,0 lossusecount,0 indatecount,0 outdatecount,0 fdatecount,0 bdatecount from tk_dzinfo where state= '3' group by dept,type union all select dept,type , 0 autousecount, 0 handusecount,count(state) cancellusecount,0 blankusecount,0 lossusecount,0 indatecount,0 outdatecount,0 fdatecount,0 bdatecount from tk_dzinfo where state= '4' group by dept,type union all select dept ,type ,0 autousecount, 0 handusecount,0 cancellusecount,count(state) blankusecount,0 lossusecount,0 indatecount,0 outdatecount,0 fdatecount,0 bdatecount from tk_dzinfo where state= '5' group by dept,type union all select dept ,type , 0 autousecount, 0 handusecount,0 cancellusecount,0 blankusecount,count(state) lossusecount,0 indatecount,0 outdatecount,0 fdatecount,0 bdatecount from tk_dzinfo where state= '6' group by dept ,type union all select dept ,type , 0 autousecount, 0 handusecount,0 cancellusecount,0 blankusecount, 0 lossusecount, count(outdate) indatecount,0 outdatecount,0 fdatecount,0 bdatecount from tk_dzinfo state= '1' and outdate is null group by dept ,type union all select dept ,type , 0 autousecount, 0 handusecount,0 cancellusecount,0 blankusecount, 0 lossusecount, 0 indatecount,0 outdatecount,0 fdatecount ,count(*) bdatecount from tk_dzinfo where state= '1' and indate is null group by dept, type union all select dept ,type , 0 autousecount, 0 handusecount,0 cancellusecount,0 blankusecount, 0 lossusecount, 0 indatecount,count(outdate) outdatecount,0 fdatecount,0 bdatecount from tk_dzinfo where state= '1' and boutdate is not null group by dept ,type union all select dept ,type , 0 autousecount, 0 handusecount,0 cancellusecount,0 blankusecount, 0 lossusecount, 0 indatecount,0 outdatecount ,count(*) fdatecount,0 bdatecount from tk_dzinfo where state= '1' and indate is not null group by dept ,type 该汇总条件区分主要是状态(state)与进入时间(indate)与发出时间(outdate) 几个条件的不同汇总不同的结果,而且sate='1'时有多个,所以一时只能写出如此语句,但由于该表数据量过大(两千万条以上)所以一执行就等于死机, 我想有什么方法可替换union all方法的占用表资源少的其他方法,或有其他更好的方法汇总,希望有高手能帮下,急求解决方案!谢谢大家帮忙看下 |
|
|
|
|