|
什么索引都不建,也没慢
- SQL> create table range_a(id number);
- 已用时间: 00: 00: 00.01
- SQL> create table range_b(start_id number,end_id integer,type number);
- 已用时间: 00: 00: 00.01
- SQL> insert into range_a
- 2 select round(dbms_random.value(1,25000000)) from dual connect by level<=200000;
- 已用时间: 00: 00: 01.14
- SQL> insert into range_b
- 2 select 10000*(level-1)+3000 as start_id, 10000*level as end_id , level as type
- 3 from dual connect by level<=2000
- 4 order by dbms_random.value;
- 已用时间: 00: 00: 00.01
- SQL> explain plan for SELECT ID, TYPE
- 2 FROM range_a A,range_b B
- 3 WHERE A.ID>=B.START_ID AND A.ID<=B.END_ID;
- 已用时间: 00: 00: 00.04
- SQL> select * from table(dbms_xplan.display);
- PLAN_TABLE_OUTPUT
- ------------------------------------------------------------------------------------------------------------------------
- Plan hash value: 1183490394
- ----------------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
- ----------------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 1086K| 53M| | 1183 (6)| 00:00:15 |
- | 1 | MERGE JOIN | | 1086K| 53M| | 1183 (6)| 00:00:15 |
- | 2 | SORT JOIN | | 2000 | 78000 | | 4 (25)| 00:00:01 |
- | 3 | TABLE ACCESS FULL | RANGE_B | 2000 | 78000 | | 3 (0)| 00:00:01 |
- |* 4 | FILTER | | | | | | |
- |* 5 | SORT JOIN | | 217K| 2758K| 8536K| 1119 (1)| 00:00:14 |
- | 6 | TABLE ACCESS FULL| RANGE_A | 217K| 2758K| | 85 (2)| 00:00:02 |
- ----------------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 4 - filter("A"."ID"<="B"."END_ID")
- 5 - access("A"."ID">="B"."START_ID")
- filter("A"."ID">="B"."START_ID")
- Note
- -----
- - dynamic sampling used for this statement (level=2)
- 已用时间: 00: 00: 00.04
- SQL> create table range_result (id number, type number);
- 已用时间: 00: 00: 00.00
- SQL> --原sql:
- SQL> insert into range_result
- 2 SELECT ID, TYPE
- 3 FROM range_a A,range_b B
- 4 WHERE A.ID>=B.START_ID AND A.ID<=B.END_ID;
- 已用时间: 00: 00: 47.07
- SQL> select count(*) from range_result;
- COUNT(*)
- ----------
- 112182
- 已用时间: 00: 00: 00.01
复制代码 |
|