SQL执行效率怎么这么低

liqijn 2008-05-10 03:23:28
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到底哪里有问题,执行实在是太慢了,大家帮忙看看,谢谢!!
...全文
217 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
优雅刺客 2008-05-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ehuman 的回复:]
SQL从表面看有两几个致命问题:

问题一:
-----------------------------------
【glcode <> ''】等价于【glcode is not null】
[/Quote]
不是很明白,,
在数据库里做了一下测试
glcode <> ''和glcode = ''什么都查不出
菜鸟求教了,,
liqijn 2008-05-16
  • 打赏
  • 举报
回复
我逐一的把各种条件去掉然后执行SQL, 影响最大的是当我把trim函数去掉后,速度提高了N倍,下面是我优化后的SQL:
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;
速度比以前快多了,谢谢各位的帮助!
liqijn 2008-05-15
  • 打赏
  • 举报
回复
首先谢谢各位的答复,ehuman看的很准,这个SQL原本是在SQL Server里的,我现在需要将它转到Oralce,我先试试各位的提议,稍后给大家答复, 很感谢大家的帮助!!
oracle_dba_11 2008-05-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ehuman 的回复:]
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左右,所以记录也不会少了,
全表扫描成本不小,所以需要使用函数索引…
[/Quote]
好像可以的,楼主试试
蘑菇小子 2008-05-12
  • 打赏
  • 举报
回复
or glcode <> ''不需要
zll711 2008-05-11
  • 打赏
  • 举报
回复
需要看你的两个表之间的关联了,如果简单的条件就可以唯一确定一条记录,没有必要加那么多了
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
ehuman 2008-05-10
  • 打赏
  • 举报
回复
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。
flg_inwind 2008-05-10
  • 打赏
  • 举报
回复
数据库设计是就不应该设计可以为空的字段
以空间换时间。
hebo2005 2008-05-10
  • 打赏
  • 举报
回复
又是 is not null 又是or 还有<>
这个SQL能快起来嘛
pwpw 2008-05-10
  • 打赏
  • 举报
回复
建索引吧,或者把表分区
dl110 2008-05-10
  • 打赏
  • 举报
回复
将这句:where (glcode is not null or glcode <> '') and glDescription is null
换成:where length(trim(ratecode))>0 and glDescription is null
hery2002 2008-05-10
  • 打赏
  • 举报
回复
先创建个临时表来存储BB表的数据吧,
然后在更新AA表,这样会快些!:)
chenhongxin 2008-05-10
  • 打赏
  • 举报
回复
你跑一下执行线索,看看你的时间都花在那里了
我看你的语句效率不太高呀 比如说 glcode is not null or glcode <> ''
你无意中加了太多的索引

17,377

社区成员

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

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