侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

远程数据库的表超过20个索引的影响详细解析

数据库  /  管理员 发布于 6年前   176

昨天同事参加了一个研讨会,有提到一个案例。一个通过dblink查询远端数据库,原来查询很快,但是远端数据库增加了一个索引之后,查询一下子变慢了。

经过分析,发现那个通过dblink的查询语句,查询远端数据库的时候,是走索引的,但是远端数据库添加索引之后,如果索引的个数超过20个,就会忽略第一个建立的索引,如果查询语句恰好用到了第一个建立的索引,被忽略之后,只能走Full Table Scan了。

听了这个案例,我查了一下,在oracle官方文档中,关于Managing a Distributed Database有一段话:

Several performance restrictions relate to access of remote objects:

Remote views do not have statistical data.
Queries on partitioned tables may not be optimized.
No more than 20 indexes are considered for a remote table.
No more than 20 columns are used for a composite index.

说到,如果远程数据库使用超过20个索引,这些索引将不被考虑。这段话,在oracle 9i起的文档中就已经存在,一直到12.2还有。

那么,超过20个索引,是新的索引被忽略了?还是老索引被忽略了?如何让被忽略的索引让oracle意识到?我们来测试一下。
(本文基于12.1.0.2的远程库和12.2.0.1的本地库进行测试,如果对测试过程没兴趣的,可以直接拉到文末看“综上”部分)

(一)初始化测试表:

--创建远程表:DROP TABLE t_remote; CREATE TABLE t_remote (col01 NUMBER,col02 NUMBER,col03 VARCHAR2(50),col04 NUMBER,col05 NUMBER,col06 VARCHAR2(50),col07 NUMBER,col08 NUMBER,col09 VARCHAR2(50),col10 NUMBER,col11 NUMBER,col12 VARCHAR2(50),col13 NUMBER,col14 NUMBER,col15 VARCHAR2(50),col16 NUMBER,col17 NUMBER,col18 VARCHAR2(50),col19 NUMBER,col20 NUMBER,col21 VARCHAR2(50),col22 NUMBER,col23 NUMBER,col24 VARCHAR2(50),col25 NUMBER,col26 NUMBER,col27 VARCHAR2(50));alter table t_remote modify (col01 not null);INSERT INTO t_remoteSELECTrownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*')FROM dualCONNECT BY level <= 10000;commit; create unique index t_remote_i01_pk on t_remote (col01);alter table t_remote add (constraint t_remote_i01_pk primary key (col01) using index t_remote_i01_pk);create index t_remote_i02 on t_remote (col02);create index t_remote_i03 on t_remote (col03);create index t_remote_i04 on t_remote (col04);create index t_remote_i05 on t_remote (col05);create index t_remote_i06 on t_remote (col06);create index t_remote_i07 on t_remote (col07);create index t_remote_i08 on t_remote (col08);create index t_remote_i09 on t_remote (col09);create index t_remote_i10 on t_remote (col10);create index t_remote_i11 on t_remote (col11);create index t_remote_i12 on t_remote (col12);create index t_remote_i13 on t_remote (col13);create index t_remote_i14 on t_remote (col14);create index t_remote_i15 on t_remote (col15);create index t_remote_i16 on t_remote (col16);create index t_remote_i17 on t_remote (col17);create index t_remote_i18 on t_remote (col18);create index t_remote_i19 on t_remote (col19);create index t_remote_i20 on t_remote (col20); exec dbms_stats.gather_table_stats(user,'T_REMOTE');
--创建本地表:drop table t_local; CREATE TABLE t_local (col01 NUMBER,col02 NUMBER,col03 VARCHAR2(50),col04 NUMBER,col05 NUMBER,col06 VARCHAR2(50)); INSERT INTO t_localSELECTrownum, rownum, rpad('*',50,'*'),rownum, rownum, rpad('*',50,'*')FROM dualCONNECT BY level <= 50; COMMIT; create index t_local_i01 on t_local (col01);create index t_local_i02 on t_local (col02);create index t_local_i03 on t_local (col03);create index t_local_i04 on t_local (col04);create index t_local_i05 on t_local (col05);create index t_local_i06 on t_local (col06); exec dbms_stats.gather_table_stats(user,'t_local');  create database link dblink_remote CONNECT TO test IDENTIFIED BY test USING 'ora121';  SQL> select host_name from v$instance@dblink_remote; HOST_NAME----------------------------------------------------------------testdb2 SQL> select host_name from v$instance; HOST_NAME----------------------------------------------------------------testdb10 SQL>

可以看到,远程表有27个字段,目前还只是在前20个字段建立了索引,且第一个字段是主键。本地表,有6个字段,6个字段都建索引。

(二)第一轮测试,远程表上有20个索引。

测试场景1:

在远程表20索引的情况下,本地表和远程表关联,用本地表的第一个字段关联远程表的第一个字段:

select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25from t_local l, t_remote@dblink_remote rwhere l.col01=r.col01;select * from table( dbms_xplan.display_cursor(null, null, 'typical LAST') );PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 04schqc3d9rgm, child number 0-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col01=r.col01Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 53 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 53 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  1 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL01","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL01"  (accessing 'DBLINK_REMOTE' ) 23 rows selected.SQL> -- 我们这里注意一下,WHERE :1="COL01"的存在,正是因为这个条件,所以在远程是走了主键而不是全表扫。我们把这个语句带入到远程执行。远程:SQL> explain plan for 2 SELECT "COL01","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL01";PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 829680338-----------------------------------------------------------------------------------------------| Id | Operation     | Name   | Rows | Bytes | Cost (%CPU)| Time  |-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT   |     |  1 | 63 |  2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID| T_REMOTE  |  1 | 63 |  2 (0)| 00:00:01 ||* 2 | INDEX UNIQUE SCAN   | T_REMOTE_I01_PK |  1 |  |  1 (0)| 00:00:01 |-----------------------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("COL01"=TO_NUMBER(:1))14 rows selected.

我们可以看到,对于远程表的执行计划,这是走主键的。

测试场景2:

在远程表20索引的情况下,本地表和远程表关联,用本地表的第一个字段关联远程表的第20个字段:

select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25from t_local l, t_remote@dblink_remote rwhere l.col01=r.col20;select * from table( dbms_xplan.display_cursor(null, null, 'typical LAST') );PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 5rwtbwcnv0tsm, child number 0-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col01=r.col20Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20"  (accessing 'DBLINK_REMOTE' ) 23 rows selected.SQL> 远程:PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 3993494813----------------------------------------------------------------------------------------------------| Id | Operation       | Name   | Rows | Bytes | Cost (%CPU)| Time  |----------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT     |    |  1 | 63 |  2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T_REMOTE  |  1 | 63 |  2 (0)| 00:00:01 ||* 2 | INDEX RANGE SCAN     | T_REMOTE_I20 |  1 |  |  1 (0)| 00:00:01 |----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("COL20"=TO_NUMBER(:1))14 rows selected.SQL>

我们可以看到,对于远程表的执行计划,这是走索引范围扫描的。

测试场景3:

在远程表20索引的情况下,本地表和远程表关联,用本地表的第2个字段关联远程表的第2个字段:

select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25from t_local l, t_remote@dblink_remote rwhere l.col02=r.col02;select * from table( dbms_xplan.display_cursor(null, null, 'typical LAST') );PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 81ctrx5huhfvq, child number 0-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col02=r.col02Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL02","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL02"  (accessing 'DBLINK_REMOTE' ) 23 rows selected.SQL> 远程:SQL> explain plan for  2 SELECT "COL02","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL02";Explained.SQL> select * from table(dbms_xplan.display());PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 2505594687----------------------------------------------------------------------------------------------------| Id | Operation       | Name   | Rows | Bytes | Cost (%CPU)| Time  |----------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT     |    |  1 | 63 |  2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T_REMOTE  |  1 | 63 |  2 (0)| 00:00:01 ||* 2 | INDEX RANGE SCAN     | T_REMOTE_I02 |  1 |  |  1 (0)| 00:00:01 |----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("COL02"=TO_NUMBER(:1))14 rows selected.SQL>

我们可以看到,对于远程表的执行计划,这是走索引范围扫描的。

测试场景4:

在远程表20索引的情况下,本地表和远程表关联,用本地表的第2个字段关联远程表的第20个字段:

select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25from t_local l, t_remote@dblink_remote rwhere l.col02=r.col20;select * from table( dbms_xplan.display_cursor(null, null, 'typical LAST') );PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 407pxjh9mgbry, child number 0-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col02=r.col20Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20"  (accessing 'DBLINK_REMOTE' )23 rows selected.SQL> 远程:SQL> explain plan for 2 SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20";Explained.SQL> select * from table(dbms_xplan.display());PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 3993494813----------------------------------------------------------------------------------------------------| Id | Operation       | Name   | Rows | Bytes | Cost (%CPU)| Time  |----------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT     |    |  1 | 63 |  2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T_REMOTE  |  1 | 63 |  2 (0)| 00:00:01 ||* 2 | INDEX RANGE SCAN     | T_REMOTE_I20 |  1 |  |  1 (0)| 00:00:01 |----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("COL20"=TO_NUMBER(:1))14 rows selected.SQL>

我们可以看到,对于远程表的执行计划,这是走索引范围扫描的。

(三)建立第21个索引:

create index t_remote_i21 on t_remote (col21);exec dbms_stats.gather_table_stats(user,'T_REMOTE');

(四)远程表上现在有21个索引,重复上面4个测试:

测试场景1:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 04schqc3d9rgm, child number 1-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col01=r.col01Plan hash value: 830255788-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 156 (100)|   |  |  ||* 1 | HASH JOIN   |   | 50 | 6300 | 156 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE | 10000 | 644K| 153 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 1 - access("L"."COL01"="R"."COL01")Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL01","COL25","COL26","COL27" FROM "T_REMOTE" "R" (accessing  'DBLINK_REMOTE' ) 28 rows selected.SQL>--我们看到,这里已经没有了之前的 WHERE :1="COL01",即使不带入到远程看执行计划,我们也可以猜到它是全表扫。远程:SQL> explain plan for 2 SELECT "COL01","COL25","COL26","COL27" FROM "T_REMOTE" "R";Explained.SQL> select * from table(dbms_xplan.display());PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 4187688566------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  |------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   | 10000 | 615K| 238 (0)| 00:00:01 || 1 | TABLE ACCESS FULL| T_REMOTE | 10000 | 615K| 238 (0)| 00:00:01 |------------------------------------------------------------------------------8 rows selected.SQL>

我们可以看到,对于远程表的执行计划,如果关联条件是远程表的第一个字段,第一个字段上的索引是被忽略的,执行计划是选择全表扫描的。

测试场景2:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 5rwtbwcnv0tsm, child number 1-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col01=r.col20Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20"  (accessing 'DBLINK_REMOTE' ) 23 rows selected.SQL> 远程:SQL> explain plan for 2 SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20";Explained.SQL> select * from table(dbms_xplan.display());PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 3993494813----------------------------------------------------------------------------------------------------| Id | Operation       | Name   | Rows | Bytes | Cost (%CPU)| Time  |----------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT     |    |  1 | 63 |  2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T_REMOTE  |  1 | 63 |  2 (0)| 00:00:01 ||* 2 | INDEX RANGE SCAN     | T_REMOTE_I20 |  1 |  |  1 (0)| 00:00:01 |----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("COL20"=TO_NUMBER(:1))14 rows selected.SQL>

我们可以看到,对于远程表的执行计划,如果关联条件是远程表的第20个字段,这第20个字段上的索引是没有被忽略的,执行计划是走索引。

测试场景3:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 81ctrx5huhfvq, child number 1-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col02=r.col02Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL02","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL02"  (accessing 'DBLINK_REMOTE' ) 23 rows selected.SQL> 远程:SQL> explain plan for 2 SELECT "COL02","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL02";Explained.SQL> select * from table(dbms_xplan.display());PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 2505594687----------------------------------------------------------------------------------------------------| Id | Operation       | Name   | Rows | Bytes | Cost (%CPU)| Time  |----------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT     |    |  1 | 63 |  2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T_REMOTE  |  1 | 63 |  2 (0)| 00:00:01 ||* 2 | INDEX RANGE SCAN     | T_REMOTE_I02 |  1 |  |  1 (0)| 00:00:01 |----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("COL02"=TO_NUMBER(:1))14 rows selected.SQL>

我们可以看到,对于远程表的执行计划,如果关联条件是远程表的第2个字段,这第2个字段上的索引是没有被忽略的,执行计划是走索引。

测试场景4:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 407pxjh9mgbry, child number 1-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col02=r.col20Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20"  (accessing 'DBLINK_REMOTE' ) 23 rows selected.SQL> 远程:SQL> explain plan for 2 SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20";Explained.SQL> select * from table(dbms_xplan.display());PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------Plan hash value: 3993494813----------------------------------------------------------------------------------------------------| Id | Operation       | Name   | Rows | Bytes | Cost (%CPU)| Time  |----------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT     |    |  1 | 63 |  2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T_REMOTE  |  1 | 63 |  2 (0)| 00:00:01 ||* 2 | INDEX RANGE SCAN     | T_REMOTE_I20 |  1 |  |  1 (0)| 00:00:01 |----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 - access("COL20"=TO_NUMBER(:1))14 rows selected.SQL>

我们可以看到,对于远程表的执行计划,如果关联条件是远程表的第20个字段,这第20个字段上的索引是没有被忽略的,执行计划是走索引。

我们目前可以总结到,当远程表第21个索引建立的时候,通过dblink关联本地表和远程表,如果关联条件是远程表的第1个建立的索引的字段,那么这个索引将被忽略,从而走全表扫描。如果关联条件是远程表的第2个建立索引的字段,则不受影响。

似乎是有效索引的窗口是20个,当新建第21个,那么第1个就被无视了。

(五)建立第22个索引,我们在来看看上述猜测是否符合。

create index t_remote_i22 on t_remote (col22);exec dbms_stats.gather_table_stats(user,'T_REMOTE');

(六),目前远程表有22个索引,重复上面4个测试:

测试场景1:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 04schqc3d9rgm, child number 2-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col01=r.col01Plan hash value: 830255788-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 156 (100)|   |  |  ||* 1 | HASH JOIN   |   | 50 | 6300 | 156 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE | 10000 | 644K| 153 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 1 - access("L"."COL01"="R"."COL01")Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL01","COL25","COL26","COL27" FROM "T_REMOTE" "R" (accessing  'DBLINK_REMOTE' ) 28 rows selected.SQL>

测试场景2:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 5rwtbwcnv0tsm, child number 2-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col01=r.col20Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20"  (accessing 'DBLINK_REMOTE' )23 rows selected.SQL>

测试场景3:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 81ctrx5huhfvq, child number 2-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col02=r.col02Plan hash value: 830255788-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 156 (100)|   |  |  ||* 1 | HASH JOIN   |   | 50 | 6300 | 156 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE | 10000 | 644K| 153 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 1 - access("L"."COL02"="R"."COL02")Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL02","COL25","COL26","COL27" FROM "T_REMOTE" "R" (accessing  'DBLINK_REMOTE' ) 28 rows selected.SQL>

测试场景4:

PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 407pxjh9mgbry, child number 2-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col02=r.col20Plan hash value: 631452043-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  |  | 103 (100)|   |  |  || 1 | NESTED LOOPS  |   | 50 | 6300 | 103 (0)| 00:00:01 |  |  || 2 | TABLE ACCESS FULL| T_LOCAL | 50 | 3000 |  3 (0)| 00:00:01 |  |  || 3 | REMOTE   | T_REMOTE |  1 | 66 |  2 (0)| 00:00:01 | DBLIN~ | R->S |-----------------------------------------------------------------------------------------------Remote SQL Information (identified by operation id):---------------------------------------------------- 3 - SELECT "COL20","COL25","COL26","COL27" FROM "T_REMOTE" "R" WHERE :1="COL20"  (accessing 'DBLINK_REMOTE' )23 rows selected.SQL>

上述的测试,其实是可以验证我们的猜测的。oracle对于通过dblink关联访问远程表,只是会意识到最近创建的20个索引的字段。这个意识到索引的窗口是20个,一旦建立了一个新索引,那么最旧的一个索引会被无视。

(七)我们尝试rebuild索引,看看有没有效果:

rebuild第2个索引

alter index t_remote_i02 rebuild;exec dbms_stats.gather_table_stats(user,'T_REMOTE');

(八)在第2个索引rebuild之后,重复上面4个测试:

--测试场景1:PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------------SQL_ID 04schqc3d9rgm, child number 0-------------------------------------select l.col06,l.col05,l.col04,r.col27, r.col26,r.col25 from t_local l,t_remote@dblink_remote r where l.col01=r.col01Plan hash value: 830255788-----------------------------------------------------------------------------------------------| Id | Operation   | Name  | Rows | Bytes | Cost (%CPU)| Time  | Inst |IN-OUT|-----------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |   |  | 

  • 上一条:
    SunlightDB 2017新型区块链数据库
    下一条:
    浅谈数据库事务四大特性
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客