Oracle SQL

    Yearly Archives: 2017

    Top N biggest tables (with lobs, indexes and nested table)

    Posted on December 16, 2017 by Sayan Malakshinov Posted in oracle Leave a comment

    Script for SQL*Plus: https://github.com/xtender/xt_scripts/blob/master/tops/top_seg_by_size.sql

    with 
      seg as (
         select 
           owner,segment_name
          ,segment_type
          ,tablespace_name
          ,sum(blocks) blocks
          ,sum(bytes)  bytes
         from dba_segments s
         where  segment_type not in (
           'TYPE2 UNDO'
          ,'ROLLBACK'
          ,'SYSTEM STATISTICS'
         )
         and segment_name not like 'BIN$%' --not in recyclebin
         and owner like '&owner_mask' -- you can specify schema here
         group by owner,segment_name,segment_type,tablespace_name
      )
     ,segs as (
         select 
           owner,segment_name
          ,case when segment_name like 'DR$%$%' then 'CTX INDEX' else segment_type end segment_type
          ,tablespace_name
          ,case 
             when segment_name like 'DR$%$%' 
               then (select table_owner||'.'||table_name from dba_indexes i where i.owner=s.owner and i.index_name = substr(segment_name,4,length(segment_name)-5))
             when segment_type in ('TABLE','TABLE PARTITION','TABLE SUBPARTITION')
                then owner||'.'||segment_name
             when segment_type in ('INDEX','INDEX PARTITION','INDEX SUBPARTITION')
                then (select i.table_owner||'.'||i.table_name from dba_indexes i where i.owner=s.owner and i.index_name=s.segment_name)
             when segment_type in ('LOBSEGMENT','LOB PARTITION','LOB SUBPARTITION')
                then (select l.owner||'.'||l.TABLE_NAME from dba_lobs l where l.segment_name = s.segment_name and l.owner = s.owner)
             when segment_type = 'LOBINDEX'
                then (select l.owner||'.'||l.TABLE_NAME from dba_lobs l where l.index_name = s.segment_name and l.owner = s.owner)
             when segment_type = 'NESTED TABLE'
                then (select nt.owner||'.'||nt.parent_table_name from dba_nested_tables nt where nt.owner=s.owner and nt.table_name=s.segment_name)
             when segment_type = 'CLUSTER'
                then (select min(owner||'.'||table_name) from dba_tables t where t.owner=s.owner and t.cluster_name=s.segment_name and rownum=1)
           end table_name
          ,blocks
          ,bytes
         from seg s
      )
     ,so as (
         select
           segs.owner
          ,substr(segs.table_name,instr(segs.table_name,'.')+1) TABLE_NAME
          ,sum(segs.bytes)  total_bytes
          ,sum(segs.blocks) total_blocks
          ,sum(case when segs.segment_type in ('TABLE','TABLE PARTITION','TABLE SUBPARTITION','NESTED TABLE','CLUSTER') then segs.bytes end) tab_size
          ,sum(case when segs.segment_type in ('INDEX','INDEX PARTITION','INDEX SUBPARTITION','CTX INDEX') then segs.bytes end) ind_size
          ,sum(case when segs.segment_type in ('CTX INDEX') then segs.bytes end) ctx_size
          ,sum(case when segs.segment_type in ('LOBSEGMENT','LOBINDEX','LOB PARTITION','LOB SUBPARTITION') then segs.bytes end) lob_size
         from segs
         group by owner,table_name
      )
     ,tops as (
         select 
               dense_rank()over (order by total_bytes desc) rnk
              ,so.*
         from so
      )
    select *
    from tops
    where rnk<=50 -- top 50
    /
    

    “Collection iterator pickler fetch”: pipelined vs simple table functions

    Posted on December 13, 2017 by Sayan Malakshinov Posted in oracle, PL/SQL, PL/SQL optimization, query optimizing, SQL, troubleshooting 2 Comments

    Alex R recently discovered interesting thing: in SQL pipelined functions work much faster than simple non-pipelined table functions, so if you already have simple non-pipelined table function and want to get its results in sql (select * from table(fff)), it’s much better to create another pipelined function which will get and return its results through PIPE ROW().

    A bit more details:

    Assume we need to return collection “RESULT” from PL/SQL function into SQL query “select * from table(function_F(…))”.
    If we create 2 similar functions: pipelined f_pipe and simple non-pipelined f_non_pipe,

    create or replace function f_pipe(n int) return tt_id_value pipelined 
    as
      result tt_id_value;
    begin
      ...
      for i in 1..n loop
        pipe row (result(i));
      end loop;
    end f_pipe;
    /
    create or replace function f_non_pipe(n int) return tt_id_value 
    as
      result tt_id_value;
    begin
      ...
      return result;
    end f_non_pipe;
    /
    
    Full functions definitions
    create or replace type to_id_value as object (id int, value int)
    /
    create or replace type tt_id_value as table of to_id_value
    /
    create or replace function f_pipe(n int) return tt_id_value pipelined 
    as
      result tt_id_value;
      
      procedure gen is
      begin
         result:=tt_id_value();
         result.extend(n);
         for i in 1..n loop
            result(i):=to_id_value(i, 1);
         end loop;
      end;    
    begin
      gen();
      for i in 1..n loop
        pipe row (result(i));
      end loop;
    end f_pipe;
    /
    create or replace function f_non_pipe(n int) return tt_id_value 
    as
      result tt_id_value;
      
      procedure gen is
      begin
         result:=tt_id_value();
         result.extend(n);
         for i in 1..n loop
            result(i):=to_id_value(i, 1);
         end loop;
      end;    
    begin
      gen();
      return result;
    end f_non_pipe;
    /
    create or replace function f_pipe_for_nonpipe(n int) return tt_id_value pipelined 
    as
      result tt_id_value;
    begin
      result:=f_non_pipe(n);
      for i in 1..result.count loop
        pipe row (result(i));
      end loop;
    end;
    /
    create or replace function f_udf_pipe(n int) return tt_id_value pipelined 
    as
      result tt_id_value;
      
      procedure gen is
      begin
         result:=tt_id_value();
         result.extend(n);
         for i in 1..n loop
            result(i):=to_id_value(i, 1);
         end loop;
      end;    
    begin
      gen();
      for i in 1..n loop
        pipe row (result(i));
      end loop;
    end;
    /
    create or replace function f_udf_non_pipe(n int) return tt_id_value 
    as
      result tt_id_value;
      
      procedure gen is
      begin
         result:=tt_id_value();
         result.extend(n);
         for i in 1..n loop
            result(i):=to_id_value(i, 1);
         end loop;
      end;    
    begin
      gen();
      return result;
    end;
    /
    

    [collapse]
    Test queries

    set echo on feed only timing on;
    --alter session set optimizer_adaptive_plans=false;
    --alter session set "_optimizer_use_feedback"=false;
    
    select sum(id * value) s from table(f_pipe(&1));
    select sum(id * value) s from table(f_non_pipe(&1));
    select sum(id * value) s from table(f_pipe_for_nonpipe(&1));
    select sum(id * value) s from table(f_udf_pipe(&1));
    select sum(id * value) s from table(f_udf_non_pipe(&1));
    with function f_inline_non_pipe(n int) return tt_id_value 
    as
      result tt_id_value;
    begin
         result:=tt_id_value();
         result.extend(n);
         for i in 1..n loop
            result(i):=to_id_value(i, 1);
         end loop;
         return result;
    end;
    select sum(id * value) s from table(f_inline_non_pipe(&1));
    /
    set timing off echo off feed on;
    

    [collapse]

    we’ll find that the function with simple “return result” works at least twice slower than pipelined function:

    Function 1 000 000 elements 100 000 elements
    F_PIPE 2.46 0.20
    F_NON_PIPE 4.39 0.44
    F_PIPE_FOR_NONPIPE 2.61 0.26
    F_UDF_PIPE 2.06 0.20
    F_UDF_NON_PIPE 4.46 0.44

    I was really surprised that even “COLLECTION ITERATOR PICKLER FETCH” with F_PIPE_FOR_NONPIPE that gets result of F_NON_PIPE and returns it through PIPE ROW() works almost twice faster than F_NON_PIPE, so I decided to analyze it using stapflame by Frits Hoogland.

    I added “dbms_lock.sleep(1)” into both of these function after collection generation, to compare the difference only between “pipe row” in loop and “return result”:

    Modified functions

    create or replace function f_pipe(n int) return tt_id_value pipelined 
    as
      result tt_id_value;
      
      procedure gen is
      begin
         result:=tt_id_value();
         result.extend(n);
         for i in 1..n loop
            result(i):=to_id_value(i, 1);
         end loop;
      end;    
    begin
      gen();
      dbms_lock.sleep(1);
      for i in 1..n loop
        pipe row (result(i));
      end loop;
    end f_pipe;
    /
    create or replace function f_non_pipe(n int) return tt_id_value 
    as
      result tt_id_value;
      
      procedure gen is
      begin
         result:=tt_id_value();
         result.extend(n);
         for i in 1..n loop
            result(i):=to_id_value(i, 1);
         end loop;
      end;    
    begin
      gen();
      dbms_lock.sleep(1);
      return result;
    end f_non_pipe;
    /
    

    [collapse]

    And stapflame showed that almost all overhead was consumed by the function “kgmpoa_Assign_Out_Arguments”:

    I don’t know what this function is doing exactly, but we can see that oracle assign collection a bit later.
    From other functions in this stack(pmucpkl, kopp2isize, kopp2colsize, kopp2atsize(attribute?), kopuadt) I suspect that is some type of preprocessiong of return arguments.
    What do you think about it?

    Full stapflame output:
    stapflame_nonpipe
    stapflame_pipe

    oracle pipelined functions pl/sql pl/sql functions pl/sql optimization

    SQL*Plus tips #8: How to read the output of dbms_output without “serveroutput on”

    Posted on December 10, 2017 by Sayan Malakshinov Posted in oracle, SQL*Plus, SQL*PLus tips 1 Comment

    When “serveroutput” is enabled, SQL*Plus executes “BEGIN DBMS_OUTPUT.GET_LINES(:LINES, :NUMLINES); END;” after each command.
    That’s why I don’t like when it is always enabled: it adds extra calls and round-trips and it is inconvenient when I want to get a plan of the last executed query:

    SQL> set serverout on;
    SQL> select * from dual;
    
    D
    -
    X
    
    SQL> select * from table(dbms_xplan.display_cursor('','','allstats last'));
    
    PLAN_TABLE_OUTPUT
    --------------------------------------------------------------------------
    SQL_ID  9babjv8yq8ru3, child number 0
    
    BEGIN DBMS_OUTPUT.GET_LINES(:LINES, :NUMLINES); END;
    
    NOTE: cannot fetch plan for SQL_ID: 9babjv8yq8ru3, CHILD_NUMBER: 0
          Please verify value of SQL_ID and CHILD_NUMBER;
          It could also be that the plan is no longer in cursor cache (check v$sql_plan)
    

    So usually I switch “serveroutput” on only if needed, but sometimes I can forget to enable it. In such cases I use very simple script that reads the output using dbms_output.get_lines and prints it using refcursor:
    https://github.com/xtender/xt_scripts/blob/master/output_print.sql

    When you set “serveroutput on“, SQL*Plus also executes “dbms_output.enable” and if you set “serverout off” it executes “dbms_output.disable”, that’s why my glogin.sql contains “call dbms_output.enable(1e6);” and you need to execute it after each “set serverout off” if you want to use this script.

    oracle SQL*Plus

    Bug with integer literals in PL/SQL

    Posted on December 9, 2017 by Sayan Malakshinov Posted in curious, oracle, PL/SQL, PL/SQL optimization, undocumented Leave a comment

    This interesting question was posted on our russian forum yesterday:

    We have a huge PL/SQL package and this simple function returns wrong result when it’s located at the end of package body:

    create or replace package body PKGXXX as
      ...
      function ffff return number is
      nRes number;
      begin        
        nRes :=  268435456;
        return nRes;
      end;
    end;
    /
    

    But it works fine in any of the following cases:
    * replace 268435456 with power(2, 28), or
    * replace 268435456 with small literal like 268, or
    * move this function to the beginning of package body

    The one of the interesting findings was that the returned value is equal to the one of literals in another function.
    We can reproduce this bug even with an anonymous pl/sql block. The following test case uses 32768 integer literals from 1000001 to 1032768 and prints 5 other integers:

    declare n number;
    begin
      n:=1000001; -- this part
      n:=1000002; -- creates
      n:=1000003; -- 32768 
       ...        -- integer
      n:=1032768; -- literals
        dbms_output.put_line('100000='||100000); -- it should print: 100000=100000
        dbms_output.put_line('32766 ='||32766);
        dbms_output.put_line('32767 ='||32767);    
        dbms_output.put_line('32768 ='||32768);
        dbms_output.put_line('32769 ='||32769);
    end;
    
    Test code
    declare
       c clob:='declare n number;begin'||chr(10);
       f varchar2(100):='n:=%s;'||chr(10);
       v varchar2(32767);
       n number:=32768;
    begin
       for i in 1..n loop
          v:=v||utl_lms.format_message(f,to_char(1e7+i));
          if length(v)>30000 then
             c:=c||v;
             v:='';
          end if;
       end loop;
       v:=v||q'[
        dbms_output.put_line('100000='||100000);
        dbms_output.put_line('32766 ='||32766);
        dbms_output.put_line('32767 ='||32767);    
        dbms_output.put_line('32768 ='||32768);
        dbms_output.put_line('32769 ='||32769);
       end;
       ]';
       c:=c||v;
       execute immediate c;
    end;
    /
    

    [collapse]
    It produces the following output:

    100000=10000001
    32766 =32766
    32767 =32767
    32768 =10000002
    32769 =10000003
    

    This test case well demonstrates wrong results:
    * instead of 100000 we get 10000001, which is the value from first line after “begin”, ie 1st integer literal in the code,
    * for 32766 and 32767 oracle returns right values
    * instead of 32768 (==32767+1) it returns 10000002, which is the integer from 2nd line, ie 2nd integer literal in the code,
    * instead of 32769 (==32767+2) it returns 10000003, which is the integer from 3rd line, ie 3rd integer literal in the code
    After several tests I can make a conclusion:

    • It doesn’t matter what plsql_optimize_level or plsql_code_type you set, was debug enabled or not, the behaviour is the same.
    • It seems that this is a kind of PL/SQL optimization: during parsing, oracle leaves integer literal in place if its value is in range -32768..32767 (16bit signed int), but if its value is out of this range, oracle adds this value into array of integers’ constants and replaces the value with the index of this element in this array. But because of index value overflow in cases when a count of such integer literals becomes larger than 32768, instead of Nth element of this array, oracle returns Mth element, where M is mod(N,32767).

    So we can describe this behaviour using first test case:

    declare n number;
    begin
      n:=1000001; -- this part
      n:=1000002; -- creates
      n:=1000003; -- 32768 
       ...        -- integer
      n:=1032768; -- literals
        dbms_output.put_line('100000='||100000); -- it should print 100000, ie 32768th element of array, but prints 10000001
                                                 -- where 10000001 is the 1st element of array (1==mod(32768,32767))
        dbms_output.put_line('32766 ='||32766);  -- these 2 lines print right values,
        dbms_output.put_line('32767 ='||32767);  -- because their values are in the range of -32768..32767
        dbms_output.put_line('32768 ='||32768);  -- this line contains 32769th element and prints 2nd element of array (2==mod(32769,32767))
        dbms_output.put_line('32769 ='||32769);  -- this line contains 32770th element and prints 3nd element of array (3==mod(32770,32767))
    end;
    

    The following query can help you to find objects which can potentially have this problem:

    select
      s.owner,s.name,s.type
     ,sum(regexp_count(text,'(\W|^)3\d{4,}([^.0-9]|$)')) nums_count -- this regexp counts integer literals >= 30000
    from dba_source s 
    where 
        owner='&owner'
    and type in ('FUNCTION','PROCEDURE','PACKAGE','PACKAGE BODY')
    group by s.owner,s.name,s.type
    having sum(regexp_count(text,'(\W|^)3\d{4,}([^.0-9]|$)'))>32767 -- filter only objects which have >=32767 integer literal
    

    Workaround:
    You may noticed that I wrote about INTEGER literals only, so the easiest workaround is to make them FLOAT – just add “.” to the end of each literal:

    declare n number;
    begin
      n:=1000001.;
      n:=1000002.;
      n:=1000003.;
       ...       
      n:=1032768.;
        dbms_output.put_line('100000='||100000.);
        dbms_output.put_line('32766 ='||32766.);
        dbms_output.put_line('32767 ='||32767.);    
        dbms_output.put_line('32768 ='||32768.);
        dbms_output.put_line('32769 ='||32769.);
    end;
    
    Fixed test cases

    declare
       c clob:='declare n number;begin'||chr(10);
       f varchar2(100):='n:=%s.;'||chr(10); -- I've added here "."
       v varchar2(32767);
       n number:=32768;
    begin
       for i in 1..n loop
          v:=v||utl_lms.format_message(f,to_char(1e7+i));
          if length(v)>30000 then
             c:=c||v;
             v:='';
          end if;
       end loop;
       v:=v||q'[
        dbms_output.put_line('100000='||100000.); -- .
        dbms_output.put_line('32766 ='||32766.);
        dbms_output.put_line('32767 ='||32767.);    
        dbms_output.put_line('32768 ='||32768.);
        dbms_output.put_line('32769 ='||32769.);
       end;
       ]';
       c:=c||v;
       execute immediate c;
    end;
    /
    

    [collapse]
    bug oracle oracle undocumented behaviour pl/sql pl/sql optimization

    Oracle issues after upgrade to 12.2

    Posted on November 24, 2017 by Sayan Malakshinov Posted in 12.2, bug, oracle 2 Comments

    Sometimes it’s really hard even to create reproducible test case to send it to oracle support, especially in case of intermittent errors.
    In such cases, I think it would be really great to have access to similar service requests or bugs of other oracle clients.
    So while my poll about knowledge sharing is still active, I want to share a couple of bugs we have faced after upgrade to 12.2 (and one bug from Eric van Roon). I’m going to remove the bugs from this list when they become “public” or “fixed”.
    If you want to add own findings into this list, you can add them into comments. To make this process easier, you can provide just symptomps, short description and the link to own post with details – I’ll add it just as a link.
    Continue reading→

    12.2.0.1 bind variable bug deterministic functions oracle troubleshooting undocumented oracle

    Triggers and Redo: changes on 12.2

    Posted on November 19, 2017 by Sayan Malakshinov Posted in 12c, oracle, trace 1 Comment

    In one of the previous posts I showed How even empty trigger increases redo generation, but running the test from that post, I have found that this behaviour a bit changed on 12.2:
    In my old test case, values of column A were equal to values of B, and on previous oracle versions including 12.1.0.2 we can see that even though “update … set B=A” doesn’t change really “B”, even empty trigger greatly increases redo generation.
    But on 12.2.0.1 in case of equal values, the trigger doesn’t increase redo, so we can see small optimization here, though in case of different values, the trigger still increases reado generation greatly.

    same_dumpredo.sql
    set feed on;
    drop table xt_curr1 purge;
    drop table xt_curr2 purge;
    -- simple table:
    create table xt_curr1 as select '2' a, '2' b from dual connect by level<=1000;
    -- same table but with empty trigger:
    create table xt_curr2 as select '2' a, '2' b from dual connect by level<=1000;
    create or replace trigger tr_xt_curr2 before update on xt_curr2 for each row
    begin
      null;
    end;
    /
    -- objectID and SCN:
    col obj1 new_val obj1;
    col obj2 new_val obj2;
    col scn  new_val scn;
    select 
      (select o.OBJECT_ID from user_objects o where o.object_name='XT_CURR1') obj1
     ,(select o.OBJECT_ID from user_objects o where o.object_name='XT_CURR2') obj2
     ,d.CURRENT_SCN scn
    from v$database d
    /
    -- logfile1:
    alter system switch logfile;
    col member new_val logfile;
    SELECT member
    FROM v$logfile
    WHERE 
         is_recovery_dest_file='NO'
     and group#=(SELECT group# FROM v$log WHERE status = 'CURRENT')
     and rownum=1;
    -- update1:
    set autot trace stat;
    update xt_curr1 set b=a;
    set autot off;
    commit;
    -- dump logfile1:
    alter session set tracefile_identifier='log1_same';
    ALTER SYSTEM DUMP LOGFILE '&logfile' SCN MIN &scn OBJNO &obj1;
    
    -- logfile2:
    alter system switch logfile;
    col member new_val logfile;
    SELECT member
    FROM v$logfile
    WHERE 
         is_recovery_dest_file='NO'
     and group#=(SELECT group# FROM v$log WHERE status = 'CURRENT')
     and rownum=1;
    -- update2:
    set autot trace stat;
    update xt_curr2 set b=a;
    set autot off;
    commit;
    -- dump logfile2:
    alter session set tracefile_identifier='log2_same';
    ALTER SYSTEM DUMP LOGFILE '&logfile' OBJNO &obj2;
    alter session set tracefile_identifier='off';
    disc;
    
    

    [collapse]

    diff_dumpredo.sql

    set feed on;
    drop table xt_curr1 purge;
    drop table xt_curr2 purge;
    -- simple table:
    create table xt_curr1 as select '1' a, '2' b from dual connect by level<=1000;
    -- same table but with empty trigger:
    create table xt_curr2 as select '1' a, '2' b from dual connect by level<=1000;
    create or replace trigger tr_xt_curr2 before update on xt_curr2 for each row
    begin
      null;
    end;
    /
    -- objectID and SCN:
    col obj1 new_val obj1;
    col obj2 new_val obj2;
    col scn  new_val scn;
    select 
      (select o.OBJECT_ID from user_objects o where o.object_name='XT_CURR1') obj1
     ,(select o.OBJECT_ID from user_objects o where o.object_name='XT_CURR2') obj2
     ,d.CURRENT_SCN scn
    from v$database d
    /
    -- logfile1:
    alter system switch logfile;
    col member new_val logfile;
    SELECT member
    FROM v$logfile
    WHERE 
         is_recovery_dest_file='NO'
     and group#=(SELECT group# FROM v$log WHERE status = 'CURRENT')
     and rownum=1;
    -- update1:
    set autot trace stat;
    update xt_curr1 set b=a;
    set autot off;
    commit;
    -- dump logfile1:
    alter session set tracefile_identifier='log1_diff';
    ALTER SYSTEM DUMP LOGFILE '&logfile' SCN MIN &scn OBJNO &obj1;
    
    -- logfile2:
    alter system switch logfile;
    col member new_val logfile;
    SELECT member
    FROM v$logfile
    WHERE 
         is_recovery_dest_file='NO'
     and group#=(SELECT group# FROM v$log WHERE status = 'CURRENT')
     and rownum=1;
    -- update2:
    set autot trace stat;
    update xt_curr2 set b=a;
    set autot off;
    commit;
    -- dump logfile2:
    alter session set tracefile_identifier='log2_diff';
    ALTER SYSTEM DUMP LOGFILE '&logfile' OBJNO &obj2;
    alter session set tracefile_identifier='off';
    disc;
    
    

    [collapse]

    Equal values:
    12.1.0.2:

    12.2.0.1:

    Different values:
    12.1.0.2:

    12.2.0.1:

    We can easily find that trigger disables batched “Array update”:

    redo triggers

    Adaptive serial direct path read decision ignores object statistics since 12.1

    Posted on November 19, 2017 by Sayan Malakshinov Posted in 12c, adaptive serial direct path reads, oracle 1 Comment

    On versions 11.2.0.2 – 11.2.0.4 Oracle uses objects’ statistics to make direct path reads decision (of course, if “_direct_read_decision_statistics_driven” haven’t changed it to “false”), and we can force serial direct reads on statement level using sql profiles with hints INDEX_STATS/TABLES_STATS, but since at least 12.1.0.2 this decision ignores statistics.

    Btw, thanks to Jure Bratina, we know now, that we need to repeat hint TABLE_STATS at least twice to make it work 🙂 And from the following test case we know that it takes parameters from second one 🙂

    Compare trace files of the following test cases:

    table_stats2.sql

    drop table t1;
    
    create table t1 as select * from dual;
    ----------------------------
    pro ######################################;
    exec dbms_stats.gather_table_stats('','T1');
    exec dbms_stats.set_table_stats(user,'T1',numblks => 33333333);
    
    col value new_val oldval noprint;
    select value from v$statname n, v$mystat s
    where n.statistic#=s.statistic# and n.name ='physical reads direct';
    
    ----------------------------
    alter session set tracefile_identifier='table_stats2';
    --alter session set events '10053 trace name context forever, level 1';
    alter session set events 'trace[nsmtio] disk highest';
    select/*+ table_stats(t1, scale, blocks=66666666 rows=2222222222) 
              table_stats(t1, scale, blocks=99999999 rows=4444444444)
           */ 
           * 
    from t1;
    select value-&oldval directreads, value from v$statname n, v$mystat s
    where n.statistic#=s.statistic# and n.name ='physical reads direct';
    
    

    [collapse]

    You can see that our hint successfully changed number of blocks and forced direct path reads on 11.2.0.4:
    Oracle 11.2.0.4:

    NSMTIO: qertbFetch:DirectRead:[OBJECT_SIZE>VLOT]
    NSMTIO: Additional Info: VLOT=797445
    Object# = 78376, Object_Size = 66666666 blocks
    SqlId = 7naawntkc57yx, plan_hash_value = 3617692013, Partition# = 0
    

    [collapse]

    But on 12.1.0.2 and 12.2.0.1 we can see 2 lines with “NSMTIO: kcbism” with the different types(2 and 3) and different number of blocks, and the direct path read decision was based on second one – from segment header:

    Oracle 12.1.0.2:

    NSMTIO: kcbism: islarge 1 next 0 nblks 66666666 type 3, bpid 65535, kcbisdbfc 0 kcbnhl 4096 kcbstt 983 keep_nb 0 kcbnbh 59010 kcbnwp 1
    NSMTIO: kcbism: islarge 0 next 0 nblks 4 type 2, bpid 3, kcbisdbfc 0 kcbnhl 4096 kcbstt 983 keep_nb 0 kcbnbh 59010 kcbnwp 1
    NSMTIO: qertbFetch:NoDirectRead:[- STT < OBJECT_SIZE < MTT]:Obect's size: 4 (blocks), Threshold: MTT(4917 blocks),
    _object_statistics: enabled, Sage: enabled, 
    Direct Read for serial qry: enabled(::::::), Ascending SCN table scan: FALSE 
    flashback_table_scan: FALSE, Row Versions Query: FALSE
    SqlId: 7naawntkc57yx, plan_hash_value: 3617692013, Object#: 302342, Parition#: 0 DW_scan: disabled
    

    [collapse]

    Oracle 12.2.0.1:

    NSMTIO: kcbism: islarge 1 next 0 nblks 66666666 type 3, bpid 65535, kcbisdbfc 0 kcbnhl 4096 kcbstt 1214 keep_nb 0 kcbnbh 45026 kcbnwp 1
    NSMTIO: kcbism: islarge 0 next 0 nblks 4 type 2, bpid 3, kcbisdbfc 0 kcbnhl 4096 kcbstt 1214 keep_nb 0 kcbnbh 45026 kcbnwp 1
    NSMTIO: qertbFetch:NoDirectRead:[- STT < OBJECT_SIZE < MTT]:Obect's size: 4 (blocks), Threshold: MTT(6072 blocks),
    _object_statistics: enabled, Sage: enabled, 
    Direct Read for serial qry: enabled(:::::::), Ascending SCN table scan: FALSE 
    flashback_table_scan: FALSE, Row Versions Query: FALSE
    SqlId: 7naawntkc57yx, plan_hash_value: 3617692013, Object#: 174411, Parition#: 0 DW_scan: disabled
    

    [collapse]

    And similar example, but for IFFS(index fast full scan):
    nb: I set the number of index blocks using dbms_stats to 33333000 and hinted the query with 77777700

    index_stats2.sql

    drop table t2 purge;
    ALTER SESSION SET optimizer_dynamic_sampling = 0;
    ALTER SESSION SET "_optimizer_use_feedback" = FALSE;
    ALTER SESSION SET optimizer_adaptive_features = FALSE;
    ALTER SESSION SET optimizer_adaptive_plans=FALSE;
    
    create table t2(x) as select level from dual connect by level<=1000;
    create index t2_ix on t2(1,x,rpad(x,100));
    
    begin
       dbms_stats.gather_table_stats('','T2',cascade => true);
       dbms_stats.set_table_stats(user,'T2'   ,numblks => 33333333);
       dbms_stats.set_index_stats(user,'T2_IX',numlblks => 33333000);
    end;
    /
    
    col value new_val oldval noprint;
    select value from v$statname n, v$mystat s
    where n.statistic#=s.statistic# and n.name ='physical reads direct';
    
    alter session set tracefile_identifier='index_stats2';
    alter session set events 'trace[nsmtio] disk highest';
    select/*+ index_stats(t2, t2_ix, scale, blocks=7777700)
              index_ffs(t2 t2_ix)
              dynamic_sampling(0)
           */ 
           count(*) cnt2
    from t2;
    select value-&oldval directreads, value from v$statname n, v$mystat s
    where n.statistic#=s.statistic# and n.name ='physical reads direct';
    
    disc;
    

    [collapse]

    You can see that on 11.2.0.4 oracle gets number of blocks from the hint (7777700)
    Oracle 11.2.0.4 - index_stats:

    NSMTIO: qerixFetchFastFullScan:DirectRead[OBJECT_SIZE > VLOT]:
    NSMTIO: AdditionalInfo: Object_size: 7777700 (blocks), vlot=797445
    SqlId=by2zv0k566hj5, plan_hash_value=3419274230,Object#=78375
    

    [collapse]

    From the first line we can see that kcbism takes the hinted number of blocks, but later kcbivlo rewrites it with the number from segment header:

    Oracle 12.1.0.2 - index_stats:

    NSMTIO: kcbism: islarge 1 next 0 nblks 7777700 type 3, bpid 65535, kcbisdbfc 0 kcbnhl 4096 kcbstt 983 keep_nb 0 kcbnbh 59010 kcbnwp 1
    NSMTIO: kcbivlo: nblks 22 vlot 500 pnb 49175 kcbisdbfc 0 is_large 0
    NSMTIO: qerixFetchFastFullScan:[MTT < OBJECT_SIZE < VLOT]:NSMTIO: AdditionalInfo: Object_size: 22 (blocks), vlot=245875
    SqlId=by2zv0k566hj5, plan_hash_value=3419274230,Object#=302347
    

    [collapse]

    Oracle 12.2.0.1 - index_stats:

    NSMTIO: kcbism: islarge 1 next 0 nblks 7777700 type 3, bpid 65535, kcbisdbfc 0 kcbnhl 4096 kcbstt 1214 keep_nb 0 kcbnbh 45026 kcbnwp 1
    NSMTIO: kcbivlo: nblks 22 vlot 500 pnb 60729 kcbisdbfc 0 is_large 0
    NSMTIO: qerixFetchFastFullScan:[MTT < OBJECT_SIZE < VLOT]:NSMTIO: AdditionalInfo: Object_size: 22 (blocks), vlot=303645
    SqlId=by2zv0k566hj5, plan_hash_value=3419274230,Object#=174409
    

    [collapse]

    So we have 2 options to force direct reads:
    1. to execute alter session set “_serial_direct_read”=’ALWAYS’;
    2. or to force parallel plan to get parallel direct path reads (we can do it with even with dop=1)

    12c direct path reads

    Easy(lazy) way to check which programs have properly configured FetchSize

    Posted on November 16, 2017 by Sayan Malakshinov Posted in oracle, SQL, statistics Leave a comment
    select 
       s.module
      ,ceil(max(s.rows_processed/s.fetches)) rows_per_fetch
    from v$sql s
    where 
        s.rows_processed>100
    and s.executions    >1
    and s.fetches       >1
    and s.module is not null
    and s.command_type  = 3    -- SELECTs only
    and s.program_id    = 0    -- do not account recursive queries from stored procs
    and s.parsing_schema_id!=0 -- <> SYS
    group by s.module
    order by rows_per_fetch desc nulls last
    /
    
    arraysize fetchsize troubleshooting

    PL/SQL functions: Iterate and keys for associative arrays

    Posted on October 2, 2017 by Sayan Malakshinov Posted in oracle, PL/SQL, PL/SQL optimization Leave a comment

    Unfortunately associative arrays still require more “coding”:
    we still can’t use “indices of” or “values of” in simple FOR(though they are available for FORALL for a long time), don’t have convinient iterators and even function to get all keys…
    That’s why I want to show my templates for such things like iterator and keys function. You can adopt these functions and create them on schema level.

    declare 
       type numbers  is table of number;
       type anumbers is table of number index by pls_integer;
       a anumbers;
       i pls_integer;
    
       function iterate( idx in out nocopy pls_integer, arr in out nocopy anumbers) 
          return boolean
       as pragma inline;
       begin
          if idx is null 
             then idx:=arr.first; 
             else idx:=arr.next(idx);
          end if;
          return idx is not null;
       end;
    
      function keys(a in out nocopy anumbers) return numbers as
         res numbers:=numbers();
         idx number;
         pragma inline;
      begin
         while iterate(idx,a) loop
            res.extend;
            res(res.count):=idx;
         end loop;
         return res;
      end;
    
    begin
       a(1):=10;
       a(3):=30;
       a(5):=50;
       a(8):=80;
       -- iterate:
       while iterate(i,a) loop
          dbms_output.put_line(a(i));
       end loop;
    
       -- keys:
       for i in 1..keys(a).count loop
          dbms_output.put_line(a(keys(a)(i)));
       end loop;
    
    end;
    

    How to group connected elements (or pairs)

    Posted on September 29, 2017 by Sayan Malakshinov Posted in oracle, PL/SQL, PL/SQL optimization, query optimizing, SQL 7 Comments

    I see quite often when developers ask questions about connected components:

    Table “MESSAGES” contains fields “SENDER” and “RECIPIENT”, which store clients id.
    How to quickly get all groups of clients who are connected even through other clients if the table has X million rows?
    So for this table, there should be 4 groups:

    • (1, 2, 4, 8, 16)
    • (3, 6, 12)
    • (5, 10, 20)
    • (7, 14)
    • (9, 18)
    SENDERRECIPIENT
    12
    24
    36
    48
    510
    612
    714
    816
    918
    1020

    Of course, we can solve this problem using SQL only (model, recursive subquery factoring or connect by with nocycle), but such solutions will be too slow for huge tables.

    Example of SQL solution

    with 
       t(sender,recipient) as (select level,level*2 from dual connect by level<=10)
    ,  v1 as (select rownum id,t.* from t)
    ,  v2 as (select id, account
              from v1
               unpivot (
                 account for x in (sender,recipient)
               ))
    , v3 as (
               select
                  id
                 ,account
                 ,dense_rank()over(order by account) account_n
                 ,count(*)over() cnt
               from v2)
    , v4 as (
               select distinct grp,account
               from v3
               model
                    dimension by (id,account_n)
                    measures(id grp,account,cnt)
                    rules
                    iterate(1e6)until(iteration_number>cnt[1,1])(
                       grp[any,any] = min(grp)[any,cv()]
                      ,grp[any,any] = min(grp)[cv(),any]
                    )
    )
    select
       listagg(account,',')within group(order by account) s
    from v4
    group by grp
    

    [collapse]

    In such situations it’s much better to adopt standard algorithms like Quick-find or Weighted quick-union for PL/SQL.
    The first time I wrote such solution about 5 years ago and I even posted here one of the latest solutions, but all of them were not universal, so I’ve created the package today with a couple of functions for most common problems: XT_CONNECTED_COMPONENTS

    It contains 2 functions based on Weighted quick-find quick-union algorithm:

    • function get_strings(cur in sys_refcursor, delim varchar2:=’,’) return strings_array pipelined;
      It accepts a cursor and returns found connected components as table of varchar2(v_size). You can change v_size in the package definition.
      Input cursor should contain one Varchar2 column with linked strings, for example: ‘a,b,c’.
      You can also specify list delimiter, by default it is comma.
      Examples:

      select * from table(xt_connected_components.get_strings( cursor(select ELEM1||','||ELEM2 from TEST));
      select * 
      from
       table(
         xt_connected_components.get_strings( 
           cursor(select 'a,b,c' from dual union all
                  select 'd,e,f' from dual union all
                  select 'e,c'   from dual union all
                  select 'z'     from dual union all
                  select 'X,Y'   from dual union all
                  select 'Y,Z'   from dual)));
      COLUMN_VALUE
      -----------------------------------------
      STRINGS('X', 'Y', 'Z')
      STRINGS('a', 'b', 'c', 'd', 'e', 'f')
      STRINGS('z')
      
      
    • function get_numbers(cur in sys_refcursor) return numbers_array pipelined;
      This function also returns connected components, but for numbers.
      Input cursor should contain two columns with linked numbers.
      Examples:

      select * 
      from table(
              xt_connected_components.get_numbers( 
                cursor(
                  select sender_id, recipient_id from messages
              )));
      select * 
      from
        table(
          xt_connected_components.get_numbers( 
             cursor(
                select level   account1
                     , level*2 account2 
                from dual 
                connect by level<=10
          )));
      SQL> select *
        2  from
        3    table(
        4      xt_connected_components.get_numbers(
        5         cursor(
        6            select level   account1
        7                 , level*2 account2
        8            from dual
        9            connect by level<=10
       10*     )))
      SQL> /
      
      COLUMN_VALUE
      ------------------------
      NUMBERS(1, 2, 4, 8, 16)
      NUMBERS(3, 6, 12)
      NUMBERS(5, 10, 20)
      NUMBERS(7, 14)
      NUMBERS(9, 18)
      

    How to install:
    Download all files from Github and execute “@install” in SQL*Plus or execute them in another tool in the following order:
    xt_connected_components_types.sql
    xt_connected_components_pkg.sql
    xt_connected_components_bdy.sql

    Download URL: https://github.com/xtender/xt_scripts/tree/master/extra/xt_connected_components

    query optimization

    Simple Android Oracle client

    Get it on Google Play

    About us

    photo Sayan Malakshinov

    Oracle ACE Associate Oracle ACE Associate
    Oracle performance tuning expert
    Russia / Moscow / Transmedia Dynamics
    photo Bair Malakshinov

    Ph.d candidate
    Senior Oracle Developer
    Poland / Cracow / Luxoft

    Popular Posts

    • Differences between integer(int) in SQL and PL/SQL 0 comments
    • Deterministic function vs scalar subquery caching. Part 1 8 comments
    • Amazing optimization of getting distinct values from the index, and TopN for each of them 4 comments
    • SQL*Plus tips #6: Colorizing output 4 comments
    • SQL*Plus tips #5: sql_text/sql_fulltext formatting(sql beatifier) 13 comments
    • SQL*Plus tips. #1 5 comments
    • A couple of well-known but often forgotten things for PL/SQL developers 2 comments
    • SYS_OP_MAP_NONNULL is in the documentation now 0 comments
    • SQL*Plus tips #4: Branching execution 0 comments
    • Oracle 12c: Lateral, row_limiting_clause 3 comments

    Recent Posts

    • Top-N again: fetch first N rows only vs rownum
    • Docker with Oracle database: install patches automatically
    • Top N biggest tables (with lobs, indexes and nested table)
    • “Collection iterator pickler fetch”: pipelined vs simple table functions
    • SQL*Plus tips #8: How to read the output of dbms_output without “serveroutput on”

    Email Subscription

    Recent Comments

    • SQL*Plus 256 Colours in terminal | EDUARDO CLARO on SQL*Plus tips #6: Colorizing output
    • A simple SQL*Plus parameter parser | EDUARDO CLARO on SQL*Plus tips. #1
    • Sayan Malakshinov on “Collection iterator pickler fetch”: pipelined vs simple table functions
    • Frits on “Collection iterator pickler fetch”: pipelined vs simple table functions
    • SQL*Plus tips #8: How to read the output of dbms_output without 'serveroutput on' - SSWUG.ORG on SQL*Plus tips #8: How to read the output of dbms_output without “serveroutput on”
    • Adaptive serial direct path read decision ignores object statistics since 12.1 - SSWUG.ORG on Adaptive serial direct path read decision ignores object statistics since 12.1
    • Oracle issues after upgrade to 12.2 - SSWUG.ORG on Oracle issues after upgrade to 12.2
    • Ampersand instead of colon for bind variables - SSWUG.ORG on Ampersand instead of colon for bind variables
    • Евгений Бабин on Oracle issues after upgrade to 12.2
    • Oracle SQL | How even empty trigger increases redo generation on Triggers and Redo: changes on 12.2

    Blogroll

    • Alex Fatkulin
    • Alexander Anokhin
    • Andrey Nikolaev
    • Charles Hooper
    • Christian Antognini
    • Coskan Gundogar
    • David Fitzjarrell
    • Igor Usoltsev
    • Jonathan Lewis
    • Karl Arao
    • Mark Bobak
    • Martin Bach
    • Martin Berger
    • Neil Chandler
    • Randolf Geist
    • Richard Foote
    • Riyaj Shamsudeen
    • Tanel Poder
    • Timur Akhmadeev
    • Valentin Nikotin

    Categories

    Aggregated by OraNA Aggregated by OraFAQ

    Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • 1
    • 2
    • Next
    ©Sayan Malakshinov. Oracle SQL