实验环境:Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
1、创建表插入数据
sql>createtabletxtx(idint,namechar(2),txchar(3),id1int,primarykey(id,name,tx)); 表已创建。 sql>insertintotxtxvalues(1,'tx',1); 已创建1行。 sql>insertintotxtxvalues(2,2); 已创建1行。 sql>insertintotxtxvalues(3,3); 已创建1行。 sql>commit;
sql>select*fromtxtx; IDNATXID1 ------------------------- 1txtx1 2txtx2 3txtx3
2、执行计划
sql>explainplanforselect*fromtxtxwhereid=1andid1=1andtx='tx'; 已解释。 sql>setlinesize200 sql>select*fromtable(DBMS_XPLAN.DISPLAY); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Planhashvalue:4191381592 -------------------------------------------------------------------------- |Id|Operation|Name|Rows|Bytes|Cost(%cpu)|Time| -------------------------------------------------------------------------- |0|SELECTSTATEMENT||1|35|3(0)|00:00:01| |*1|TABLEACCESSFULL|TXTX|1|35|3(0)|00:00:01| -------------------------------------------------------------------------- PredicateInformation(identifiedbyoperationid): --------------------------------------------------- PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1-filter("ID"=1AND"ID1"=1AND"TX"='tx') Note ----- -dynamicsamplingusedforthisstatement(level=2) 已选择17行。
通过以上执行计划,可以看出,不含前导列,进行了全表扫描,以下使用了前导列,查询速度就上来了
sql>explainplanforselect*fromtxtxwhereid=1andname='tx'andtx='tx'; 已解释。 sql>select*fromtable(DBMS_XPLAN.DISPLAY); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Planhashvalue:913771524 -------------------------------------------------------------------------------------------- |Id|Operation|Name|Rows|Bytes|Cost(%cpu)|Time| -------------------------------------------------------------------------------------------- |0|SELECTSTATEMENT||1|35|1(0)|00:00:01| |1|TABLEACCESSBYINDEXROWID|TXTX|1|35|1(0)|00:00:01| |*2|INDEXUNIQUESCAN|SYS_C0024000|1||1(0)|00:00:01| -------------------------------------------------------------------------------------------- PredicateInformation(identifiedbyoperationid): PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------- 2-access("ID"=1AND"NAME"='tx'AND"TX"='tx') 已选择14行。