SQL> create table t1 as select * from user_tables;
表已创建。
SQL> create table t2 as select * from user_indexes;
表已创建。
SQL> select count(*) from t1;
COUNT(*)
----------
704
SQL> select count(*) from t2;
COUNT(*)
----------
812
SQL> set autot on exp
-----------------------------------T1 T2均无索引
SQL> select count(*) from t1,t2 where t1.table_name = t2.table_name;
COUNT(*)
----------
789
Note
-----
- dynamic sampling used for this statement
--------------这里走的是hash join 方式
---------------------------------对T1建立索引
SQL> create index index_tn_t1 on t1(table_name);
索引已创建。
SQL> select count(*) from t1,t2 where t1.table_name = t2.table_name;
COUNT(*)
----------
789
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T1"."TABLE_NAME"="T2"."TABLE_NAME"
Note
-----
- dynamic sampling used for this statement
--------------------------------对T2建立索引
SQL> create index index_tn_t2 on t2(table_name);
索引已创建。
SQL> select count(*) from t1,t2 where t1.table_name = t2.table_name;
COUNT(*)
----------
789
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T1"."TABLE_NAME"="T2"."TABLE_NAME"
Note
-----
- dynamic sampling used for this statement
------------------------------------删除T1的索引,保留T2的索引
SQL> drop index index_tn_t1;
索引已删除。
SQL> select count(*) from t1,t2 where t1.table_name = t2.table_name;
COUNT(*)
----------
789
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("T1"."TABLE_NAME"="T2"."TABLE_NAME"
Note
-----
- dynamic sampling used for this statement
-------------------------------------对T1建立主键
SQL> alter table t1 add constraint pk_t1 primary key (table_name);
表已更改。
SQL> select count(*) from t1,t2 where t1.table_name = t2.table_name;
COUNT(*)
----------
789
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("T1"."TABLE_NAME"="T2"."TABLE_NAME"
Note
-----
- dynamic sampling used for this statement
---------------------------------------------------------问题如下:
无论T1、T2是否有索引,连接查询时都走hash join
如果对T1加了主键,就会走 nested loop join
为何加了主键就会走nested loop join呢?而没加时都走hash join呢?[