zoukankan      html  css  js  c++  java
  • with as +分析函数优化自链接

    select para_id
      from dwf.f_Savc_Buscode b
     where b.Para_Id = (SELECT MIN(Para_Id)
                          FROM Dwf.f_Savc_Buscode
                         WHERE Bus_Code = b.Bus_Code);
    
    SQL> set linesize 200
    SQL> set pagesize 200
    SQL> explain plan for select para_id
      from dwf.f_Savc_Buscode b
     where b.Para_Id = (SELECT MIN(Para_Id)
                          FROM Dwf.f_Savc_Buscode
                         WHERE Bus_Code = b.Bus_Code);  2    3    4    5  
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 1952012934
    
    ----------------------------------------------------------------------------------------
    | Id  | Operation		| Name	       | Rows  | Bytes | Cost (%CPU)| Time     |
    ----------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT	|	       |    86 |  2924 |     7	(15)| 00:00:01 |
    |*  1 |  HASH JOIN		|	       |    86 |  2924 |     7	(15)| 00:00:01 |
    |   2 |   VIEW			| VW_SQ_1      |    86 |  2236 |     4	(25)| 00:00:01 |
    |   3 |    HASH GROUP BY	|	       |    86 |   688 |     4	(25)| 00:00:01 |
    |   4 |     INDEX FAST FULL SCAN| SYS_C0036092 |   435 |  3480 |     3	 (0)| 00:00:01 |
    |   5 |   INDEX FAST FULL SCAN	| SYS_C0036092 |   435 |  3480 |     3	 (0)| 00:00:01 |
    ----------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - access("B"."PARA_ID"="MIN(PARA_ID)" AND "ITEM_1"="B"."BUS_CODE")
    
    17 rows selected.
    
    改写为with as 后:
    with A as (select para_id,min(Para_Id) over(partition by Bus_Code) min_para_id 
    from Dwf.f_Savc_Buscode)
    select para_id from A where A.para_id=a.min_para_id
    SQL> explain plan for with A as (select para_id,min(Para_Id) over(partition by Bus_Code) min_para_id 
    from Dwf.f_Savc_Buscode)
    select para_id from A where A.para_id=a.min_para_id  2    3  ;
    
    Explained.
    
    SQL> select * from table(dbms_xplan.display());
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 1562643515
    
    ---------------------------------------------------------------------------------------
    | Id  | Operation	       | Name	      | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT       |	      |   435 | 11310 |     4  (25)| 00:00:01 |
    |*  1 |  VIEW		       |	      |   435 | 11310 |     4  (25)| 00:00:01 |
    |   2 |   WINDOW SORT	       |	      |   435 |  3480 |     4  (25)| 00:00:01 |
    |   3 |    INDEX FAST FULL SCAN| SYS_C0036092 |   435 |  3480 |     3	(0)| 00:00:01 |
    ---------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("A"."PARA_ID"="A"."MIN_PARA_ID")
    
    15 rows selected.
    
    
    SQL> select * from (with A as (select para_id,min(Para_Id) over(partition by Bus_Code) min_para_id 
    from Dwf.f_Savc_Buscode)
    select para_id from A where A.para_id=a.min_para_id)
    minus
    select para_id
      from dwf.f_Savc_Buscode b
     where b.Para_Id = (SELECT MIN(Para_Id)
                          FROM Dwf.f_Savc_Buscode
                         WHERE Bus_Code = b.Bus_Code)  2    3    4    5    6    7    8    9  ;
    
    no rows selected
    

  • 相关阅读:
    MFC 控件RadioButton和CheckBox区别
    python的传递实参
    python的返回值
    Machine Learning的定义
    pythion的定义函数和传递实参
    python的用户输入和while循环
    python的字典
    python的if语句
    python的元组及其书写规矩
    python中操作列表
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13352203.html
Copyright © 2011-2022 走看看