SQL优化经典案例----让in/exists子查询作为驱动表

本文通过一个具体案例展示了如何通过调整SQL语句的结构,使in/exists子查询作为驱动表来提高查询效率。优化前后对比显示,逻辑读操作次数显著减少。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQL优化经典案例----让in/exists子查询作为驱动表

 
---http://blog.sina.com.cn/s/blog_61cd89f60102efam.html

    在NL连接方式中,用小表或小的结果集作业驱动表,是提高SQL执行效率的重要因素之一,下面我们看一下让in/exists子查询作为驱动表优化的案例

select  rowid rid 
   from its_car_pass7 v 
  where 1 = 1 
    and pass_datetime >= 
        to_date('2013-07-06 :17:46:04', 'yyyy-mm-dd hh24:mi:ss') 
    and pass_datetime <= 
        to_date('2013-07-06 :18:46:06', 'yyyy-mm-dd hh24:mi:ss') 
    and v.pass_device_unid in 
        (select unid 
           from its_base_device 
          where dev_bay_unid in ('01685EFE4658C19D59C4DDAAEDD37393') 
            and dev_type = '1' 
            and dev_chk_flag = '1' 
            and dev_delete_flag = 'N') 
  order by v.pass_datetime asc
 

Execution Plan  
SQL优化经典案例----让in/exists子查询作为驱动表
Predicate Information (identified by operation id): 
--------------------------------------------------- 
 
   5 - access("PASS_DATETIME">=TO_DATE(' 2013-07-06 17:46:04', 'syyyy-mm-dd hh24:mi:ss') AND 
              "PASS_DATETIME"<=TO_DATE(' 2013-07-06 18:46:06', 'syyyy-mm-dd hh24:mi:ss')) 
       filter("PASS_DATETIME">=TO_DATE(' 2013-07-06 17:46:04', 'syyyy-mm-dd hh24:mi:ss') AND 
              "PASS_DATETIME"<=TO_DATE(' 2013-07-06 18:46:06', 'syyyy-mm-dd hh24:mi:ss')) 
   6 - access("V"."PASS_DEVICE_UNID"="UNID") 
   7 - filter("DEV_BAY_UNID"='01685EFE4658C19D59C4DDAAEDD37393' AND "DEV_TYPE"='1' AND 
              "DEV_DELETE_FLAG"='N' AND "DEV_CHK_FLAG"='1')  
Statistics 
---------------------------------------------------------- 
          1  recursive calls 
          0  db block gets 
     110973  consistent gets 
          0  physical reads 
          0  redo size 
      47861  bytes sent via SQL*Net to client 
       1656  bytes received via SQL*Net from client 
        105  SQL*Net roundtrips to/from client 
          1  sorts (memory) 
          0  sorts (disk) 
       1560  rows processed 

查看outline部分信息

SELECT FROM TABLE(DBMS_XPLAN.DISPLAY(NULL, NULL, 'ADVANCED -PROJECTION'));    

 Outline Data 
------------- 
SQL优化经典案例----让in/exists子查询作为驱动表

 

SQL优化后:

select SQL优化经典案例----让in/exists子查询作为驱动表 rowid rid 
   from its_car_pass7 v 
  where 1 = 1 
    and pass_datetime >= 
        to_date('2013-07-06 :17:46:04', 'yyyy-mm-dd hh24:mi:ss') 
    and pass_datetime <= 
        to_date('2013-07-06 :18:46:06', 'yyyy-mm-dd hh24:mi:ss') 
    and v.pass_device_unid in 
        (select unid 
           from its_base_device 
          where dev_bay_unid in ('01685EFE4658C19D59C4DDAAEDD37393') 
            and dev_type = '1' 
            and dev_chk_flag = '1' 
            and dev_delete_flag = 'N') 
  order by v.pass_datetime asc 

优化后执行计划信息

SQL优化经典案例----让in/exists子查询作为驱动表

Statistics 
---------------------------------------------------------- 
         1  recursive calls 
         0  db block gets  
   18645  consistent gets 
      130  physical reads 
        0  redo size 
    47861  bytes sent via SQL*Net to client 
     1657  bytes received via SQL*Net from client 
      105  SQL*Net roundtrips to/from client 
        1  sorts (memory) 
        0  sorts (disk) 
     1560  rows processed  

优化效果还是不错的,逻辑读有原有的 110973 变为:18645

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值