关于sql语句的优化问题

yuzhong218 2012-05-10 02:28:20
sql语句如下所示:
select a.an, a.num, b.num
from (select a.an, count(distinct b.cp) as num
from xq_jzw a, paper b
where a.is_xq = 'Y'
and instr(b.ad,a.an) =1
group by a.an) a,
(select a.a, sum(bn.cp) as num
from xq_jzw a, paper b
where a.is_xq = 'Y'
and instr(b.ad,a.an) =1
group by a.an) b
where a.n = b.n
该语句的执行计划如下:
SELECT STATEMENT, GOAL = ALL_ROWS Cost=43592665 Cardinality=2457 Bytes=270270
HASH JOIN Cost=43592665 Cardinality=2457 Bytes=270270
VIEW Object owner=PORTAL_DATA Cost=21705301 Cardinality=2457 Bytes=135135
SORT GROUP BY Cost=21705301 Cardinality=2457 Bytes=105651
NESTED LOOPS Cost=21691981 Cardinality=66218330 Bytes=2847388190
TABLE ACCESS FULL Object owner=PORTAL_DATA Object name=xq_jzw Cost=790 Cardinality=2458 Bytes=46702
TABLE ACCESS FULL Object owner=PORTAL_DATA Object name=paper Cost=8825 Cardinality=26941 Bytes=646584
VIEW Object owner=PORTAL_DATA Cost=21887364 Cardinality=2457 Bytes=135135
HASH GROUP BY Cost=21887364 Cardinality=2457 Bytes=95823
NESTED LOOPS Cost=21874044 Cardinality=66218330 Bytes=2582514870
TABLE ACCESS FULL Object owner=PORTAL_DATA Object name=xq_jzw Cost=790 Cardinality=2458 Bytes=46702
TABLE ACCESS FULL Object owner=PORTAL_DATA Object name=paper Cost=8899 Cardinality=26941 Bytes=538820
请问:该语句怎么优化一下,让其执行速度比较快,xq_jzw数据量17万,paper数据量300万
...全文
97 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuzhong218 2012-05-11
  • 打赏
  • 举报
回复
呵呵,结贴了,谢谢各位的帮助
yuzhong218 2012-05-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

这样就ok了,执行计划会更准确一些。像1楼那么改的话,能减少一半能必要的资源消耗。
where a.is_xq = 'Y'
and instr(b.ad,a.an) =1
这里能优化的话,性能问题就差不多解决了。首先,我的疑问是,你看看
select * from xq_jzw a, paper b
where a.is_xq = 'Y'
and instr(b.ad,……
[/Quote]是会有重复的数据,但是甲方就是想看看paper 的数据包含xq_jzw 的数据能否关联出的数据更多些
小灰狼W 2012-05-10
  • 打赏
  • 举报
回复
这样就ok了,执行计划会更准确一些。像1楼那么改的话,能减少一半能必要的资源消耗。
where a.is_xq = 'Y'
and instr(b.ad,a.an) =1
这里能优化的话,性能问题就差不多解决了。首先,我的疑问是,你看看
select * from xq_jzw a, paper b
where a.is_xq = 'Y'
and instr(b.ad,a.an) =1
这里的结果是对的吗?你确定没有重复?
yuzhong218 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 6 楼 的回复:

引用 3 楼 的回复:

统计信息不准确,应该重新收集
begin
dbms_stats.gather_table_stats('PORTAL_DATA','XQ_JZW');
dbms_stats.gather_table_stats('PORTAL_DATA','PAPER');
end;
/

不太懂,这一块是什么意思啊?

重新采集……
[/Quote] 呵呵,还是不太懂,我在command窗口下执行了这条命令,提示存储成功创建,怎么查看重新收集的信息啊?
秋雨飘落 2012-05-10
  • 打赏
  • 举报
回复
头痛~~~
小灰狼W 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 3 楼 的回复:

统计信息不准确,应该重新收集
begin
dbms_stats.gather_table_stats('PORTAL_DATA','XQ_JZW');
dbms_stats.gather_table_stats('PORTAL_DATA','PAPER');
end;
/

不太懂,这一块是什么意思啊?
[/Quote]
重新采集统计信息。统计信息不正确会影响执行计划
yuzhong218 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

统计信息不准确,应该重新收集
begin
dbms_stats.gather_table_stats('PORTAL_DATA','XQ_JZW');
dbms_stats.gather_table_stats('PORTAL_DATA','PAPER');
end;
/
[/Quote]
不太懂,这一块是什么意思啊?
yuzhong218 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

问题出在xq_jzw a, paper b两表的连接上,连接条件instr()这里代价很大。结果对吗?连接后的结果是不是有很多重复的记录
[/Quote]
是有些重复记录,但是是为了关联出的记录更多些
小灰狼W 2012-05-10
  • 打赏
  • 举报
回复
统计信息不准确,应该重新收集
begin
dbms_stats.gather_table_stats('PORTAL_DATA','XQ_JZW');
dbms_stats.gather_table_stats('PORTAL_DATA','PAPER');
end;
/
小灰狼W 2012-05-10
  • 打赏
  • 举报
回复
问题出在xq_jzw a, paper b两表的连接上,连接条件instr()这里代价很大。结果对吗?连接后的结果是不是有很多重复的记录
  • 打赏
  • 举报
回复

select a.an,
count(distinct b.cp) as c_num
sum(bn.cp) as s_num,
from xq_jzw a, paper b
where a.is_xq = 'Y'
and instr(b.ad,a.an) =1
group by a.an

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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