Oracle树形查询功能,也就是connect by语句使用起来非常方便。树形结构是很常见的,比如组织机构树,产品目录树等。我们这里不讲述connect by 如何使用,只是提出在使用树形查询时需要考虑的一个有关于性能方面的问题。 在这个例子中所使用的表,是一个真实的生产系统中的表,BSS_ORG: SQL> desc bss_org 名称 是否为空? 类型 ---------------------------- -------- -------------- BSS_ORG_ID NOT NULL NUMBER(9) NAME NOT NULL VARCHAR2(64) BSS_PARENT_ORG_ID NUMBER(9) BSS_ORG_LEVEL_ID NOT NULL NUMBER(3) STATE NOT NULL VARCHAR2(3) STATE_DATE DATE BSS_ORG_CODE VARCHAR2(15) Table Number Empty Average Chain Average Global Name of Rows Blocks Blocks Space Count Row Len Stats --------------- -------------- -------- ------------ ------- -------- ------- ------ BSS_ORG 5,739 52 0 0 0 53 YES Column Column Distinct Number Name Details Values Density Buckets ------------------------- ------------------------ ------------ --------- ------- -- BSS_ORG_ID NUMBER(9,0) NOT NULL 5,739 .000174 1 NAME VARCHAR2(64) NOT NULL 5,034 .000361 200 BSS_PARENT_ORG_ID NUMBER(9,0) 905 .002189 200 BSS_ORG_LEVEL_ID NUMBER(3,0) NOT NULL 6 .000087 6 STATE VARCHAR2(3) NOT NULL 2 .000087 2 STATE_DATE DATE 1,624 .001434 200 BSS_ORG_CODE VARCHAR2(15) 5,639 .000179 200 Table Number Empty Average Chain Average Global Name of Rows Blocks Blocks Space Count Row Len Stats --------------- -------------- -------- ------------ ------- -------- ------- ------ BSS_ORG 5,739 52 0 0 0 53 YES Column Column Distinct Number Name Details Values Density Buckets ------------------------- ------------------------ ------------ --------- ------- -- BSS_ORG_ID NUMBER(9,0) NOT NULL 5,739 .000174 1 NAME VARCHAR2(64) NOT NULL 5,034 .000361 200 BSS_PARENT_ORG_ID NUMBER(9,0) 905 .002189 200 BSS_ORG_LEVEL_ID NUMBER(3,0) NOT NULL 6 .000087 6 STATE VARCHAR2(3) NOT NULL 2 .000087 2 STATE_DATE DATE 1,624 .001434 200 BSS_ORG_CODE VARCHAR2(15) 5,639 .000179 200 Table Number Empty Average Chain Average Global Name of Rows Blocks Blocks Space Count Row Len Stats --------------- -------------- -------- ------------ ------- -------- ------- ------ BSS_ORG 5,739 52 0 0 0 53 YES Column Column Distinct Number Name Details Values Density Buckets ------------------------- ------------------------ ------------ --------- ------- -- BSS_ORG_ID NUMBER(9,0) NOT NULL 5,739 .000174 1 NAME VARCHAR2(64) NOT NULL 5,034 .000361 200 BSS_PARENT_ORG_ID NUMBER(9,0) 905 .002189 200 BSS_ORG_LEVEL_ID NUMBER(3,0) NOT NULL 6 .000087 6 STATE VARCHAR2(3) NOT NULL 2 .000087 2 STATE_DATE DATE 1,624 .001434 200 BSS_ORG_CODE VARCHAR2(15) 5,639 .000179 200 Table Number Empty Average Chain Average Global Name of Rows Blocks Blocks Space Count Row Len Stats --------------- -------------- -------- ------------ ------- -------- ------- ------ BSS_ORG 5,739 52 0 0 0 53 YES Column Column Distinct Number Name Details Values Density Buckets ------------------------- ------------------------ ------------ --------- ------- -- BSS_ORG_ID NUMBER(9,0) NOT NULL 5,739 .000174 1 NAME VARCHAR2(64) NOT NULL 5,034 .000361 200 BSS_PARENT_ORG_ID NUMBER(9,0) 905 .002189 200 BSS_ORG_LEVEL_ID NUMBER(3,0) NOT NULL 6 .000087 6 STATE VARCHAR2(3) NOT NULL 2 .000087 2 STATE_DATE DATE 1,624 .001434 200 BSS_ORG_CODE VARCHAR2(15) 5,639 .000179 200 Table Number Empty Average Chain Average Global Name of Rows Blocks Blocks Space Count Row Len Stats --------------- -------------- -------- ------------ ------- -------- ------- ------ BSS_ORG 5,739 52 0 0 0 53 YES Column Column Distinct Number Name Details Values Density Buckets ------------------------- ------------------------ ------------ --------- ------- -- BSS_ORG_ID NUMBER(9,0) NOT NULL 5,739 .000174 1 NAME VARCHAR2(64) NOT NULL 5,034 .000361 200 BSS_PARENT_ORG_ID NUMBER(9,0) 905 .002189 200 BSS_ORG_LEVEL_ID NUMBER(3,0) NOT NULL 6 .000087 6 STATE VARCHAR2(3) NOT NULL 2 .000087 2 STATE_DATE DATE 1,624 .001434 200 BSS_ORG_CODE VARCHAR2(15) 5,639 .000179 200 Table Number Empty Average Chain Average Global Name of Rows Blocks Blocks Space Count Row Len Stats --------------- -------------- -------- ------------ ------- -------- ------- ------ BSS_ORG 5,739 52 0 0 0 53 YES Column Column Distinct Number Name Details Values Density Buckets ------------------------- ------------------------ ------------ --------- ------- -- BSS_ORG_ID NUMBER(9,0) NOT NULL 5,739 .000174 1 NAME VARCHAR2(64) NOT NULL 5,034 .000361 200 BSS_PARENT_ORG_ID NUMBER(9,0) 905 .002189 200 BSS_ORG_LEVEL_ID NUMBER(3,0) NOT NULL 6 .000087 6 STATE VARCHAR2(3) NOT NULL 2 .000087 2 STATE_DATE DATE 1,624 .001434 200 BSS_ORG_CODE VARCHAR2(15) 5,639 .000179 200 这2条SQL返回的结果行数,与前面的2条SQL一样,分别是5739和4,但是从执行计划上看,Oracle优化器评估出来的行数都是6。 (责任编辑:admin) |