drop table total;
create table total as select empid,sum(nvl(money,0)) money from detail group by empid;
create table total as select empid,sum(nvl(money,0)) money from detail group by empid;
解决方案 »
- 刚学RMAN遇到的疑问。
- 8i的客户端能连10g的服务器吗?
- 请问为何nvl(a.cqualitylevelid,'a') = 'a' 行而 nvl(a.cqualitylevelid,'') = ''不行?
- 两个结构数组的成员之间如何拷贝数据?
- 请问怎样用触发器来做及时更新?
- 急救~~~混装了oracle 8.17和8.16两个版本,启动8.16,报错:ORA-01092 ORACLE instance!
- 表空间
- 问牛人一个sql问题!在线等,急!!!!!!!
- 为什么子查询不能用于between?
- 高分求救oracle在Linux 或者unix下能正确显示中文的 表名和 字段名吗
- 修改db_block_size 导致数据库无法启动,高手支招
- insert的问题???着急
这2个表是很复杂的表的
money=(select sum(nvl(money,0))
from detail where a.empid= empid group by
empid ) 改为update total a set
money=(select sum(nvl(money,0))
from detail where a.empid= empid )1. 去除group by empid
2. 查看一下detail对empid是否有索引。
money=(select sum(nvl(b.money,0))
from detail b where a.empid=b.empid )
where exists (select 1 from detail b where a.empid=b.empid );
set money=(select sum(nvl(detail.money,0))
from detail where total.empid=detail.empid)
where total.empid=detail.empid
update total
set money=(select sum(nvl(detail.money,0))
from detail where total.empid=detail.empid)
偶觉得
update total a
set money=(select sum(nvl(money,0)) from detail where a.empid= empid )
where exists (select 1 from detail b where a.empid=b.empid )
效率应该是比较高的--
当然前提是两个表的index!
不过偶一般采用PL/SQL来写:
/***
一个DETAIL 表,
item ,empid, money
1 001 100
2 001 150
... ... ...一个 TOTAL 表,
empid , money
**/
declare
cursor the_cur is
select empid, sum(nvl(money,0)) money
from detail
group by empid
order by empid;
begin
for my_cur in the_cur loop
update total
set money=my_cur.money
where empid=my_cur.empid;
end loop;
end;个人测试表明:这样写似乎比update效率高许多~~~
原来我的 detail 表没有设置关键字,
关键字设置后, update 只需要 2s了。。
汗 !!因为我发现这种类似的语句在其他地方都很快的,就这里慢,才发现是detail 表没有关键字
只是措词不一样而已!