Chinaunix首页 | 论坛 | 博客
  • 博客访问: 650633
  • 博文数量: 789
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 4985
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-28 09:29
文章分类

全部博文(789)

文章存档

2011年(1)

2008年(788)

我的朋友

分类:

2008-10-28 09:44:45

    1。尽量不要使用 like '%..%'
   
    2。对于 like '..%..' (不以 % 开头),可以应用 colunm上的index
   
    3。对于 like '%...' 的 (不以 % 结尾),可以利用reverse + function index 的形式,变化成 like '..%'


    代码:


    -- '建表和Index,注意,重点在于带reverse的function index。同时,一定要使用CBO才行……


    > select reverse('123') from dual;REVERSE('123')

    --------------------------------

    321


    1 row selected.

    > create table test_like as select object_id,object_name from dba_objects;

    Table created.

    > create index test_like__name on test_like(object_name);

    Index created.

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

    Index created.
    > analyze table test_like compute statistics for table for all indexes;

    Table analyzed.

    > set autotrace trace exp


    -- '常量开头的like , 会利用index ,没问题…… '

    > select * from test_like where object_name like '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=655Bytes=15720)

    2 1 INDEX (RANGE SCAN) OF 'TEST_LIKE__NAME' (NON-UNIQUE) (Cost=2 Card=118)


    --'开头和结尾都是 % ,对不起,很难优化'

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


    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 ytes=15720)


    -- '以常量结束,直接写的时候是不能应用index的'

    > 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)


    --'以常量结束的,加个reverse 函数,又可以用上index了'

    > 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)

 

【责编:Ken】

--------------------next---------------------

阅读(318) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~