連線條件字元型別不一樣。

wei-xh發表於2011-01-11

drop table wxh_tbd1;
Table dropped.

rop table wxh_tbd2;

Table dropped.

create table wxh_tbd1 (id number ,name varchar2(2000));

Table created.

create table wxh_tbd2 (id varchar2(1000) ,name varchar2(2000));

Table created.

insert into wxh_tbd1 select object_id,object_name from dba_objects;

18667 rows created.

commit;

Commit complete.

insert into wxh_tbd2 select object_id,object_name from dba_objects;

18667 rows created.

commit;

Commit complete.

create index t2 on wxh_tbd1(id);

Index created.

create index t3 on wxh_tbd2(id);

Index created.

create index t1 on wxh_tbd1(name);

Index created.

select * from wxh_tbd1 a ,wxh_tbd2 b where a.id=b.id and a.name='1';

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 422716281

-----------------------------------------------------------------------------------------
| Id  | Operation                    | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |          |     1 |  2519 |    26   (4)| 00:00:01 |
|   1 |  NESTED LOOPS                |          |       |       |            |          |
|   2 |   NESTED LOOPS               |          |     1 |  2519 |    26   (4)| 00:00:01 |
|   3 |    TABLE ACCESS FULL         | WXH_TBD2 | 19022 |    27M|    25   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN          | T1       |     1 |       |     0   (0)| 00:00:01 |
|*  5 |   TABLE ACCESS BY INDEX ROWID| WXH_TBD1 |     1 |  1015 |     0   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("A"."NAME"='1')
   5 - filter("A"."ID"=TO_NUMBER("B"."ID"))


select * from wxh_tbd1 a ,wxh_tbd2 b where to_char(a.id)=b.id and a.name='1';

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 186266070

------------------------------------------------------------------------------------------
| Id  | Operation                     | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |          |     1 |  2519 |     3   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                 |          |       |       |            |          |
|   2 |   NESTED LOOPS                |          |     1 |  2519 |     3   (0)| 00:00:01 |
|   3 |    TABLE ACCESS BY INDEX ROWID| WXH_TBD1 |     1 |  1015 |     1   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | T1       |     1 |       |     1   (0)| 00:00:01 |
|*  5 |    INDEX RANGE SCAN           | T3       |     1 |       |     1   (0)| 00:00:01 |
|   6 |   TABLE ACCESS BY INDEX ROWID | WXH_TBD2 |     1 |  1504 |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("A"."NAME"='1')
   5 - access("B"."ID"=TO_CHAR("A"."ID"))

ORACLE為什麼不去對A表的id透過to_char轉換,而總是會在varchar2的型別欄位上增加to_number函式。
數字轉char總是可以成功的,而char轉number是可能報錯的。而oracle總是傾向與後者。不知道為什麼。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22034023/viewspace-683598/,如需轉載,請註明出處,否則將追究法律責任。

相關文章