Oracle 11g Release 1 (11.1)——聚簇和非聚簇的简单查询比较
2012-08-25 23:14
681 查看
本文内容
创建非聚簇的相关表创建聚簇
简单查询比较
创建非聚簇的相关模式对象
创建EMPLOYEES和DEPTMENTS表。[code]--Createtable
createtableEMPLOYEES
(
EMPNONUMBER(4)notnull,
ENAMEVARCHAR2(10),
JOBVARCHAR2(9),
MGRNUMBER(4),
HIREDATEDATE,
SALNUMBER(7,2),
COMMNUMBER(7,2),
DEPTNONUMBER(3)
)
tablespaceMYTBS
pctfree10
initrans1
maxtrans255
storage
(
initial64K
next1M
minextents1
maxextentsunlimited
);
--Create/Recreateprimary,uniqueandforeignkeyconstraints
altertableEMPLOYEES
addconstraintPK_EMPLOYEES_EMPNOprimarykey(EMPNO)
usingindex
tablespaceMYTBS
pctfree10
initrans2
maxtrans255
storage
(
initial64K
next1M
minextents1
maxextentsunlimited
);
--Createtable
createtableDEPTMENTS
(
DEPTNONUMBER(3)notnull,
DNAMEVARCHAR2(14),
LOCVARCHAR2(13)
)
tablespaceMYTBS
pctfree10
initrans1
maxtrans255
storage
(
initial64K
next1M
minextents1
maxextentsunlimited
);
--Create/Recreateprimary,uniqueandforeignkeyconstraints
altertableDEPTMENTS
addconstraintPK_DEPTMENTS_DEPTNOprimarykey(DEPTNO)
usingindex
tablespaceMYTBS
pctfree10
initrans2
maxtrans255
storage
(
initial64K
next1M
minextents1
maxextentsunlimited
);
[/code]
创建聚簇的相关模式对象
创建聚簇emp_dept、聚簇的表emp和dept,以及聚簇索引emp_dept_index。[code]--CreateCluster
CREATECLUSTERemp_dept(deptnoNUMBER(3))
SIZE600
TABLESPACEmytbs
STORAGE(INITIAL200K
NEXT300K
MINEXTENTS2
PCTINCREASE33);
--Createtable
createtableEMP
(
EMPNONUMBER(4)notnull,
ENAMEVARCHAR2(10),
JOBVARCHAR2(9),
MGRNUMBER(4),
HIREDATEDATE,
SALNUMBER(7,2),
COMMNUMBER(7,2),
DEPTNONUMBER(3)
)
clusterEMP_DEPT(DEPTNO);
--Create/Recreateprimary,uniqueandforeignkeyconstraints
altertableEMP
addconstraintPK_EMP_EMPNOprimarykey(EMPNO)
usingindex
tablespaceMYTBS
pctfree10
initrans2
maxtrans255
storage
(
initial64K
next1M
minextents1
maxextentsunlimited
);
--Createtable
createtableDEPT
(
DEPTNONUMBER(3)notnull,
DNAMEVARCHAR2(14),
LOCVARCHAR2(13)
)
clusterEMP_DEPT(DEPTNO);
--Create/Recreateprimary,uniqueandforeignkeyconstraints
altertableDEPT
addconstraintPK_DEPT_DEPTNOprimarykey(DEPTNO)
usingindex
tablespaceMYTBS
pctfree10
initrans2
maxtrans255
storage
(
initial64K
next1M
minextents1
maxextentsunlimited
);
--CreateClusterindex
CREATEINDEXemp_dept_index
ONCLUSTERemp_dept
TABLESPACEMYTBS
STORAGE(INITIAL50K
NEXT50K
MINEXTENTS2
MAXEXTENTS10
PCTINCREASE33);
[/code]
向非聚簇的表和聚簇的表插入数据
下载并执行简单查询比较
示例1:比较等值连接[code] [code]SQL>;EXPLAINPLAN
2SETSTATEMENT_ID='ex_plan1'
3FOR
4SELECT*
5FROMemployeest1,deptmentst2
6WHEREt1.deptno=t2.deptno;
Explained
SQL>;SELECTplan_table_output
2FROMTABLE(DBMS_XPLAN.display(NULL,'ex_plan1','TYPICAL'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Planhashvalue:3396288718
----------------------------------------------------------------------------------------------------
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
----------------------------------------------------------------------------------------------------
|0|SELECTSTATEMENT||14|812|6(17)|00:00:01|
|1|MERGEJOIN||14|812|6(17)|00:00:01|
|2|TABLEACCESSBYINDEXROWID|DEPTMENTS|4|80|2(0)|00:00:01|
|3|INDEXFULLSCAN|PK_DEPTMENTS_DEPTNO|4||1(0)|00:00:01|
|*4|SORTJOIN||14|532|4(25)|00:00:01|
|5|TABLEACCESSFULL|EMPLOYEES|14|532|3(0)|00:00:01|
----------------------------------------------------------------------------------------------------
PredicateInformation(identifiedbyoperationid):
---------------------------------------------------
4-access("T1"."DEPTNO"="T2"."DEPTNO")
filter("T1"."DEPTNO"="T2"."DEPTNO")
18rowsselected
SQL>;EXPLAINPLAN
2SETSTATEMENT_ID='ex_plan2'
3FOR
4SELECT*
5FROMempt1,deptt2
6WHEREt1.deptno=t2.deptno;
Explained
SQL>;SELECTplan_table_output
2FROMTABLE(DBMS_XPLAN.display(NULL,'ex_plan2','TYPICAL'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Planhashvalue:2705476012
----------------------------------------------------------------------------------------
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
----------------------------------------------------------------------------------------
|0|SELECTSTATEMENT||14|812|6(17)|00:00:01|
|1|MERGEJOIN||14|812|6(17)|00:00:01|
|2|TABLEACCESSCLUSTER|DEPT|4|80|2(0)|00:00:01|
|3|INDEXFULLSCAN|EMP_DEPT_INDEX|1||1(0)|00:00:01|
|*4|SORTJOIN||14|532|4(25)|00:00:01|
|5|TABLEACCESSFULL|EMP|14|532|3(0)|00:00:01|
----------------------------------------------------------------------------------------
PredicateInformation(identifiedbyoperationid):
---------------------------------------------------
4-access("T1"."DEPTNO"="T2"."DEPTNO")
filter("T1"."DEPTNO"="T2"."DEPTNO")
18rowsselected
SQL>
[/code]
[/code]
示例2:比较左外连接
[code] [code]SQL>;EXPLAINPLAN
2SETSTATEMENT_ID='ex_plan1'
3FOR
4SELECT*
5FROMemployeest1LEFTJOINdeptmentst2ON(t1.deptno=t2.deptno);
Explained
SQL>;SELECTplan_table_output
2FROMTABLE(DBMS_XPLAN.display(NULL,'ex_plan1','TYPICAL'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Planhashvalue:47896472
--------------------------------------------------------------------------------
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
--------------------------------------------------------------------------------
|0|SELECTSTATEMENT||14|812|7(15)|00:00:01|
|*1|HASHJOINOUTER||14|812|7(15)|00:00:01|
|2|TABLEACCESSFULL|EMPLOYEES|14|532|3(0)|00:00:01|
|3|TABLEACCESSFULL|DEPTMENTS|4|80|3(0)|00:00:01|
--------------------------------------------------------------------------------
PredicateInformation(identifiedbyoperationid):
---------------------------------------------------
1-access("T1"."DEPTNO"="T2"."DEPTNO"(+))
15rowsselected
SQL>;EXPLAINPLAN
2SETSTATEMENT_ID='ex_plan2'
3FOR
4SELECT*
5FROMempt1LEFTJOINdeptt2ON(t1.deptno=t2.deptno);
Explained
SQL>;SELECTplan_table_output
2FROMTABLE(DBMS_XPLAN.display(NULL,'ex_plan2','TYPICAL'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Planhashvalue:3577968021
------------------------------------------------------------------------------
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
------------------------------------------------------------------------------
|0|SELECTSTATEMENT||14|812|17(0)|00:00:01|
|1|NESTEDLOOPSOUTER||14|812|17(0)|00:00:01|
|2|TABLEACCESSFULL|EMP|14|532|3(0)|00:00:01|
|*3|TABLEACCESSCLUSTER|DEPT|1|20|1(0)|00:00:01|
------------------------------------------------------------------------------
PredicateInformation(identifiedbyoperationid):
---------------------------------------------------
3-filter("T1"."DEPTNO"="T2"."DEPTNO"(+))
15rowsselected
SQL>
[/code]
[/code]
示例3:比较选择部门编号为10的所有员工
[code] [code]SQL>;EXPLAINPLAN
2SETSTATEMENT_ID='ex_plan1'
3FOR
4SELECTt1.dname,
5t1.loc,
6t2.empno,
7t2.ename,
8t2.job,
9t2.mgr,
10t2.hiredate,
11t2.sal
12FROMdeptmentst1LEFTJOINemployeest2ON(t1.deptno=t2.deptno)
13WHEREt1.deptno=10;
Explained
SQL>;SELECTplan_table_output
2FROMTABLE(DBMS_XPLAN.display(NULL,'ex_plan1','TYPICAL'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Planhashvalue:2928143533
----------------------------------------------------------------------------------------------------
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
----------------------------------------------------------------------------------------------------
|0|SELECTSTATEMENT||3|171|4(0)|00:00:01|
|1|NESTEDLOOPSOUTER||3|171|4(0)|00:00:01|
|2|TABLEACCESSBYINDEXROWID|DEPTMENTS|1|20|1(0)|00:00:01|
|*3|INDEXUNIQUESCAN|PK_DEPTMENTS_DEPTNO|1||0(0)|00:00:01|
|*4|TABLEACCESSFULL|EMPLOYEES|3|111|3(0)|00:00:01|
----------------------------------------------------------------------------------------------------
PredicateInformation(identifiedbyoperationid):
---------------------------------------------------
3-access("T1"."DEPTNO"=10)
4-filter("T2"."DEPTNO"(+)=10)
17rowsselected
SQL>;EXPLAINPLAN
2SETSTATEMENT_ID='ex_plan2'
3FOR
4SELECTt1.dname,
5t1.loc,
6t2.empno,
7t2.ename,
8t2.job,
9t2.mgr,
10t2.hiredate,
11t2.sal
12FROMdeptt1LEFTJOINempt2ON(t1.deptno=t2.deptno)
13WHEREt1.deptno=10;
Explained
SQL>;SELECTplan_table_output
2FROMTABLE(DBMS_XPLAN.display(NULL,'ex_plan2','TYPICAL'));
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Planhashvalue:4275711305
-----------------------------------------------------------------------------------------------
|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|
-----------------------------------------------------------------------------------------------
|0|SELECTSTATEMENT||3|171|2(0)|00:00:01|
|1|NESTEDLOOPSOUTER||3|171|2(0)|00:00:01|
|2|TABLEACCESSBYINDEXROWID|DEPT|1|20|1(0)|00:00:01|
|*3|INDEXUNIQUESCAN|PK_DEPT_DEPTNO|1||0(0)|00:00:01|
|*4|TABLEACCESSCLUSTER|EMP|3|111|1(0)|00:00:01|
-----------------------------------------------------------------------------------------------
PredicateInformation(identifiedbyoperationid):
---------------------------------------------------
3-access("T1"."DEPTNO"=10)
4-filter("T2"."DEPTNO"(+)=10)
17rowsselected
SQL>
[/code]
[/code]
备注:
执行计划输出中Operation列最后一行。前者的访问路径是全表扫描,而后则是聚簇。这个区别决定了之后的差异。
执行计划输出中Rows和Bytes列相同。因为两个语句的WHERE子句相同。
但是,Cost列(CPU利用率)就有差异了。
相关文章推荐
- Oracle 11g Release 1 (11.1)——简单管理聚簇
- Oracle 11g Release 1 (11.1) 表空间——简单管理永久表空间
- Oracle 11g Release 1 (11.1) SQL_层级查询(详)
- Oracle 11g Release 1 (11.1) 单行函数——比较函数
- Oracle 11g Release 1 (11.1) SQL_层级查询(概)
- Oracle 11g Release 1 (11.1) 查询优化器的访问路径
- Oracle 11g Release 1 (11.1) 单行函数——数字函数
- Oracle 11g Release 1 (11.1) Oracle 本文索引的四种类型
- Oracle 11g Release 1 (11.1) 游标——在 PL/SQL 管理游标
- Oracle 11g Release 1 (11.1) Data Pump 技术
- Oracle 11g Release (11.1) 索引底层的数据结构
- Oracle 11g Release 1 (11.1) PL/SQL_了解 Collections 和 Records 类型
- Oracle 11g Release 1 (11.1)——自动存储管理(Automatic Storage Management,ASM)
- 查询结果集cache--oracle 11g release 2
- Oracle 11g Release 1 (11.1) Oracle 文本自定义 CONTEXT 索引的“偏好”
- Oracle 11g Release 1 (11.1) 单行函数——NULL 相关函数
- Oracle 11g Release 1 (11.1) PL/SQL_理解 Record 类型
- Oracle 11g Release 1 (11.1) Data Pump 导入模式
- Oracle 11g 学习二:scott表结构、简单查询、限定查询和数据排序
- Oracle 11g Release 1 (11.1) 表空间——创建和扩展永久表空间