【SQL優化】LIKE vs INDEX
這裡給一個常識性的,SQL中like與索引使用的原則。很熟悉的朋友可以飄過鳥~~
1.包含一百萬行記錄的測試表
sec@ora10g> select count(*) from t;
COUNT(*)
----------
1000000
2.在x列上建立索引
sec@ora10g> create index i_t on t(x);
Index created.
3.直接查詢,可以利用到索引
sec@ora10g> set autot trace exp
sec@ora10g> select * from t where x = 'SEC';
Execution Plan
----------------------------------------------------------
Plan hash value: 2928007915
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1214 | 101K| 51 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1214 | 101K| 51 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I_T | 1214 | | 5 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("X"='SEC')
4.以like 'SEC%'形式進行查詢,可以利用到索引
sec@ora10g> select * from t where x like 'SEC%';
Execution Plan
----------------------------------------------------------
Plan hash value: 2928007915
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1214 | 101K| 51 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1214 | 101K| 51 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I_T | 1214 | | 5 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("X" LIKE 'SEC%')
filter("X" LIKE 'SEC%')
5.以not like 'SEC%'形式進行查詢,無法利用到索引
sec@ora10g> select * from t where x not like 'SEC%';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000K| 82M| 2840 (2)| 00:00:35 |
|* 1 | TABLE ACCESS FULL| T | 1000K| 82M| 2840 (2)| 00:00:35 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" NOT LIKE 'SEC%')
6.以like '%SEC'形式進行查詢,無法利用到索引
sec@ora10g> select * from t where x like '%SEC';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50103 | 4207K| 2826 (1)| 00:00:34 |
|* 1 | TABLE ACCESS FULL| T | 50103 | 4207K| 2826 (1)| 00:00:34 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" LIKE '%SEC')
7.同樣道理,以like '%SEC%'形式進行查詢,同樣也無法利用到索引
sec@ora10g> select * from t where x like '%SEC%';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50103 | 4207K| 2826 (1)| 00:00:34 |
|* 1 | TABLE ACCESS FULL| T | 50103 | 4207K| 2826 (1)| 00:00:34 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" LIKE '%SEC%')
8.小結
LIKE '%***'、LIKE '%***%'及NOT LIKE語句無法使用到索引;
儘量不使用LIKE語句,不可避免時,可使用like ' ***%'。
-- The End --
1.包含一百萬行記錄的測試表
sec@ora10g> select count(*) from t;
COUNT(*)
----------
1000000
2.在x列上建立索引
sec@ora10g> create index i_t on t(x);
Index created.
3.直接查詢,可以利用到索引
sec@ora10g> set autot trace exp
sec@ora10g> select * from t where x = 'SEC';
Execution Plan
----------------------------------------------------------
Plan hash value: 2928007915
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1214 | 101K| 51 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1214 | 101K| 51 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I_T | 1214 | | 5 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("X"='SEC')
4.以like 'SEC%'形式進行查詢,可以利用到索引
sec@ora10g> select * from t where x like 'SEC%';
Execution Plan
----------------------------------------------------------
Plan hash value: 2928007915
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1214 | 101K| 51 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1214 | 101K| 51 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | I_T | 1214 | | 5 (0)| 00:00:01 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("X" LIKE 'SEC%')
filter("X" LIKE 'SEC%')
5.以not like 'SEC%'形式進行查詢,無法利用到索引
sec@ora10g> select * from t where x not like 'SEC%';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000K| 82M| 2840 (2)| 00:00:35 |
|* 1 | TABLE ACCESS FULL| T | 1000K| 82M| 2840 (2)| 00:00:35 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" NOT LIKE 'SEC%')
6.以like '%SEC'形式進行查詢,無法利用到索引
sec@ora10g> select * from t where x like '%SEC';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50103 | 4207K| 2826 (1)| 00:00:34 |
|* 1 | TABLE ACCESS FULL| T | 50103 | 4207K| 2826 (1)| 00:00:34 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" LIKE '%SEC')
7.同樣道理,以like '%SEC%'形式進行查詢,同樣也無法利用到索引
sec@ora10g> select * from t where x like '%SEC%';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50103 | 4207K| 2826 (1)| 00:00:34 |
|* 1 | TABLE ACCESS FULL| T | 50103 | 4207K| 2826 (1)| 00:00:34 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X" LIKE '%SEC%')
8.小結
LIKE '%***'、LIKE '%***%'及NOT LIKE語句無法使用到索引;
儘量不使用LIKE語句,不可避免時,可使用like ' ***%'。
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-616907/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle sql like優化(一)OracleSQL優化
- SQL使用模糊查詢like的優化SQL優化
- Like 的優化 (zt)優化
- 高效的SQL(Index unique scan最優化)SQLIndex優化
- 高效的SQL(index range scan優化排序)SQLIndex優化排序
- sql優化一例(index_desc)SQL優化Index
- Oracle中的like優化Oracle優化
- MySQL SQL優化 - 覆蓋索引(covering index)MySql優化索引Index
- Oracle SQL Like 的最佳化OracleSQL
- MySQL 針對 like 條件的優化MySql優化
- 簡單說兩句 Like 的優化優化
- 索引優化index skip scan索引優化Index
- 重建索引index rebuild online vs offline vs index coalesce vs index shrik space索引IndexRebuild
- 【SQL優化】SQL優化工具SQL優化
- SQL Server優化之SQL語句優化SQLServer優化
- SQL中LIKE的妙用SQL
- SQL優化SQL優化
- with as優化sql優化SQL
- 效能優化案例-SQL優化優化SQL
- Index Full Scan vs Index Fast Full ScanIndexAST
- 資料庫優化 - SQL優化資料庫優化SQL
- sql優化之邏輯優化SQL優化
- SQL優化:limit分頁優化SQL優化MIT
- MySQL 效能優化之SQL優化MySql優化
- SQL優化--用各種hints優化一條SQLSQL優化
- 【SQL優化】SQL優化的10點注意事項SQL優化
- SQL SERVER中SQL優化SQLServer優化
- SQL Like萬用字元使用SQL字元
- SQL最佳化-COUNT_ INDEX的巧用SQLIndex
- no_index最佳化sql一例IndexSQL
- SQL優化指南SQL優化
- SQL SERVER優化SQLServer優化
- sql效能優化SQL優化
- Sql優化方法SQL優化
- oracle sql優化OracleSQL優化
- SQL優化-索引SQL優化索引
- SQL優化(一)SQL優化
- oracle sql 優化OracleSQL優化