网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > 数据库 > Oracle教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
数据库:数据库教程,数据库技巧,Oracle教程,MySQL教程,Sybase教程,Access教程,DB2教程,数据库安全,数据库文摘
本月文章推荐
.多系统共存的方法.
.ORACLE数据库常见问题诊断方法 -.
.Oracle数据库维护常用SQL语句集合.
.使用Oracle9i全索引扫描快速访问.
.Oracle数据库10g产品特性简介.
.教你怎样进行Oracle数据库性能完.
.用改变跟踪技术加速Oracle递增备.
.Linux Shadow-Password-HOWTO.
.构建用于位置信息开放标准的Port.
.Mandrake9.0的启动过程(从init开.
.关于Oracle数据库各种存储形式的.
.Oracle 排序中常用的NULL值处理方.
.Oracle SQL*Loader 使用指南(转.
.Oracle数据库技术(13).
.Oracle 9i Installation on Red .
.oracle数据库文件中的导入\导出(.
.甲骨文亚太地区第二财季总收入同.
.RESETLOGS后没有备份情况下的数据.
.一步步教你在RHEL AS 3安装Oracl.
.ORACLE入门之OLTP和DSS不同数据库.

通过Oracle虚拟索引决定是否建立索引

发表日期:2008-2-9



  在应用过程中,我们偶然会碰到想要在增加index前看看该index是否能带来预期的效果。为了达到这个目标,我们可以使用oem的virtual index的功能。为了能让大家比较简单地了解这个功能,我们为大家演示一下不在oem下virtual index的使用。 另外,即然叫virtual index, 那么当建立它时,应当不耗资源的。这对于很大的表,当我们想看建某个index是否能改善当前语句的执行计划时,显然很有帮助。我们来看例子:
   
  有一news表,400多M。有语句:
  
   Code: [Copy to clipboard] 
  
  select count(*) from News 
   where Gmt_Origin >= trunc(sysdate + 0.5)
    and Gmt_Origin < trunc(sysdate + 0.5) + 1 
    and news_type_general = 'y'
    and ((News_Category_Id IN (100901,5002640,5002641,100902,5000042,5001060,5002041,9)) 
     OR (News_Category_Id_2 IN (100901,5002640,5002641,100902,5000042,5001060,5002041,9)) 
     OR (News_Category_Id_3 IN (100901,5002640,5002641,100902,5000042,5001060,5002041,9)));
  
  它使用了index,然而效果并不理想:
  
   Code: [Copy to clipboard] 
  
  Operation Object Name Rows Bytes Cost Object Node In/Out PStart PStop
   
  SELECT STATEMENT Hint=CHOOSE  1    267       
    SORT AGGREGATE  1  28         
     FILTER            
      TABLE Access BY INDEX ROWID NEWS 1  28  267       
       INDEX RANGE SCAN NEWS_RELATED3_IND 1    266       
   
  分析条件后,我们认为在gmt_origin上建index可能会有帮助,然而我们并不清楚有于其他indexes的影响,当我们建了基于gmt_origin的index时,是否的确会有帮助。
  
  于是我们预备在gmt_origin上建个virtual index:
  
   Code: [Copy to clipboard] 
  
  SQL> alter session set sql_trace = true;
   
  Session altered.
   
  SQL> create index news_gorigin_ind on news(gmt_origin) nosegment ; 
   
  Index created.
  
  trace是:
  
   Code: [Copy to clipboard] 
  
  create index news_gorigin_ind on news(gmt_origin) nosegment
   
  call   count    cpu  elapsed    disk   query  current    rows
  ------- ------ -------- ---------- ---------- ---------- ---------- ----------
  Parse    1   0.06    0.08     2     4     0      0
  Execute   1   0.02    0.42     0     2     2      0
  Fetch    0   0.00    0.00     0     0     0      0
  ------- ------ -------- ---------- ---------- ---------- ---------- ----------
  total    2   0.08    0.50     2     6     2      0
   
  Misses in library cache during parse: 1
  Optimizer goal: CHOOSE
  
  我们可以从tracer看出,建立它的确是不耗多少资源。
   
  为了能利用这个virtual index,我们还必须修改当前session参数:
  
  Code: [Copy to clipboard] 
  
  SQL> alter session set "_use_nosegment_indexes" = true;
   
  Session altered.
  
  现在我们来看看效果:
  
  SQL> eXPlain plan for 
    2 select count(*) from News 
    3  where Gmt_Origin >= trunc(sysdate + 0.5)
    4  and Gmt_Origin < trunc(sysdate + 0.5) + 1 
    5  and news_type_general = 'y'
    6  and ((News_Category_Id IN (100901,5002640,5002641,100902,5000042,5001060,5002041,9)) 
    7   OR (News_Category_Id_2 IN (100901,5002640,5002641,100902,5000042,5001060,5002041,9)) 
    8   OR (News_Category_Id_3 IN (100901,5002640,5002641,100902,5000042,5001060,5002041,9)))
    9 /
   
  Explained.
   
  SQL> @$Oracle_HOME/rdbms/admin/utlxpls
   
  PLAN_TABLE_OUTPUT
  ------------------------------------------------------------------------------------------------------------------------
   
  -----------------------------------------------------------------------------------
   Id  Operation            Name       Rows  Bytes Cost 
  -----------------------------------------------------------------------------------
    0 SELECT STATEMENT                    1   20    3
    1  SORT AGGREGATE                     1   20    
    2   FILTER                                
    3   TABLE ACCESS BY INDEX ROWID NEWS          1   20    3
    4    INDEX RANGE SCAN      NEWS_GORIGIN_IND    3        2
  -----------------------------------------------------------------------------------
   
  Note: cpu costing is off, 'plan_table' is old version
   
  12 rows selected.
   
  执行计划显示它的确用到了我们预备建的index。

  
  设置10053看看
  
  -- Index stats
    INDEX NAME: NEWS_GMODIFIED_IND COL#: 3
     TOTAL :: LVLS: 2  #LB: 1295 #DK: 21318 LB/K: 1 DB/K: 8 CLUF: 186911
    INDEX NAME: NEWS_GORIGIN_IND COL#: 7
     TOTAL :: LVLS: 1  #LB: 25 #DK: 100 LB/K: 1 DB/K: 1 CLUF: 800
  ...
   Access path: index (scan)
      Index: NEWS_GORIGIN_IND
    TABLE: NEWS
      RSC_CPU: 0  RSC_IO: 3
    IX_SEL: 4.5000e-03 TB_SEL: 1.1250e-05
   ...
  Access path: index (index-only)
      Index: NEWS_GORIGIN_IND
    TABLE: NEWS
      RSC_CPU: 0  RSC_IO: 2
    IX_SEL: 4.5000e-03 TB_SEL: 4.5000e-03
上一篇:Oracle 数据安全问题面面观(2) 人气:990
下一篇:Oracle中使用的若干技术经典总结 人气:626
浏览全部Oracle教程的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐