update AA set gldescription =
(select max(RateDescription) from BB
where trim(msifilesiteid) = AA.msifilesiteid and
Trim(ratecode) = AA.GLCode and
length(trim(ratecode)) > 3)
where (glcode is not null or glcode <> '') and glDescription is null;这是一个存储过程的一部分,每天执行,其它的部分执行都很快,但到了这里就好半天才过去,AA数据量大概每天有3000条记录,BB表中大概有300000条记录, 每天增加。不知道我的SQL到底哪里有问题,执行实在是太慢了,大家帮忙看看,谢谢!!
(select max(RateDescription) from BB
where trim(msifilesiteid) = AA.msifilesiteid and
Trim(ratecode) = AA.GLCode and
length(trim(ratecode)) > 3)
where (glcode is not null or glcode <> '') and glDescription is null;这是一个存储过程的一部分,每天执行,其它的部分执行都很快,但到了这里就好半天才过去,AA数据量大概每天有3000条记录,BB表中大概有300000条记录, 每天增加。不知道我的SQL到底哪里有问题,执行实在是太慢了,大家帮忙看看,谢谢!!
然后在更新AA表,这样会快些!:)
这个SQL能快起来嘛
-----------------------------------
where (glcode is not null or glcode <> '') and glDescription is null
这样写好像没有意义吧,在Oracle里
【glcode <> ''】等价于【glcode is not null】
看这SQL好想是在SQL Server里的吧。问题二:
is null 不能使用索引,因为AA表每天增加3000左右,所以记录也不会少了,
全表扫描成本不小,所以需要使用函数索引解决 is null函数问题,
例: create index decode(glDescription,null,1,0);然后在Where中使用
....and decode(glDescription,null,1,0) = 1
这样的转化。问题三:
set字段使用查询,你这样做存在两个问题,
在其中一个问题和问题二一样,追加函数索引,
解决Trim、length等函数不使用索引问题,
然后使用Merge函数改进,Merge那本Oracle书
都有,自己一查就知道了。
select max(RateDescription) from BB
where trim(msifilesiteid) = AA.msifilesiteid and
Trim(ratecode) = AA.GLCode and
length(trim(ratecode)) > 3以上都照做,保证你能够写出好SQL。
我看你的语句效率不太高呀 比如说 glcode is not null or glcode <> ''
你无意中加了太多的索引
换成:where length(trim(ratecode))>0 and glDescription is null
update (select AA.gldescription as AA_gldescription , max(BB.RateDescription) BB_RateDescription from BB,AA
where BB.msifilesiteid = AA.msifilesiteid
and BB.ratecode = AA.GLCode )
set AA_gldescription = BB_RateDescription
update AA set gldescription =
(select max(RateDescription) from BB
where msifilesiteid = AA.msifilesiteid and
ratecode = AA.GLCode and length(ratecode) > 3)
where glcode is not null and decode(glDescription,null,1,0) = 1;
速度比以前快多了,谢谢各位的帮助!
在数据库里做了一下测试
glcode <> ''和glcode = ''什么都查不出
菜鸟求教了,,