CTOCIO IT专家网

天极传媒 比特网 | 天极网 | IT专家网 | IT商网 | 52PK游戏网 | 手机天极 | IT分众 |
IT专家网搜索

数据库 | Oracle | DB2 | SQL Server | MySQL | 商业智能 | BI | DBA | Sybase| SQL Server 2008

Oracle数据库中简单的Like优化

作者: RollingPig,  出处:专家博客, 责任编辑: 金璞, 
2007-01-26 08:00
    简单说两句,具体看例子   1。尽量不要使用 like '%..%'   2。对于 like '..%..' (不以 % 开头),Oracle可以应用 colunm上的index   3。对于 like '%...' 的 (不以 % 结尾),可以利用reverse + function index 的形式,变化成 like '..%'

  简单说两句,具体看例子

  1。尽量不要使用 like '%..%'

  2。对于 like '..%..' (不以 % 开头),Oracle可以应用 colunm上的index

  3。对于 like '%...' 的 (不以 % 结尾),可以利用reverse + function index 的形式,变化成 like '..%'

  代码:

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

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.
sys@mescp> analyze table test_like compute statistics for table for all indexes;

Table analyzed.

sys@mescp> set autotrace trace exp

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

sys@mescp> 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)

共2页。 1 2 :
  • 本文关键词:

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。
  • 周排行榜
  • 月排行榜

邮件订阅


    
天极服务 | 关于我们 | 网站律师 | 加入我们 | 联系我们 | 广告业务 | 友情链接 | 我要挑错