关于oracle的更新效率问题,select时很快,但update就很慢

zhiquan911 2010-09-20 09:20:01
以下两条语句,一条查询的,一条是把想把查询的东西update到对应行中,但update的语句效率很慢,
两条语句条件的写法上大致一样,究竟oracle的update是怎样的原理,导致效率这么慢,求助一下各位

--更新的语句(速度很慢,基本不动)
update t_join_situation a
set (a.special_clinic_compen_money,a.special_clinic_compen_count)=
(
select a.special_clinic_compen_money+compensation_sum as a1,a.special_clinic_compen_count+compensation_count as a2 from
(
select sum(this_year_compensation_cost) as compensation_sum,count(1) as compensation_count,people_number from t_compensate_proof where
type='3' and state='0' and year='2010' and city_number='P00000'
group by people_number
) b
where a.city_number='P00000' and a.year='2010' and a.people_number=b.people_number
)
where exists (select 1 from t_compensate_proof b where
a.people_number=b.people_number and a.city_number='P00000' and a.city_number=b.city_number
)



--查询速度很快
select a.people_number from t_join_situation a,
(
select sum(this_year_compensation_cost) as compensation_sum,count(1) as compensation_count,people_number from t_compensate_proof where
type='2' and state='0' and year='2010' and city_number='U00000'
group by people_number
) b
where a.city_number='U00000' and a.year='2010' and a.people_number=b.people_number


...全文
2312 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
minitoy 2010-09-20
  • 打赏
  • 举报
回复
那看看执行计划呢
zhiquan911 2010-09-20
  • 打赏
  • 举报
回复
t_join_situation 表 按city_number字段分区,并且people_number和year是索引字段,
但我更新的字段不是索引字段
minitoy 2010-09-20
  • 打赏
  • 举报
回复
恩,应该是索引的原因.索引是典型的以空间换时间.select时使用索引可以大大减少检索时间.但是update的时候得维护索引,所以导致update变慢.
Diza1986 2010-09-20
  • 打赏
  • 举报
回复
对表建立索引或分区可以大幅提高查询的效率,但如果在该列数据上作更新操作则效率会非常差

另外对于这种条件
where a.city_number='P00000' and a.year='2010' and a.people_number=b.people_number


建议这样写
where a.people_number=b.people_number and a.year='2010' and a.city_number='P00000'


oracle条件语句是从后开始执行的,这样效率会好一些
心中的彩虹 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhiquan911 的回复:]
以下两条语句,一条查询的,一条是把想把查询的东西update到对应行中,但update的语句效率很慢,
两条语句条件的写法上大致一样,究竟oracle的update是怎样的原理,导致效率这么慢,求助一下各位

SQL code

--更新的语句(速度很慢,基本不动)
update t_join_situation a
set (a.special_clinic_compen_mo……
[/Quote]


-- 试下这样



update (select a.special_clinic_compen_money t1,a.special_clinic_compen_count t2,compensation_sum,compensation_count
from t_join_situation a,
(select sum(this_year_compensation_cost) as compensation_sum,count(1) as compensation_count,people_number,city_number
from t_compensate_proof where type='3' and state='0' and year='2010' and city_number='P00000'
group by city_number,people_number) b
where a.people_number=b.people_number and a.city_number=b.city_number)
set t1=compensation_sum,t2=compensation_count



csuxp2008 2010-09-20
  • 打赏
  • 举报
回复
可能更索引有关系,看看t_compensate_proof这张表上的索引

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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