ORACLE MYSQL中join 欄位型別不同索引失效的情況
關於JOIN使用不同型別的欄位型別,資料庫可能進行隱士轉換,MYSQL ORACLE都是如此,
下面使用一個列子來看看,指令碼如下:
mysql:
drop table testjoin1;
drop table testjoin2;
create table testjoin1(id int, name varchar(20));
create table testjoin2(id varchar(20),name varchar(20),key(id);
oracle:
drop table testjoin1;
drop table testjoin2;
create table testjoin1(id int,name varchar2(20));
create table testjoin2(id varchar(20),name varchar2(20));
create index test_id_2 on testjoin2(id);
insert into testjoin1 values(1,'gaopeng');
insert into testjoin1 values(2,'gaopeng');
insert into testjoin1 values(3,'gaopeng');
insert into testjoin1 values(4,'gaopeng');
insert into testjoin1 values(5,'gaopeng');
insert into testjoin2 values('1','gaopeng');
ORACLE中的隱士轉換,
SQL> select /*+ use_nl(a b) ordered */ * from testjoin1 a join testjoin2 b on a.id=b.id ;
ID NAME ID NAME
---------- -------------------- -------------------- --------------------
1 gaopeng 1 gaopeng
Execution Plan
----------------------------------------------------------
Plan hash value: 2498279186
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 49 | 5 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 1 | 49 | 5 (0)| 00:00:01 |
| 2 | TABLE ACCESS FULL| TESTJOIN1 | 5 | 125 | 2 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| TESTJOIN2 | 1 | 24 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("A"."ID"=TO_NUMBER("B"."ID")) --雖然TESTJOIN2是被驅動表由於隱士轉換索引用不到
mysql> explain select * from testjoin1 a Straight_JOIN testjoin2 b on a.id=b.id ;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | a | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | NULL |
| 1 | SIMPLE | b | NULL | ALL | id | NULL | NULL | NULL | 1 | 100.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
2 rows in set, 3 warnings (0.00 sec)
Warning (Code 1739): Cannot use ref access on index 'id' due to type or collation conversion on field 'id' --雖然TESTJOIN2是被驅動表由於隱士轉換索引用不到 possible_keys可以看出
Warning (Code 1739): Cannot use range access on index 'id' due to type or collation conversion on field 'id'
Note (Code 1003): /* select#1 */ select `test`.`a`.`id` AS `id`,`test`.`a`.`name` AS `name`,`test`.`b`.`id` AS `id`,`test`.`b`.`name` AS `name` from `test`.`testjoin1` `a` straight_join `test`.`testjoin2` `b` where (`test`.`a`.`id` = `test`.`b`.`id`)
MYSQL手冊原文:
To retrieve rows from other tables when performing joins. MySQL can use indexes on columns more
efficiently if they are declared as the same type and size. In this context, VARCHARand CHARare
considered the same if they are declared as the same size. For example, VARCHAR(10)and CHAR(10)
are the same size, but VARCHAR(10)and CHAR(15)are not.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2131608/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql索引失效的情況MySql索引
- Mysql 會導致索引失效的情況MySql索引
- MYSQL索引失效的各種情況小結MySql索引
- 簡單介紹MySQL索引失效的幾種情況MySql索引
- oracle的欄位型別Oracle型別
- MySQL中TEXT與BLOB欄位型別的區別MySql型別
- MySQL欄位型別最全解析MySql型別
- MySQL欄位型別小記MySql型別
- oracle 修改欄位型別的方法Oracle型別
- Laravel 對於 Mysql 欄位string型別查詢,當使用數字對這個欄位進行查詢,PHP弱型別語言導致索引失效LaravelMySql型別PHP索引
- SQL優化--強制走索引失效的情況SQL優化索引
- mysql表操作(alter)/mysql欄位型別MySql型別
- MongoDB中的欄位型別IdMongoDB型別
- MySQL欄位新增註釋,但不改變欄位的型別MySql型別
- 在不清楚資料表欄位資料分佈的情況下,應該建立什麼型別的索引?型別索引
- [轉]MySQL 欄位型別參考MySql型別
- 關於mysql中欄位定義的型別int、tinyint區別MySql型別
- Oracle -- left join 什麼情況可以直接改成joinOracle
- mysql索引不會命中的情況MySql索引
- ORACLE索引被抑制情況Oracle索引
- Oracle隱式型別轉換導致索引失效Oracle型別索引
- java查詢資料庫,int型欄位為null的情況Java資料庫Null
- MySQL哪些情況需要新增索引?MySql索引
- oracle中建立insert select from 語句實現兩個表中某一個欄位相同統計其他不同欄位的情況Oracle
- oracle order by索引是否使用的情況Oracle索引
- Mysql兩種情況下更新欄位中部分資料的方法MySql
- Oracle 12.2中的索引統計被呼叫情況Oracle索引
- PHP 操作 mysql blob 資料型別的欄位PHPMySql資料型別
- 查詢mysql某張表中的所有資料(欄位)型別MySql型別
- Oracle 修改欄位型別和長度Oracle型別
- MySQL索引的型別MySql索引型別
- 批量修改欄位長度,考慮主鍵外來鍵索引的情況【轉】索引
- Oracle資料庫聯接(inner join ,outer join)和NOT IN的特殊情況Oracle資料庫
- MYSQL 字符集不同引起的join無法走索引MySql索引
- MySQL中欄位型別與合理的選擇欄位型別;int(11)最大長度是多少?varchar最大長度是多少?MySql型別
- 細說SQL SERVER中欄位型別SQLServer型別
- 保留兩位小數:資料庫欄位型別NUMBER,Java欄位型別Double型別資料庫型別Java
- 修改表的欄位型別型別