• 标量子查询 子查询执行次数计算公式


    select a.username,(select count(*) from all_objects b where b.owner=a.username) cnt from all_users a;
    
    create table a as select * from all_users;
    
    create table b as select * from all_objects;
    
    
    select a.username,(select count(*) from b where b.owner=a.username) cnt from a;
    
    
    SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    SQL_ID	2q0bhbzak85z9, child number 0
    -------------------------------------
    select a.username,(select count(*) from b where b.owner=a.username) cnt
    from a
    
    Plan hash value: 3049668959
    
    ----------------------------------------------------------------------------------------------
    | Id  | Operation	   | Name | Starts | E-Rows | A-Rows |	 A-Time   | Buffers | Reads  |
    ----------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT   |	  |	 1 |	    |	  36 |00:00:00.01 |	  6 |	   0 |
    |   1 |  SORT AGGREGATE    |	  |	36 |	  1 |	  36 |00:00:01.09 |   36756 |  36648 |
    |*  2 |   TABLE ACCESS FULL| B	  |	36 |	780 |  43773 |00:00:01.02 |   36756 |  36648 |
    |   3 |  TABLE ACCESS FULL | A	  |	 1 |	 36 |	  36 |00:00:00.01 |	  6 |	   0 |
    ----------------------------------------------------------------------------------------------
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       2 - filter("B"."OWNER"=:B1)
    
    Note
    -----
       - dynamic sampling used for this statement (level=2)
    
    
    25 rows selected.
    
    标量子查询会执行传入的值 DISTINCT那么多次,这里传入的值是a.username 就是distinct(a.username)次
    
    
    SQL> 
    select count(distinct a.username) from a;SQL> 
    
    COUNT(DISTINCTA.USERNAME)
    -------------------------
    		       36
    
    
    改写成左链接:
    
    
    
      select a.username,nvl(b.cnt,0) from a left join (select count(*) cnt ,b.owner from b group by b.owner ) b
      on b.owner = a.username
      
      select a.username,count(b.owner) from  a left join  b on b.owner=a.username group by a.username
    
    
    
    
    
    
    
    
    
    

  • 相关阅读:
    BZOJ 2400: Spoj 839 Optimal Marks (按位最小割)
    bzoj4873: [Shoi2017]寿司餐厅(最大权闭合子图)
    bzoj1497: [NOI2006]最大获利(最大权闭合子图)
    bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
    bzoj1024: [SCOI2009]生日快乐
    bzoj2761: [JLOI2011]不重复数字
    bzoj1257: [CQOI2007]余数之和sum
    bzoj2456: mode
    bzoj1831: [AHOI2008]逆序对(DP+双精bzoj1786)
    bzoj2431: [HAOI2009]逆序对数列
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13352331.html
Copyright © 2020-2023  润新知