- 最后登录
- 2015-3-26
- 在线时间
- 148 小时
- 威望
- 84
- 金钱
- 1061
- 注册时间
- 2011-11-26
- 阅读权限
- 50
- 帖子
- 128
- 精华
- 0
- 积分
- 84
- UID
- 96
|
1#
发表于 2012-8-14 11:12:17
|
查看: 6777 |
回复: 4
最近在做SQL优化,在网上看到有网友说“使用instr函数要比使用like匹配快很多“,持着怀疑的态度测试了一下。
测试结果:使用instr就是比like匹配快,虽然like走的是索引,而我并没有建立基于instr函数的索引。
测试过程:
1、表dm_accntab 信息
SQL> select count(*) from dm_accntab;
COUNT(*)
----------
828128
SQL> select status,LAST_ANALYZED from user_tables where table_name='DM_ACCNTAB';
STATUS LAST_ANALYZED
-------- -------------
VALID 8/13/2012 10:
SQL> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name=i.index_name and t.table_name=i.table_name and t.table_name='DM_ACCNTAB';
INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESCEND INDEX_TYPE
------------------------------ ------------------------------ -------------------------------------------------------------------------------- --------------- ------------- ----------- ------- ---------------------------
IDX_DM_ACCNTAB DM_ACCNTAB ACCT_CODE 1 64 64 ASC NORMAL
IDX_DM_ACCNTAB DM_ACCNTAB DATA_DATE 2 10 10 ASC NORMAL
2、对比
select acct_code,data_date from dm_accntab where acct_code like '450231%';
5.265 seconds
SQL> select acct_code,data_date from dm_accntab where instr(acct_code,'450231')=1;
4.172 seconds
3、查看执行计划
SQL> explain plan for select acct_code,data_date from dm_accntab where acct_code like '450231%';
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3997140732
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 67 | 1541 | 3 (0)| 00:00:0
|* 1 | INDEX RANGE SCAN| IDX_DM_ACCNTAB | 67 | 1541 | 3 (0)| 00:00:0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ACCT_CODE" LIKE '450231%')
filter("ACCT_CODE" LIKE '450231%')
14 rows selected
SQL> explain plan for select acct_code,data_date from dm_accntab where instr(acct_code,'450231')=1;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3646046415
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 8290 | 186K| 4791 (1)| 00:00:58 |
|* 1 | TABLE ACCESS FULL| DM_ACCNTAB | 8290 | 186K| 4791 (1)| 00:00:58 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(INSTR("ACCT_CODE",'450231')=1)
13 rows selected
我想请教下刘大,这真的是神话吗? 如果是,那以后见到Like就把它替换成instr ... |
|