zoukankan      html  css  js  c++  java
  • 添加索引后SQL消耗量在执行计划中的变化

    不同索引的执行效率也是不一样的,下面比较三条SQL语句在正常查询与建立普通索引与位图索引后的CPU消耗量的变化,目的为了是加强对索引的理解与运用

    实验步骤:

    1、创建有特点的大数据表。
    为了保证索引产生前后,查询效果的正确比对,应建立一个存在大量数据的测试表。这个测试表的数据来源于SYS模式下的all_objects视图,其中包括本数据库实例中的全部对象的基本描述,具体包括对象的所有者、对象名称、创建日期等信息。
    创建测试表的具体过程:

    创建大数据表,sys用户下的all_objects表就比较适合做查询用,把该表创建在scott用户下,可以先确定scott用户是否是解锁的

        SQL>  CREATE TABLE scott.DemoTable as

        SQL>  SELECT * FROM all_objects

     SQL>  WHERE owner IN ('SYS','PUBLIC','SCOTT');

    这里需要注意的是如果where后面的条件不成立的话,只会复制表结构,可以另外添加一下数据

    SQL> insert into scott.demotable select * from sys.all_objects;

    85029 rows created.

    2.查询创建好的demotable的基本情况,连接scott用户

    Desc scott.demotable

    SQL> insert into scott.demotable select * from sys.all_objects;

    SQL> select count(distinct owner),

      2  count(distinct object_type),

      3  count(distinct object_name)

      4  from demotable;

    3、分析表,开启追踪   这里的意思是打开执行计划,执行计划的查看方式有很多种。
    analyze table demoTable compute statistics;
    set autotrace trace explain  --开启追踪
    注意:关闭追踪的方法为  set autotrace off; 

    开启追踪

    SQL> analyze table demotable compute statistics;

    Table analyzed.

    SQL> set autotrace trace explain;

    查看EMP的信息,

    SQL> select * from demotable where object_name = 'EMP';

    SQL> select * from demotable where owner='SCOTT';

    Scott用户cpu342,所占279K

    SQL> SELECT count(*) FROM demoTable where owner ='SCOTT';

    基于owner字段做实验建立建立索引

    SQL> CREATE INDEX idx_owner ON demoTable(owner);

    Index created.

    SQL> SELECT * FROM demoTable where object_name = 'EMP';

    SQL> SELECT * FROM demoTable where owner ='SCOTT';

    SELECT count(*) FROM demoTable where owner ='SCOTT';

    4、创建位图索引,查看位图索引与普通索引,或者不建立索引带来的变化

       --删除原有owner索引
        DROP INDEX idx_owner;

    SQL> CREATE BITMAP INDEX idx_owner_bitmap ON demoTable(owner);

    Index created.

    SQL> SELECT * FROM demoTable where object_name = 'EMP';

     

    SQL> SELECT * FROM demoTable where owner ='SCOTT';

    SELECT count(*) FROM demoTable where owner ='SCOTT';

  • 相关阅读:
    Java UDP通信简单实现
    为什么要阅读——兼分享《首先,打破一切常规》[中译文]:世界顶级管理者的成功秘诀/(美)马库斯·白金汉,(美)柯特·科夫曼 著
    怎样提高团队管理能力9
    POJ2777 Count Color 线段树区间更新
    Swift和Objective-C混合编程——Swift调用OC
    LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    ZZUOJ-1195-OS Job Scheduling(郑州大学第七届ACM大学生程序设计竞赛E题)
    CentOS出错You don't have permission to access on this server
    string 和 vector 初探
    ICMP报文类型
  • 原文地址:https://www.cnblogs.com/houzhiheng/p/12411089.html
Copyright © 2011-2022 走看看