about me:oracle ace pro,optimistic,passionate and harmonious. focus on oracle,mysql and other database programming,peformance tuning,db design, j2ee,linux/aix,architecture tech,etc
全部博文(166)
分类: oracle
2024-04-02 16:51:51
drop table t1; create table t1 as select to_char(trunc(dbms_random.value(10000000000, 20000000000 ))) phone_no, trunc(dbms_random.value(0, 30 )) ext, lpad(level,10) v1, rpad('x',100) padding from dual connect by level <= 1000000; drop table t2; create table t2 as select to_char(trunc(dbms_random.value(10000000000, 20000000000 ))) phone_no, trunc(dbms_random.value(0, 30 )) ext, lpad(level,10) v1, rpad('x',100) padding from dual connect by level <= 1000000; --创建索引 create index idx_t2 on t2(phone_no); --收集统计信息 exec dbms_stats.gather_table_stats(ownname=>user,tabname=>'t1',estimate_percent=>10,method_opt=>'for all columns size auto',no_invalidate=>false,cascade=>true,degree => 10); exec dbms_stats.gather_table_stats(ownname=>user,tabname=>'t2',estimate_percent=>10,method_opt=>'for all columns size auto',no_invalidate=>false,cascade=>true,degree => 10); 数据量: select count(*) from t1; count(*) ---------- 1000000 select count(*) from t2; count(*) ---------- 1000000 统计信息: table_name num_rows sample_size last_analyzed ------------------------------ ---------- ----------- ------------------- t1 1003550 100355 2023-08-18 17:13:11 t2 996380 99638 2023-08-18 17:13:11 |
select phone_no,ext,v1,padding from t1 where substr(t1.phone_no,1,8) in (select t2.phone_no from t2 where length(t2.phone_no)=8) or substr(t1.phone_no,1,9) in (select t2.phone_no from t2 where length(t2.phone_no)=9) or substr(t1.phone_no,1,10) in (select t2.phone_no from t2 where length(t2.phone_no)=10) or substr(t1.phone_no,1,11) in (select t2.phone_no from t2 where length(t2.phone_no)=11); |
执行计划如下所示:
95 rows selected.
|
select distinct t1.phone_no,t1.ext,t1.v1,t1.padding from t1,(select distinct phone_no from t2 where length(t2.phone_no) between 8 and 11) t22 where t1.phone_no like t22.phone_no||'%' --构造hash join关键,{banned}最佳好重复值小,不然有hash碰撞 and substr(t1.phone_no,1,8)=substr(t22.phone_no,1,8) --下面的是防止其他不满足9-11的条件过滤,这个类似原来or条件 and substr(t1.phone_no,1,length(t22.phone_no))=t22.phone_no; |
95 rows selected. elapsed: 00:00:00.88 execution plan ---------------------------------------------------------- plan hash value: 555290710 --------------------------------------------------------------------------------- | id | operation | name | rows | bytes | cost (%cpu)| time | --------------------------------------------------------------------------------- | 0 | select statement | | 1 | 139 | 6085 (3)| 00:01:14 | | 1 | hash unique | | 1 | 139 | 6085 (3)| 00:01:14 | |* 2 | hash join | | 1 | 139 | 6084 (3)| 00:01:14 | |* 3 | index fast full scan| idx_t2 | 2491 | 29892 | 882 (2)| 00:00:11 | | 4 | table access full | t1 | 1003k| 121m| 5082 (1)| 00:01:01 | --------------------------------------------------------------------------------- predicate information (identified by operation id): --------------------------------------------------- 2 - access(substr("t1"."phone_no",1,8)=substr("phone_no",1,8)) filter("t1"."phone_no" like "phone_no"||'%' and "phone_no"=substr("t1"."phone_no",1,length("phone_no"))) 3 - filter(length("t2"."phone_no")>=8 and length("t2"."phone_no")<=11) statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 21748 consistent gets 18519 physical reads 0 redo size 4993 bytes sent via sql*net to client 586 bytes received via sql*net from client 8 sql*net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 95 rows processed |