SQL 语句中对于like 的调优
SQL> create table test as select * from dba_objects;
表已創建。
SQL> select owner,object_name from test where owner like 'ROBINSON%';
OWNER??????????????????????????????????????????????????????? OBJECT_NAME
------------------------------------------------------------ ------------------------------
ROBINSON???????????????????????????????????????????????????? TEST
ROBINSON???????????????????????????????????????????????????? T2
ROBINSON???????????????????????????????????????????????????? T1
ROBINSON???????????????????????????????????????????????????? T3
ROBINSON???????????????????????????????????????????????????? T4
ROBINSON???????????????????????????????????????????????????? HEX_TO_BIN
已選擇6行。
SQL> create index i_owner on test(owner);
索引已創建。
SQL> update test set owner='SYSMAN' where owner!='ROBINSON';
已更新50353行。
SQL> update test set owner='SYSTEM' where object_id>1000 and object_id<1020;
已更新19行。
SQL> commit;
提交完成。
SQL> update test set owner='SCOTT' where object_id>2000 and object_id<2005;
已更新4行。
SQL> select distinct owner from test;
OWNER
------------------------------------------------------------
ROBINSON
SYSTEM
SCOTT
SYS
SQL> BEGIN
? 2???? DBMS_STATS.GATHER_TABLE_STATS(ownname => 'ROBINSON',
? 3?????????????????????????????????? tabname => 'TEST',
? 4?????????????????????????????????? estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
? 5?????????????????????????????????? method_opt => 'for all columns size repeat',
? 6?????????????????????????????????? degree => DBMS_STATS.AUTO_DEGREE,
? 7?????????????????????????????????? cascade=>TRUE
? 8?????????????????????????????????? );
? 9? END;
?10? /
PL/SQL 過程已成功完成。
SQL> set autot traceonly
SQL> select owner,object_name from test where owner not like 'SYS%';
已選擇10行。
執行計劃
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id? | Operation???????? | Name | Rows? | Bytes | Cost (%CPU)| Time???? |
--------------------------------------------------------------------------
|?? 0 | SELECT STATEMENT? |????? | 25180 |?? 737K|?? 142?? (3)| 00:00:02 |
|*? 1 |? TABLE ACCESS FULL| TEST | 25180 |?? 737K|?? 142?? (3)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
?? 1 - filter("OWNER" NOT LIKE 'SYS%')
統計信息
----------------------------------------------------------
????????? 0? recursive calls
????????? 0? db block gets
????????? 0? consistent gets
????????? 0? physical reads
????????? 0? redo size
????????? 0? bytes sent via SQL*Net to client
????????? 0? bytes received via SQL*Net from client
????????? 0? SQL*Net roundtrips to/from client
????????? 0? sorts (memory)
????????? 0? sorts (disk)
???????? 10? rows processed
從此處可以看到選擇了10行,但是卻走了全表掃描,現在對其加HINT提示
SQL> select /*+ INDEX (test I_OWNER) */ owner,object_name from test where owner not like 'SYS%';
已選擇10行。
執行計劃
----------------------------------------------------------
Plan hash value: 469605390
---------------------------------------------------------------------------------------
| Id? | Operation?????????????????? | Name??? | Rows? | Bytes | Cost (%CPU)| Time???? |
---------------------------------------------------------------------------------------
|?? 0 | SELECT STATEMENT??????????? |???????? | 25180 |?? 737K|?? 490?? (1)| 00:00:06 |
|?? 1 |? TABLE ACCESS BY INDEX ROWID| TEST??? | 25180 |?? 737K|?? 490?? (1)| 00:00:06 |
|*? 2 |?? INDEX FULL SCAN?????????? | I_OWNER | 25180 |?????? |?? 140?? (3)| 00:00:02 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
?? 2 - filter("OWNER" NOT LIKE 'SYS%')
統計信息
----------------------------------------------------------
????????? 0? recursive calls
????????? 0? db block gets
????????? 0? consistent gets
????????? 0? physical reads
????????? 0? redo size
????????? 0? bytes sent via SQL*Net to client
????????? 0? bytes received via SQL*Net from client
????????? 0? SQL*Net roundtrips to/from client
????????? 0? sorts (memory)
????????? 0? sorts (disk)
???????? 10? rows processed
?
我們發現,強制加HINT提示所花的成本比全表掃描成本更高,這里要想優化這條SQL 必須改like
SQL> drop index i_owner;
索引已刪除。
SQL>? create? index f_owner on test(instr(owner,'SYS',1,1)) ;
索引已創建。
SQL> select? owner,object_name from test where instr(owner,'SYS',1,1)=0;
已選擇10行。
執行計劃
----------------------------------------------------------
Plan hash value: 535182827
---------------------------------------------------------------------------------------
| Id? | Operation?????????????????? | Name??? | Rows? | Bytes | Cost (%CPU)| Time???? |
---------------------------------------------------------------------------------------
|?? 0 | SELECT STATEMENT??????????? |???????? |?? 504 | 15120 |??? 52?? (0)| 00:00:01 |
|?? 1 |? TABLE ACCESS BY INDEX ROWID| TEST??? |?? 504 | 15120 |??? 52?? (0)| 00:00:01 |
|*? 2 |?? INDEX RANGE SCAN????????? | F_OWNER |?? 201 |?????? |??? 49?? (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
?? 2 - access(INSTR("OWNER",'SYS',1,1)=0)
統計信息
----------------------------------------------------------
????????? 0? recursive calls
????????? 0? db block gets
????????? 0? consistent gets
????????? 0? physical reads
????????? 0? redo size
????????? 0? bytes sent via SQL*Net to client
????????? 0? bytes received via SQL*Net from client
????????? 0? SQL*Net roundtrips to/from client
????????? 0? sorts (memory)
????????? 0? sorts (disk)
???????? 10? rows processed
當使用函數索引后,COST由原來的142下降到現在的52
總結:對于LIKE語句,我們可以使用instr函數來進行SQL調優
?
?
轉載于:https://www.cnblogs.com/hehe520/archive/2010/03/23/6330603.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的SQL 语句中对于like 的调优的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell函数可接受不同参数
- 下一篇: c语言从html控件sscanf,ssc