这个sql为什么cbo没有重写

[复制链接]
查看11 | 回复8 | 2012-5-21 10:19:41 | 显示全部楼层 |阅读模式
SQL> prompt 18c
18c
SQL> select * from(
2select
3
c_name,
4
c_custkey,
5
o_orderkey,
6
o_orderdate,
7
o_totalprice,
8
sum_l_quantity
9from
10
customer,
11
orders,
12
( select
13
l_orderkey,sum(l_quantity)sum_l_quantity
14
from
15
lineitem
16
group by
17
l_orderkey having
18
sum(l_quantity) > 312
19
)lineitem
20where
21
c_custkey = o_custkey
22
and o_orderkey = l_orderkey
23order by
24
o_totalprice desc,
25
o_orderdate
26)where rownum312)

Statistics
----------------------------------------------------------
169recursive calls

0db block gets
914940consistent gets
905472physical reads

0redo size
7033bytes sent via SQL*Net to client
586bytes received via SQL*Net from client

8SQL*Net roundtrips to/from client

1sorts (memory)

0sorts (disk)
100rows processed
SQL> prompt 18
18
SQL> select * from (select
2
c_name,
3
c_custkey,
4
o_orderkey,
5
o_orderdate,
6
o_totalprice,
7
sum(l_quantity)
8from
9
customer,
10
orders,
11
lineitem
12where
13
o_orderkey in (
14
select
15
l_orderkey
16
from
17
lineitem
18
group by
19
l_orderkey having
20
sum(l_quantity) > 312
21
)
22
and c_custkey = o_custkey
23
and o_orderkey = l_orderkey
24group by
25
c_name,
26
c_custkey,
27
o_orderkey,
28
o_orderdate,
29
o_totalprice
30order by
31
o_totalprice desc,
32
o_orderdate
33)where rownum312)

Statistics
----------------------------------------------------------
146recursive calls

0db block gets
1568034consistent gets
1551519physical reads

0redo size
7034bytes sent via SQL*Net to client
586bytes received via SQL*Net from client

8SQL*Net roundtrips to/from client

1sorts (memory)

0sorts (disk)
100rows processed

回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
18是原文
18c是人工改写
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
一个更差的改写
SQL> prompt 18b
18b
SQL> select * from(select
2
c_name,
3
c_custkey,
4
o_orderkey,
5
o_orderdate,
6
o_totalprice,
7
sum(l_quantity)
8from
9
customer,
10
orders,
11
lineitem
12where
13
c_custkey = o_custkey
14
and o_orderkey = l_orderkey
15group by
16
c_name,
17
c_custkey,
18
o_orderkey,
19
o_orderdate,
20
o_totalprice having
21
sum(l_quantity) > 312
22order by
23
o_totalprice desc,
24
o_orderdate
25)where rownum312)
5 - access("O_ORDERKEY"="L_ORDERKEY")
6 - access("C_CUSTKEY"="O_CUSTKEY")

Statistics
----------------------------------------------------------
2424recursive calls
70db block gets
914937consistent gets
1073105physical reads

0redo size
7034bytes sent via SQL*Net to client
586bytes received via SQL*Net from client

8SQL*Net roundtrips to/from client

0sorts (memory)

1sorts (disk)
100rows processed

回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
重写指得是?
明显第一个更好啊~排序更少的内容
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
原帖由 iori809 于 2011-1-30 14:43 发表
重写指得是?
明显第一个更好啊~排序更少的内容

是啊,所以我疑惑cbo怎么不能把18转成18c,
18b更差的原因是什么?
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
原因显而易见啊~
第一个排序的内容更少
group byl_orderkey having
而下面的
group by c_name,

c_custkey,

o_orderkey,

o_orderdate,

o_totalprice
优化其不是万能的
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
关注。。。
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
这么多TABLE ACCESS FULL,试试把group by 改写成sum() over(partition by)分析函数
回复

使用道具 举报

千问 | 2012-5-21 10:19:41 | 显示全部楼层
原帖由 sleepzzzzz 于 2011-1-31 11:35 发表
这么多TABLE ACCESS FULL,试试把group by 改写成sum() over(partition by)分析函数

第一个执行计划只有一次全表扫描
LINEITEM
回复

使用道具 举报

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

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行