查看: 5010|回复: 8

[Tips] When 11g SQL is faster than 10g without any plan change…

[复制链接]
论坛徽章:
92
2011新春纪念徽章
日期:2011-01-25 15:42:33咸鸭蛋
日期:2012-03-19 10:46:00版主1段
日期:2012-05-15 15:24:11奥运会纪念徽章:排球
日期:2012-08-29 07:02:50奥运会纪念徽章:跳水
日期:2012-09-26 06:44:27ITPUB 11周年纪念徽章
日期:2012-09-28 17:34:42ITPUB 11周年纪念徽章
日期:2012-10-09 18:03:32奥运会纪念徽章:击剑
日期:2012-10-12 07:20:332013年新春福章
日期:2013-02-25 14:51:242012新春纪念徽章
日期:2012-02-13 15:13:20
跳转到指定楼层
1#
发表于 2007-8-10 10:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
还是看看原版的链接

http://www.pythian.com/blogs/542/my-work-before-12g-goes-out


« Oracle 11g (11.1.0.6) and Documentation Available on OTNWhen 11g SQL is faster than 10g without any plan change…
August 9th, 2007 - by Gregory Guillou
11g is out !

We all know what Oracle Marketing will say : 11g is x% faster than 10g. And guess what ? I’ll need 3 more years to decipher all those changes that enhance the 11g optimizer and the query algorithms. This will probably be time for 12g then !

To avoid being too far behind you guys that are already upgrading to 11g, I’ve decided to invest on 11g during the Beta Program. This has been a lot of fun and I wish I can share some of my findings with you. For example, did you know that 11g can go faster without any change at all ? The queries below will illustrate the change made to the Nested Loop Algorithm in 11g :

1°- First, you have to create and fill a table to run your query :

create table gark
  (id1 number not null,
   id2 number not null,
   id3 number not null);

begin
for i in 1..100000 loop
insert into gark(id1, id2, id3)
values (i, i, i);
end loop;
commit;
end;
/

create unique index gark_idx on gark(id1, id3);

begin
dbms_stats.gather_table_stats(
   user,
   'GARK',
   cascade=>true,
   estimate_percent=>100,
   method_opt=>'FOR ALL COLUMNS SIZE 254',
   no_invalidate=> false);
end;
/2°- Then, run the query below on a 10g database :

set autotrace traceonly

select /*+ use_nl(A B) */count(a.id3)
from gark a, gark b
where a.id1=b.id2;

Execution Plan
----------------------------
Plan hash value: 3137705415

--------------------------------------------------------
| Id  | Operation           | Name     | Rows  |  Cost |
--------------------------------------------------------
|   0 | SELECT STATEMENT    |          |     1 |  100K |
|   1 |  SORT AGGREGATE     |          |     1 |       |
|   2 |   NESTED LOOPS      |          |   100K|  100K |
|   3 |    TABLE ACCESS FULL| GARK     |   100K|    65 |
|*  4 |    INDEX RANGE SCAN | GARK_IDX |     1 |     1 |
--------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

4 - access("A"."ID1"="B"."ID2"

Statistics
----------------------------------------------------------
       0  recursive calls
       0  db block gets
  100556  consistent gets
       0  physical reads
       0  redo size
     415  bytes sent via SQL*Net to client
     385  bytes received via SQL*Net from client
       2  SQL*Net roundtrips to/from client
       0  sorts (memory)
       0  sorts (disk)
       1  rows processed3°- Run the same query on 11g (There is no trick here, the plan is the right plan, I’ve check it with dbms_xplan.display_cursor) :

set autotrace traceonly

select /*+ use_nl(A B) */count(a.id3)
from gark a, gark b
where a.id1=b.id2;

Execution Plan
----------------------------------------------------------
Plan hash value: 3137705415

-------------------------------------------------------
| Id  | Operation           | Name     | Rows  | Cost |
-------------------------------------------------------
|   0 | SELECT STATEMENT    |          |     1 | 100K |
|   1 |  SORT AGGREGATE     |          |     1 |      |
|   2 |   NESTED LOOPS      |          |   100K| 100K |
|   3 |    TABLE ACCESS FULL| GARK     |   100K|  105 |
|*  4 |    INDEX RANGE SCAN | GARK_IDX |     1 |   11 |
-------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

4 - access("A"."ID1"="B"."ID2"

Statistics
----------------------------------------------------------
       0  recursive calls
       0  db block gets
    3373  consistent gets
       0  physical reads
       0  redo size
     422  bytes sent via SQL*Net to client
     415  bytes received via SQL*Net from client
       2  SQL*Net roundtrips to/from client
       0  sorts (memory)
       0  sorts (disk)
       1  rows processedIf you know how the NL algorithm works in 10g, it’s quite easy to understand how it has been modified. The good news with that is that it won’t only speed up queries with optimal plans but also speed up queries with sub optimal plans that are using Nested Loops.

-Grégory

PS : Don’t dream, it won’t be so huge with a real workload and of course HASH JOIN stays the most efficient plan to solve this particular query.
论坛徽章:
151
2014年新春福章
日期:2014-04-17 11:38:13奥运会纪念徽章:皮划艇静水
日期:2012-07-31 15:42:58奥运会纪念徽章:田径
日期:2012-07-10 16:21:10奥运会纪念徽章:跆拳道
日期:2012-06-20 22:07:29奥运会纪念徽章:皮划艇静水
日期:2012-06-16 02:55:21奥运会纪念徽章:曲棍球
日期:2012-06-13 10:09:19蛋疼蛋
日期:2012-05-19 23:20:41迷宫蛋
日期:2012-05-16 17:35:25版主2段
日期:2012-05-15 15:24:11双黄蛋
日期:2012-03-19 19:34:04
2#
发表于 2007-8-10 13:36 | 只看该作者
我测试了一下10G下的,过会看看11G
[php]

SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bi
PL/SQL Release 10.2.0.2.0 - Production
CORE    10.2.0.2.0      Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.2.0 - Productio
NLSRTL Version 10.2.0.2.0 - Production

Elapsed: 00:00:00.07
SQL> create table gark
  2  (id1 number not null,
  3  id2 number not null,
  4  id3 number not null);


Table created.

Elapsed: 00:00:00.10
SQL> SQL>
SQL> begin
  2  for i in 1..100000 loop
  3  insert into gark(id1, id2, id3)
  4  values (i, i, i);
  5  end loop;
  6  commit;
  7  end;
  8  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:04.43
SQL> create unique index gark_idx on gark(id1, id3);

Index created.

Elapsed: 00:00:00.25
SQL> begin
  2  dbms_stats.gather_table_stats(
  3  user,
  4  'GARK',
  5  cascade=>true,
  6  estimate_percent=>100,
  7  method_opt=>'FOR ALL COLUMNS SIZE 254',
  8  no_invalidate=> false);
  9  end;
10  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:03.32
SQL> set autotrace traceonly
SQL>
SQL> select /*+ use_nl(A B) */count(a.id3)
  2  from gark a, gark b
  3  where a.id1=b.id2;

Elapsed: 00:00:00.60

Execution Plan
----------------------------------------------------------
Plan hash value: 3257338542

--------------------------------------------------------------------------------
| Id  | Operation           | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |          |     1 |    10 |   100K  (1)| 00:23:22 |
|   1 |  SORT AGGREGATE     |          |     1 |    10 |            |          |
|   2 |   NESTED LOOPS      |          |   100K|   976K|   100K  (1)| 00:23:22 |
|   3 |    TABLE ACCESS FULL| GARK     |   100K|   488K|    58   (2)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN | GARK_IDX |     1 |     5 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("A"."ID1"="B"."ID2"


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
     100327  consistent gets
          0  physical reads
          0  redo size
        519  bytes sent via SQL*Net to client
        469  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
..
[/php]

BTW,NND,本来都下好了,结果磁盘空间不够,最后一分钟硬生生地把我给弄没了,害我重新下载,郁闷。

使用道具 举报

回复
论坛徽章:
47
马上加薪
日期:2014-02-19 11:55:142011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:41:012010新春纪念徽章
日期:2010-03-01 11:20:512010年世界杯参赛球队:日本
日期:2010-02-26 11:04:222010新春纪念徽章
日期:2010-01-04 08:33:08祖国60周年纪念徽章
日期:2009-10-09 08:28:00生肖徽章2007版:牛
日期:2009-09-10 11:14:59
3#
发表于 2007-8-10 13:39 | 只看该作者
最初由 blue_prince 发布
[B]我测试了一下10G下的,过会看看11G


BTW,NND,本来都下好了,结果磁盘空间不够,最后一分钟硬生生地把我给弄没了,害我重新下载,郁闷。 [/B]

用下载工具啊

使用道具 举报

回复
论坛徽章:
151
2014年新春福章
日期:2014-04-17 11:38:13奥运会纪念徽章:皮划艇静水
日期:2012-07-31 15:42:58奥运会纪念徽章:田径
日期:2012-07-10 16:21:10奥运会纪念徽章:跆拳道
日期:2012-06-20 22:07:29奥运会纪念徽章:皮划艇静水
日期:2012-06-16 02:55:21奥运会纪念徽章:曲棍球
日期:2012-06-13 10:09:19蛋疼蛋
日期:2012-05-19 23:20:41迷宫蛋
日期:2012-05-16 17:35:25版主2段
日期:2012-05-15 15:24:11双黄蛋
日期:2012-03-19 19:34:04
4#
发表于 2007-8-10 13:41 | 只看该作者
最初由 hotiice 发布
[B]
用下载工具啊 [/B]


公司不能用下载工具的

使用道具 举报

回复
论坛徽章:
139
2009日食纪念
日期:2009-07-22 09:30:00ITPUB8周年纪念徽章
日期:2009-09-27 10:21:21祖国60周年纪念徽章
日期:2009-10-09 08:28:002010年世界杯参赛球队:葡萄牙
日期:2010-01-18 09:23:302010年世界杯参赛球队:意大利
日期:2010-01-21 07:30:192010年世界杯参赛球队:南非
日期:2010-01-22 09:48:242010年世界杯参赛球队:加纳
日期:2010-02-13 16:34:422010新春纪念徽章
日期:2010-03-01 11:04:572010年世界杯参赛球队:斯洛伐克
日期:2010-05-21 11:24:312010年世界杯参赛球队:塞尔维亚
日期:2010-06-30 13:43:14
5#
发表于 2007-8-10 14:46 | 只看该作者
下的好慢,只有70K,估计全世界都在下

使用道具 举报

回复
论坛徽章:
151
2014年新春福章
日期:2014-04-17 11:38:13奥运会纪念徽章:皮划艇静水
日期:2012-07-31 15:42:58奥运会纪念徽章:田径
日期:2012-07-10 16:21:10奥运会纪念徽章:跆拳道
日期:2012-06-20 22:07:29奥运会纪念徽章:皮划艇静水
日期:2012-06-16 02:55:21奥运会纪念徽章:曲棍球
日期:2012-06-13 10:09:19蛋疼蛋
日期:2012-05-19 23:20:41迷宫蛋
日期:2012-05-16 17:35:25版主2段
日期:2012-05-15 15:24:11双黄蛋
日期:2012-03-19 19:34:04
6#
发表于 2007-8-10 15:45 | 只看该作者
11G的结果出来了

[php]

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
PL/SQL Release 11.1.0.6.0 - Production
CORE    11.1.0.6.0      Production
TNS for Linux: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production

Elapsed: 00:00:00.00
SQL> create table gark
  2  (id1 number not null,
  3  id2 number not null,
  4  id3 number not null);

Table created.

Elapsed: 00:00:00.15
SQL> begin
  2  for i in 1..100000 loop
  3  insert into gark(id1, id2, id3)
  4  values (i, i, i);
  5  end loop;
  6  commit;
  7  end;
  8  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:04.34
SQL> create unique index gark_idx on gark(id1, id3);

Index created.

Elapsed: 00:00:00.28
SQL> begin
  2  dbms_stats.gather_table_stats(
  3  user,
  4  'GARK',
  5  cascade=>true,
  6  estimate_percent=>100,
  7  method_opt=>'FOR ALL COLUMNS SIZE 254',
  8  no_invalidate=> false);
  9  end;
10  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:01.97
SQL> set autotrace traceonly

select /*+ use_nl(A B) */count(a.id3)
from gark a, gark b
SQL> SQL>   2    3  where a.id1=b.id2;

Elapsed: 00:00:00.14

Execution Plan
----------------------------------------------------------
Plan hash value: 3137705415

--------------------------------------------------------------------------------
| Id  | Operation           | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |          |     1 |    10 |   100K  (1)| 00:20:02 |
|   1 |  SORT AGGREGATE     |          |     1 |    10 |            |          |
|   2 |   NESTED LOOPS      |          |   100K|   976K|   100K  (1)| 00:20:02 |
|   3 |    TABLE ACCESS FULL| GARK     |   100K|   488K|   103   (1)| 00:00:02 |
|*  4 |    INDEX RANGE SCAN | GARK_IDX |     1 |     5 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("A"."ID1"="B"."ID2"


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       3323  consistent gets
          0  physical reads
          0  redo size
        415  bytes sent via SQL*Net to client
        385  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

..
[/php]

使用道具 举报

回复
论坛徽章:
151
2014年新春福章
日期:2014-04-17 11:38:13奥运会纪念徽章:皮划艇静水
日期:2012-07-31 15:42:58奥运会纪念徽章:田径
日期:2012-07-10 16:21:10奥运会纪念徽章:跆拳道
日期:2012-06-20 22:07:29奥运会纪念徽章:皮划艇静水
日期:2012-06-16 02:55:21奥运会纪念徽章:曲棍球
日期:2012-06-13 10:09:19蛋疼蛋
日期:2012-05-19 23:20:41迷宫蛋
日期:2012-05-16 17:35:25版主2段
日期:2012-05-15 15:24:11双黄蛋
日期:2012-03-19 19:34:04
7#
发表于 2007-8-10 15:46 | 只看该作者
果然性能有了大幅的提升

使用道具 举报

回复
论坛徽章:
56
马上加薪
日期:2014-12-22 13:11:12复活蛋
日期:2011-08-31 14:55:20ITPUB9周年纪念徽章
日期:2010-10-08 09:32:252010世博会纪念徽章
日期:2010-09-07 08:52:012010年世界杯参赛球队:瑞士
日期:2010-06-24 09:16:222010年世界杯参赛球队:德国
日期:2010-04-27 09:40:272010年世界杯参赛球队:韩国
日期:2010-04-23 23:34:412010新春纪念徽章
日期:2010-03-01 11:06:222010新春纪念徽章
日期:2010-01-04 08:33:08参与WIN7挑战赛纪念
日期:2009-11-06 10:44:24
8#
发表于 2007-8-13 15:09 | 只看该作者
的确有意思

使用道具 举报

回复
论坛徽章:
19
生肖徽章:羊
日期:2006-09-06 21:18:482012新春纪念徽章
日期:2012-01-04 11:51:22ITPUB十周年纪念徽章
日期:2011-11-01 16:21:15CTO参与奖
日期:2009-02-12 11:45:48生肖徽章2007版:龙
日期:2008-12-16 14:04:41奥运会纪念徽章:篮球
日期:2008-10-24 13:29:38奥运会纪念徽章:沙滩排球
日期:2008-07-02 12:09:31生肖徽章2007版:鼠
日期:2008-01-02 17:35:53ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44会员2007贡献徽章
日期:2007-09-26 18:42:10
9#
发表于 2007-8-13 15:51 | 只看该作者
刚出来就下载好了,忙的一直没顾上装,只能看你们玩了

使用道具 举报

回复

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

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表