【求助】 对一个sql语句的执行效率有点不解

[复制链接]
查看11 | 回复7 | 2010-3-1 11:19:06 | 显示全部楼层 |阅读模式
表 t_a (
idvarchar2(20);
namevarchar2(200);
date date;
type varchar2(3);
valuenumber(16,4);
-- 其它字段若干
)
现约 30,000 条数据
表 t_b (
id varchar2(8);
name varchar2(100);
--其它字段若干
)
现约500条记录
两个表都没有任何keys,checks,t_a 的 id 实际上是来自 t_b,name是冗余过来的.
要找出 t_a中所有id前4位不在t_b的id前4位中的记录,并根据date和type汇总value
我这样写的
select a.id
,a.name,
,a.date
,a.type
,sum(a.value)
from t_a a
where a.substr(a.id,0,4) not in (select substr(id, 0,4) from t_b)
group by a.id, a.name, a.type
plsql中执行,检索出来1000条左右的数据,花费了5秒多
感觉费时有点长,请问这种情况正常吗?表 t_b 数据量基本上不会变了,t_a可能数据量比较大
单从sql语句本身来说有什么能改进的地方吗?
回复

使用道具 举报

千问 | 2010-3-1 11:19:06 | 显示全部楼层
try using
not exist
回复

使用道具 举报

千问 | 2010-3-1 11:19:06 | 显示全部楼层
试用外连接呢
回复

使用道具 举报

千问 | 2010-3-1 11:19:06 | 显示全部楼层
刚试了下
where a.substr(a.id,0,4) not in (select substr(id, 0,4) from t_b)
==>
where not exists (select * from t_b b where sub(b.id, 0, 4) = substr(a.id, 0, 4)
时间由 5.313 sec => 5.156
回复

使用道具 举报

千问 | 2010-3-1 11:19:06 | 显示全部楼层
原帖由 mychary 于 2007-12-25 15:22 发表
try using
not exist

not exists将会更慢~~~
语句很简单,不具备再调优的余地~~~
我觉着主开销是在a.substr(a.id,0,4),如果希望更快,建一个substr(a.id,0,4)的索引应该有效~
回复

使用道具 举报

千问 | 2010-3-1 11:19:06 | 显示全部楼层
原帖由 oraclesea 于 2007-12-25 15:40 发表
试用外连接呢

select t_a.*
join t_b.*
on substr(t_a.id, 0, 4) = substr(t_b.id, 0, 4)
然后再group?
这样吗?
on substr(t_a.id, 0, 4)substr(t_b.id, 0, 4) 这样会出错吧?
不好意思,没想明白
回复

使用道具 举报

千问 | 2010-3-1 11:19:06 | 显示全部楼层
这样的语句会比 not in 要快一点.
但我在执行计划中比较了一下,速度并不会快很多,所以问题应该还是因为
substr 此函数的原因.
select a.idate,a.type,sum(a.value)
from t_a a
where
not exists
(select b.id,b.name from t_b b
where substr(a.id,0,4)=substr(b.id,0,4))
group by a.idate,a.type
回复

使用道具 举报

千问 | 2010-3-1 11:19:06 | 显示全部楼层
明白了,谢谢楼上各位
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行