• 索引特性之存列值优化sum avg


    --要领:只要索引能回答问题,索引就可以当成一个"瘦表",访问路径就会减少。另外切记不存储空值


    drop table t purge;
    create table t as select * from dba_objects;
    update t set object_id=rownum;
    commit;
    create index idx1_object_id on t(object_id);
    set autotrace on
    select count(*) from t;
    执行计划
    -------------------------------------------------------------------
    | Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
    -------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |      |     1 |   292   (1)| 00:00:04 |
    |   1 |  SORT AGGREGATE    |      |     1 |            |          |
    |   2 |   TABLE ACCESS FULL| T    | 69485 |   292   (1)| 00:00:04 |
    -------------------------------------------------------------------
    统计信息
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
           1048  consistent gets

    --为啥用不到索引,因为索引不能存储空值,所以加上一个is not null,再试验看看            
    select count(*) from t where object_id is not null;
    执行计划
    ----------------------------------------------------------------------------------------
    | Id  | Operation             | Name           | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT      |                |     1 |    13 |    50   (2)| 00:00:01 |
    |   1 |  SORT AGGREGATE       |                |     1 |    13 |            |          |
    |*  2 |   INDEX FAST FULL SCAN| IDX1_OBJECT_ID | 69485 |   882K|    50   (2)| 00:00:01 |
    ----------------------------------------------------------------------------------------
    统计信息
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
            170  consistent gets

    --也可以不加is not null,直接把列的属性设置为not null,也成,继续试验如下:
    alter table t modify OBJECT_ID not null;
    select count(*) from t;
    执行计划
    --------------------------------------------------------------------------------
    | Id  | Operation             | Name           | Rows  | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT      |                |     1 |    49   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE       |                |     1 |            |          |
    |   2 |   INDEX FAST FULL SCAN| IDX1_OBJECT_ID | 69485 |    49   (0)| 00:00:01 |
    --------------------------------------------------------------------------------
    统计信息
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
            170  consistent gets
              0  physical reads
              0  redo size
            425  bytes sent via SQL*Net to client
            416  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processed


    --如果是主键就无需定义列是否允许为空了。
    drop table t purge;
    create table t as select * from dba_objects;
    update t set object_id=rownum;
    alter table t add constraint pk1_object_id primary key (OBJECT_ID);
    set autotrace on
    select count(*) from t;

    执行计划
    -------------------------------------------------------------------------------
    | Id  | Operation             | Name          | Rows  | Cost (%CPU)| Time     |
    -------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT      |               |     1 |    46   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE       |               |     1 |            |          |
    |   2 |   INDEX FAST FULL SCAN| PK1_OBJECT_ID | 69485 |    46   (0)| 00:00:01 |
    -------------------------------------------------------------------------------
    统计信息
    ----------------------------------------------------------
              0  recursive calls
              0  db block gets
            160  consistent gets

  • 相关阅读:
    token系统讲解及过期处理
    SpringCloudGateway网关实战(二)
    对比两个列表是否相等
    Python 字典类型拓展(包括 MappingProxyType 只读字典, defaultdict 缺省字典和 ChainMap)
    Apipost forEach控制器怎么用
    hyper-v虚拟机的基本使用
    六零导航页SQL注入漏洞复现(CVE-2023-45951)
    Spark基础【两个小案例、Yarn模式下任务执行源码】
    飞桨框架v2.3发布高可复用算子库PHI!重构开发范式,降本增效
    matlab矩阵的输入
  • 原文地址:https://blog.csdn.net/qq_25439957/article/details/126380574