全部博文(136)
分类: Oracle
2009-03-29 22:42:49
C:\Documents and Settings\yuechao.tianyc>sqlplus test/test
SQL*Plus: Release 10.2.0.1.0 - Production on 星期日 3月 29 22:37:13 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options SQL> create table test_big as select * from dba_objects;
表已创建。
SQL> insert into test_big select * from test_big;
已创建50000行。
SQL> /
已创建100000行。
...
SQL> /
已创建6400000行。
SQL> commit;
提交完成。
SQL> create table test_small as select * from dba_objects where rownum < 5001;
表已创建。
SQL> create index ind_test_big on test_big(object_id);
索引已创建。
SQL> create index ind_test_small on test_small(object_id);
索引已创建。 |
-- 1. 使用test_big作为驱动行源
-- 执行步骤:(1)全表扫描test_big,得到行源A;(2)将A作为驱动行源,嵌套循环连接索引ind_test_small,得到行源B;(3)将行源B通过索引ind_test_small与表test_small关联,得到结果集。
SQL> explain plan for
2 select/*+ordered use_nl(test_big,test_small)*/ count(test_big.object_name||test_small.object_name)
3 from test_big, test_small where test_big.object_id = test_small.object_id;
已解释。
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Plan hash value: 3591390207
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 158 | 15M (1)| 53:19:22 |
| 1 | SORT AGGREGATE | | 1 | 158 | | |
| 2 | TABLE ACCESS BY INDEX ROWID| TEST_SMALL | 1 | 79 | 2 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 1247K| 187M| 15M (1)| 53:19:22 |
| 4 | TABLE ACCESS FULL | TEST_BIG | 14M| 1106M| 39134 (2)| 00:07:50 |
|* 5 | INDEX RANGE SCAN | IND_TEST_SMALL | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------- Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("test_big"."OBJECT_ID"="test_small"."OBJECT_ID")
Note
-----
- dynamic sampling used for this statement
已选择21行。 -- 2. 使用test_small作为驱动行源
-- 执行步骤:(1)全表扫描test_small,得到行源A;(2)将A作为驱动行源,嵌套循环连接索引ind_test_big,得到行源B;(3)将行源B通过索引ind_test_big与表test_big关联,得到结果集。
SQL> explain plan for
2 select/*+ordered use_nl(test_big,test_small)*/ count(test_big.object_name||test_small.object_name)
3 from test_small, test_big where test_big.object_id = test_small.object_id;
已解释。
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Plan hash value: 1952886871
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 158 | 1098K (1)| 03:39:42 |
| 1 | SORT AGGREGATE | | 1 | 158 | | |
| 2 | TABLE ACCESS BY INDEX ROWID| TEST_BIG | 257 | 20303 | 226 (0)| 00:00:03 |
| 3 | NESTED LOOPS | | 1247K| 187M| 1098K (1)| 03:39:42 |
| 4 | TABLE ACCESS FULL | TEST_SMALL | 4854 | 374K| 17 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | IND_TEST_BIG | 257 | | 2 (0)| 00:00:01 | ----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("test_big"."OBJECT_ID"="test_small"."OBJECT_ID")
Note
-----
- dynamic sampling used for this statement
已选择21行。 |
SQL> set timing on
-- 使用大表作为驱动行源
SQL> select/*+ordered use_nl(test_big,test_small)*/ count(test_big.object_name||test_small.object_name)
2 from test_big, test_small where test_big.object_id = test_small.object_id;
COUNT(TEST1.OBJECT_NAME||TEST2.OBJECT_NAME)
-------------------------------------------
12800000
已用时间: 00: 00: 36.70
-- 使用小表作为驱动行源
SQL> select/*+ordered use_nl(test1,test2)*/ count(test1.object_name||test2.object_name)
2 from test2, test1 where test1.object_id = test2.object_id;
COUNT(TEST1.OBJECT_NAME||TEST2.OBJECT_NAME)
-------------------------------------------
12800000
已用时间: 00: 02: 42.89 |
SQL> select count(*) from test_big;
COUNT(*)
----------
12800000
已用时间: 00: 00: 26.14
SQL> set timing off
SQL> explain plan for
2 select count(*) from test_big;
已解释。
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
--------------------------------------------------------------------------
Plan hash value: 3224830981
-----------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 38982 (2)| 00:07:48 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| TEST_BIG | 14M| 38982 (2)| 00:07:48 |
-----------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement
已选择13行。 |
SQL> explain plan for
2 select count(test_big.object_name||test_small.object_name)
3 from test_big, test_small where test_big.object_id = test_small.object_id;
已解释。
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Plan hash value: 1810242240
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 158 | 39404 (3)| 00:07:53 |
| 1 | SORT AGGREGATE | | 1 | 158 | | |
|* 2 | HASH JOIN | | 1247K| 187M| 39404 (3)| 00:07:53 |
| 3 | TABLE ACCESS FULL| TEST_SMALL | 4854 | 374K| 17 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| TEST_BIG | 14M| 1106M| 39134 (2)| 00:07:50 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("TEST_BIG"."OBJECT_ID"="TEST_SMALL"."OBJECT_ID")
Note
-----
- dynamic sampling used for this statement
已选择20行。 SQL> set timing on
SQL> select count(test_big.object_name||test_small.object_name)
2 from test_big, test_small where test_big.object_id = test_small.object_id;
COUNT(TEST_BIG.OBJECT_NAME||TEST_SMALL.OBJECT_NAME)
---------------------------------------------------
1280000
已用时间: 00: 00: 21.42 |
chinaunix网友2009-04-21 22:48:30
nested join一般适用于根据outertable循环,对于outertable中的每一条数据,能快速定位到innertable中的表的连接,具体到真是的应用环境中,理论就是理论了。一个itput版主说过,脱离了真实得数据环境是无法谈优化的。田哥,最近过的如何?