Oracle中LIKE語句最佳化

來源:互聯網
上載者:User

1。盡量不要使用 like '%%'

2。對於 like '%' (不以 % 開頭),Oracle可以應用 colunm上的index

3。對於 like '%…' 的 (不以 % 結尾),可以利用reverse + function index 的形式,變化成 like '%'

建測試表和Index,注意,重點在於帶reverse的function index。同時,一定要使用CBO才行

create table test_like as select object_id,object_name from dba_objects;

-------建立測試表

create index test_like__name on test_like(object_name);

------建立索引

create index test_like__name_reverse on test_like(reverse(object_name));

------建立反向索引

analyze table test_like compute statistics for table for all indexes;

------對錶進行分析

都過SQLPLUS串連到資料,一定是SQLPLUS,因為下面有寫命令在PLSQL的命令列中不被支援;

set autotrace trace exp

-----設定SQL跟蹤

set linesize 2000

-------設定輸出寬度

select * from test_like where object_name like 'AS%';

使用了索引

select * from test_like where object_name like '%S';

未使用索引

select * from test_like where reverse(object_name)like reverse('%AS');

使用了索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.