[點晴永久免費OA]SQL:Like 的優(yōu)化
1。盡量不要使用 like '%..%'
2。對于 like '..%..' (不以 % 開頭),Oracle可以應用 colunm上的index 3。對于 like '%...' 的 (不以 % 結(jié)尾),可以利用reverse + function index 的形式,變化成 like '..%'
sys@mescp> select reverse('123') from dual;REVERSE('123') -------------------------------- 321 1 row selected. sys@mescp> create table test_like as select object_id,object_name from dba_objects; Table created. sys@mescp> create index test_like__name on test_like(object_name); Index created. sys@mescp> create index test_like__name_reverse on test_like(reverse(object_name)); Index created. Table analyzed. sys@mescp> set autotrace trace exp
sys@mescp> select * from test_like where object_name like AS%';
---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=655 Bytes=15720) 1 0 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_LIKE' (Cost=2 Card=655Bytes=15720) 2 1 INDEX (RANGE SCAN) OF 'TEST_LIKE__NAME' (NON-UNIQUE) (Cost=2 Card=118)
sys@mescp> select * from test_like where object_name like '%%';
---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=6 Card=655 Bytes=15720) 1 0 TABLE ACCESS (FULL) OF 'TEST_LIKE' (Cost=6 Card=655 ytes=15720)
sys@mescp> select * from test_like where object_name like '%S'; Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=6 Card=655 Bytes=15720) 1 0 TABLE ACCESS (FULL) OF 'TEST_LIKE' (Cost=6 Card=655 Bytes=15720) --'以常量結(jié)束的,加個reverse 函數(shù),又可以用上index了' sys@mescp> select * from test_like where reverse(object_name)like reverse('%AS'); Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=655 Bytes=15720) 1 0 TABLE ACCESS (BY INDEX ROWID) OF 'TEST_LIKE' (Cost=2 Card=655 Bytes=15720) 2 1 INDEX (RANGE SCAN) OF 'TEST_LIKE__NAME_REVERSE' (NON-UNIQUE) (Cost=2 Card=118) 該文章在 2011/2/28 11:34:43 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |