您的位置:首页 > 数据库 > Oracle

Oracle 11g Release 1 (11.1)——聚簇和非聚簇的简单查询比较

2012-08-25 23:14 681 查看

本文内容

创建非聚簇的相关表
创建聚簇
简单查询比较
本文简单比较建立聚簇后,对查询的影响。虽然就几条数据,但多少也能说明点问题。有机会的话,再试下大数据量的比较。

创建非聚簇的相关模式对象

创建EMPLOYEESDEPTMENTS表。

[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、聚簇的表empdept,以及聚簇索引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]

 

向非聚簇的表和聚簇的表插入数据

下载并执行.sql文件,插入数据。

 

简单查询比较

示例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利用率)就有差异了。



 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐
章节导航