[求助]存储过程中foreach语句优化问题,求大牛们帮忙

yanpingsha 2008-07-04 11:35:27
foreach SELECT grpIndex,county,grpid
INTO vgrpIndex,vtempcountyID,bgrpid
FROM table_group

SELECT count(*)
INTO vusers
FROM table_grpuser
WHERE grpIndex=vgrpIndex;


SELECT count(*)
INTO vuserfeeuser
FROM table_grpuser
WHERE grpIndex=vgrpIndex and type='1';
.....
.....

insert into table_tmp
values(.......)
--将上面相关值插入tmp表

end foreach


foreach select locationid,countryid
into vlocationid,vcountryid
from location

SELECT sum(grpuser),sum(grprulednum)
INTO vusernum,vrulednum
FROM table_temp
WHERE location=vlocationID and county=vcountyID;
........
....

end foreach
语句如上,grpuser表的数据量在120万左右,group表在30万左右,请问第一个foreach循环中有好的优化方式么?
第二个循环里面的sum是不是也是影响效率的原因?

另外foreach执行的机理是什么?是先执行select取出一个表的所有数据再执行下一个操作,还是select一条数据,然后执行下一个数据呢?
...全文
286 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujian1986 2009-07-24
  • 打赏
  • 举报
回复

select table_group.county,table_group.grpid,count(*),
sum(case when table_grpuser.type='1' then 1 else 0 end)
from table_group,table_grpuser
where table_group.grpIndex=table_grpuser.grpIndex
--我不大明白你想做什么,这是第一个foreach,你试试,下面你再处理
--你最好先看看sql基础查询中的联表查询
vampirewp 2009-06-16
  • 打赏
  • 举报
回复
检查表table_grpuser 是否创建了grpIndex列的索引

执行第二个Foreach这前,如果table_temp表记录很多,对该表创建location,county的组合索引
linyafeng 2008-10-24
  • 打赏
  • 举报
回复
怎么不使用游标啊!
cloudflashes 2008-08-03
  • 打赏
  • 举报
回复
楼上说的用临时表是个办法。
rabbitjump 2008-07-11
  • 打赏
  • 举报
回复
SELECT grpIndex, count(1) as fl_Num
from table_grpuser
group by grpIndex, type
into temp tmp_grpuser;

SELECT A.grpIndex, B.fl_Num
FROM table_group A, tmp_grpuser B
WHERE A.grpIndex = B.grpIndex
into temp tmp_Filter;

后面再进行关联统计,可以不用到foreach模式,提高处理效率




linyafeng 2008-07-08
  • 打赏
  • 举报
回复
不知道你的foreach的作用是不是取多条数据
如果是的话,你可以使用游标的。
linyafeng 2008-07-08
  • 打赏
  • 举报
回复
SELECT count(*)的执行效率比SELECT count (1)的执行效率要慢很多。
所以优化,要从很多方面考虑的!

1,194

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 Informix
社区管理员
  • Informix社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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