查询表中最新纪录-减少表扫描量

简介:

 今天开发给了一条sql,查询表中最近更新的一条记录:

       select id,
       type,
       object_id,
       nick,
       isv_id,
       next_auditor_role,
       status,
       apply_parameters,
       comments,
       gmt_create,
       gmt_modified
      from (select id,
                type,
                object_id,
                nick,
                isv_id,
                next_auditor_role,
                status,
                apply_parameters,
                comments,
                gmt_create,
                gmt_modified
          from tops_apply
          where object_id = 552
          and status = 1
          order by gmt_create desc)
 where rownum = 1

一看这条sql就有优化的余地,内层查询查询了大量无用的数据。

2.创建索引:
17:02:07 SQL> create index ind_tops_apply_oid_sts_create1 on tops_apply(object_id,status,gmt_create)
17:02:30   2  tablespace tbs_top_ind
17:02:30   3  compute statistics;

3.执行sql
17:02:31 SQL> select id,
17:02:42   2         type,
17:02:42   3         object_id,
17:02:42   4         nick,
17:02:42   5         isv_id,
17:02:42   6         next_auditor_role,
17:02:42   7         status,
17:02:42   8         apply_parameters,
17:02:42   9         comments,
17:02:42  10         gmt_create,
17:02:42  11         gmt_modified
17:02:42  12    from (select id,
17:02:42  13                 type,
17:02:42  14                 object_id,
17:02:42  15                 nick,
17:02:42  16                 isv_id,
17:02:42  17                 next_auditor_role,
17:02:42  18                 status,
17:02:42  19                 apply_parameters,
17:02:42  20                 comments,
17:02:42  21                 gmt_create,
17:02:42  22                 gmt_modified
17:02:42  23            from tops_apply
17:02:42  24           where object_id = 552
17:02:42  25             and status = 1
17:02:42  26           order by gmt_create desc)
17:02:42  27   where rownum = 1;
 
         13  consistent gets
  
4.查询有13个逻辑读,改写sql,换成rowid形式来取结果:
17:02:44 SQL>  select id,
17:02:59   2          type,
17:02:59   3          object_id,
17:02:59   4          nick,
17:02:59   5          isv_id,
17:02:59   6          next_auditor_role,
17:02:59   7          status,
17:02:59   8          apply_parameters,
17:02:59   9          comments,
17:02:59  10          gmt_create,
17:02:59  11          gmt_modified
17:02:59  12     from tops_apply t, (select rid from (select rowid rid
17:02:59  13             from tops_apply
17:02:59  14            where object_id = 552
17:02:59  15              and status = 1
17:02:59  16            order by gmt_create desc) where rownum<2)tt where tt.rid=t.rowid;

 
          3  consistent gets

5.改写查询后,只有3个逻辑读,但是一个很简单的查询,但写出来的sql有些复杂,能不能有办法改进一下;在执行计划中看到了:
 INDEX RANGE SCAN DESCENDING| IND_TOPS_APPLY_OID_STS_CREATE1
 oracle很聪明的在order by gmt_create desc的时候对索引进行了descending扫描,在仔细想一想这条sql的目的是找出表中的最新记录, 而索引在创建的时候是有序的,这样就可以利用索引的有序性来查找出结果,oracle也只需要扫描索引的第一行(descending建索引)或者索引最后一行(默认 ASCEDING建索引 ),改写sql:

A.select id,
               type,
               object_id,
               nick,
               isv_id,
               next_auditor_role,
               status,
               apply_parameters,
               comments,
               gmt_create,
               gmt_modified
          from tops_apply
          where object_id = 552
          and status = 1 and rownum<2 order by gmt_create desc ;
         (index(object_id,status,gmt_create))
          执行计划:INDEX RANGE SCAN DESCENDING)
B.select id,
               type,
               object_id,
               nick,
               isv_id,
               next_auditor_role,
               status,
               apply_parameters,
               comments,
               gmt_create,
               gmt_modified
    from tops_apply
    where object_id = 552 and status = 1  and rownum<2;
    (index(object_id,status,gmt_create desc))
    执行计划:INDEX RANGE SCAN)

目录
相关文章
|
存储 编解码 人工智能
自媒体影视后期数字助理--视频调色中间件设计
阿里云提供的线上AI能力在处理视觉信息方面已经有较为成熟和通用的产品,对于开始兴建媒体资源管理平台的自媒体来说,采用阿里云的AI能力、函数计算以及OSS等产品进行平台搭建可以快速实现建设与能力扩充。本文为调色中间件的开发思路、技术架构设计和开发实战中参数的设置介绍,对一些数字影像的基础概念和阿里云人工智能视觉生产的API细节进行了分析。
1706 118
自媒体影视后期数字助理--视频调色中间件设计
|
存储 网络协议 数据中心
|
JSON 安全 前端开发
使用Gradio显示 System32文件夹下的DLL文件说明
使用Gradio显示 System32文件夹下的DLL文件说明3. 编写 Gradio 应用程序代码 接下来,我们将编写一个 Python 脚本,使用 Gradio 创建一个界面。在这个界面中,我们将定义一个函数来格式化和返回 DLL 文件信息,并通过 Gradio 显示它。 以下是完整的代码示例: import gradio as gr # 要展示的文件说明 data = { "System32下的文件说明": [ {"name": "aclui.dll", "remark": "Security Descriptor Editor,没有它,注册表编缉器会无法运
452 3
|
人工智能 搜索推荐 算法
AI Agent应用出路到底在哪?
【10月更文挑战第6天】
476 0
AI Agent应用出路到底在哪?
|
缓存 算法 编译器
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
887 0
|
C++
有效的完全平方数(C++)
有效的完全平方数(C++)
308 0
谈一谈|“互联网+”时代的创新
谈一谈|“互联网+”时代的创新
368 0
|
缓存 监控 网络协议
云上业务稳定性保障实践白皮书》——五.行业客户稳定性保障实践——5.3 平台网站业务稳定性保障——5.3.3高可用架构建设(上)
云上业务稳定性保障实践白皮书》——五.行业客户稳定性保障实践——5.3 平台网站业务稳定性保障——5.3.3高可用架构建设(上)
767 0
|
Java Maven Android开发
eclipse新建maven项目:'Building' has encountered a problem. Errors occurred during the build.
eclipse新建maven项目:'Building' has encountered a problem. Errors occurred during the build.
1340 0
eclipse新建maven项目:'Building' has encountered a problem. Errors occurred during the build.

热门文章

最新文章