How to Improve this PL/SQL script

Hi All,
I have a package/procedure that insert data into table but it takes time more than 2 days. Have any known how to modify this script to improve the performance and reduce loading time, The following code is procedure I used to insert data.....
Procedure INSERT_DATA (p_month IN DATE, p_product_id IN NUMBER ) IS
cursor c1 is select * from tab#1; --reference data
cursor c2 is select * from tab#2;
cursor c3 is select * from tab#3;
cursor c4 is select * from tab#4;
cursor c5 is select * from tab#5;
v_rec claim_table%rowtype;
Begin
for c1rec in c1 loop
exit when c1%notfound;
call procedure in package....;
open c2(c1rec.claim_no);
fetch c2 into v_location_cd ,v_claim_type_cd ;
close c2;
v_rec.location_cd := v_location_cd;
v_rec.claim_type_cd := v_claim_type_cd ;
open c3(c1rec.claim_no);
fetch c3 into v_col#3,v_col#4;
close c3;
v_rec.col#3 := v_col#3 ;
v_rec.col#4 := v_col#4 ;
open c4(c1rec.claim_no);
fetch c4 into v_col#5,v_col#6;
close c4;
v_rec.col#5 := v_col#5 ;
v_rec.col#6 := v_col#6 ;
insert into claim_table values ( v_rec.location_cd, v_rec.claim_type_cd , v_rec.col#3 , .......) ;
if (c1%rowcount/1000) = trunc(c1%rowcount /1000) then
     commit;
end if;
end loop;
commit;
Exception
exception statement....
commit;
End;
Thanks All,
Mcka

A copy and paste of a reply I posted just a hour or so ago to the exact same approach used by a poster in [url http://forums.oracle.com/forums/thread.jspa?threadID=636929]this thread.
Yucky code IMO. You are using PL/SQL to drive a nested loop join. Why? SQL is by far more capable of joining tables and is not limited to a using a nested loop approach only.
Also, as you are using PL/SQL to drive it, it means that each cursor fetch in each (nested) FOR loop is a slow context switch from PL/SQL to SQL, in order to ship the row's data from the SQL engine to the PL/SQL engine. Only then to have that very same data shipped back (via yet another context switch) to the SQL engine to be inserted into table4.
This code violates one of the most rudimentary performance principles in Oracle - it is not maximizing SQL and it it not minimizing PL/SQL. It is doing the exact opposite.
As for the ad-hoc commits inside the loop - this does not make much sense. This will generate more redo and more overheads. Also, when something goes pear shape in that process, some rows would have been processed and inserted and committed. Some not.
In this case, how do you expect to restart the failed process at the very first row that failed to be committed and continue processing from there?

Similar Messages

  • How can I run a SQL script file...

    How can I run a SQL script file from a location on my computer without providing the whole path?
    Is there some way I can set a "Working folder" in SQL Plus??
    Thanks!
    Tom

    You can create an environment variable called "SQLPATH" which is a list of directories that SQL*Plus will search for your .SQL
    scripts.
    I would like to use another directory than the oracle/bin...
    How can I do this ??
    Hello,
    U can do this by this way:
    Save odm_script.sql file to the default Oracle
    directory i.e. Oracle-Home/bin and Run following command
    through SQL Plus.
    SQL>@Script_Name
    I hope this will resolve ur problem.
    Regards,
    Omer Saeed Khan.

  • How to use views in sql script report?

    All all
    Can any one tell how to use views in sql script report?

    Most of the views are based on tables (or other views which are based on tables).
    The view typically shows one org at a time based on the context that is set.
    If you need records for all orgs, you need to use the underlying tables. Oracle typically names the tables with a _all suffix.
    e.g. PO_HEADERS will show records for one org at a time. PO_HEADERS_ALL will show records for all orgs.
    Hope this answers your question,
    Sandeep Gandhi

  • When I use AirPlay to mirror a video through my iPad there is a lag at times. Does anyone know how to improve this?

    When I use AirPlay to mirror a video through my iPad there is a lag at times. Does anyone know how to improve this?

    Since I updated my apple TV firmware the other day to 4.4.2 (3160) My video streamed from my iphone 4s loads inconsistently, and more troubling, much more slowly that with the previous version of the software.
    Since I'm not technically, 'mirroring' as you are, on it's face, doesn't seem related. I'm just throwing this out since after looking for comments about radical changes in airplay after this update, I couldn't find much on this.
    Did you install  this latest Apple TV update?  For me it's an  obvious difference. Previously I could start the airplay from my phone and not really have delays for loadiing or buffering. I don't have any other  'load' on my wifi or anything new that could be delaying the link from phone to airpay.
    Anyone?.....

  • How to hide passwords in sql scripts?

    Hi!
    I have got a few sql scripts for some actions. In the scripts the connection information is plain text - even the SYS password in one script.
    Are there ways to solve this behaviour? Perhaps encrypt it or... What does the practice say - how do you solve this???
    Thanks
    Markus

    Depending on your environment and if these are administrative (DBA) scritps or production batch then here are some ideas:
    For DBA
    Run the scripts via cron in a DBA privileged account
    sqlplus /nolog <<EOF
    connect / as sysdba
    start script
    exit
    EOF
    The above requires no password,
    For production batch assuming that all production runs under one OS ID that access to is restricted then potentially you can use OS authentication so the Oracle username has no password: create user bob identified externally.
    sqlplus / @script
    will then do the job
    HTH -- Mark D Powell --

  • How to Achieve this in SQL Query?

    How to Achieve this ?
    I have a table with numeric value populated like this
    create table random_numeral (numerals Number(10));
    insert into random_numeral values (1);
    insert into random_numeral values (2);
    insert into random_numeral values (3);
    insert into random_numeral values (4);
    insert into random_numeral values (5);
    insert into random_numeral values (6);
    insert into random_numeral values (56);
    insert into random_numeral values (85);
    insert into random_numeral values (24);
    insert into random_numeral values (11);
    insert into random_numeral values (120);
    insert into random_numeral values (114);
    Numerals
    1
    2
    3
    4
    5
    6
    56
    85
    24
    11
    120
    114
    I want to display the data as follows
    col1 / col2 / col3
    1 / 2 / 3
    4 / 5 / 6
    11 / 24 / 56
    85 / 114 / 120
    Can anyone Help me?

    I hope there might be some simple way to do this and waiting for experts to reply.
    Try the below query.
    SQL> select * from random_numeral;
      NUMERALS
             1
             2
             3
             4
             5
             6
            56
            85
            24
            11
           120
      NUMERALS
           114
           100
           140
    14 rows selected.
    SQL> select a.numerals ||' / '||b.numerals||' / '||c.numerals from
      2          (select numerals,rownum rn1 from
      3          (
      4              select numerals,mod(row_number() over(partition by 1 order by numerals),3)
      5              from random_numeral
      6          )
      7          where rn=1) a,
      8          (select numerals,rownum rn1 from
      9          (
    10              select numerals,mod(row_number() over(partition by 1 order by numerals),3)
    11              from random_numeral
    12          )
    13          where rn=2) b,
    14          (select numerals,rownum rn1 from
    15          (
    16              select numerals,mod(row_number() over(partition by 1 order by numerals),3)
    17              from random_numeral
    18          )
    19          where rn=0) c
    20  where   a.rn1=b.rn1(+)
    21  and b.rn1=c.rn1(+)
    22  /
    A.NUMERALS||'/'||B.NUMERALS||'/'||C.NUMERALS
    1 / 2 / 3
    4 / 5 / 6
    11 / 24 / 56
    85 / 100 / 114
    120 / 140 /
    SQL>Cheers,
    Mohana

  • Problem while executing script in Toad - How to use '&' in the sql script ?

    I have to execute sql script in toad. Sql script has one insert query in which one insert-value is 'USA & CAN'. When I executed the script in toad by pressing F5, I got a prompt window asking for the value if 'CAN' as it is after the &.
    I tried using[b] {escape '\' } .... but could not resolve the problem.
    Is there any solution or workaround to overcome this problem. I have thousands of records with such values and I have to use sql script only.

    There is an option in TOAD to change this behaviour.
    Look in VIEW/OPTIONS/SQL Editor/
    Uncheck the box for "Scan statements for bound variables before execution".
    In SQL*PLUS it would be SET SCAN OFF
    (desupported version is SET DEFINE OFF)
    Message was edited by:
    Sven Weller

  • How to improve this query speed ?....help me

    How to improve the query speed. Any hints can u suggest in the query or any correction. Here i am using sample tables for checking purpose, When i am trying with my original values, this type of query taking longer time to run
    select ename,sal,comm from emp where(comm is null and &status='ok') or (comm is not null and &status='failed');
    Thanx in advance
    prasanth a.s.

    What about
    select ename,sal,comm from emp where comm is null and &status='ok'
    union all
    select ename,sal,comm from emp where comm is not null and &status='failed';
    Regards
    Vaishnavi

  • How to double dereference in sql scripts

    hi all,
    I wanted to call a sql file(sql2) from within an another sql file(sql1). The call to sql2 from sql1 contains some parameters( in form of integer and some variables). It is possible to access integer values in sql2 but i am unable to access the value stored in the variables in file2.
    How to access the parameter's(which is itself is a variable) value in the called sql script.
    Thanks,
    Shyam

    Depending on your environment and if these are administrative (DBA) scritps or production batch then here are some ideas:
    For DBA
    Run the scripts via cron in a DBA privileged account
    sqlplus /nolog <<EOF
    connect / as sysdba
    start script
    exit
    EOF
    The above requires no password,
    For production batch assuming that all production runs under one OS ID that access to is restricted then potentially you can use OS authentication so the Oracle username has no password: create user bob identified externally.
    sqlplus / @script
    will then do the job
    HTH -- Mark D Powell --

  • Here is one for the tekkies. How to do this in SQL?

    Input : String 541862356
    Table has 2 columns:
    SNO                             SLENGTH
    5418                                4
    54186                               5
    54321                               5
    Output should be 54186.
    We don't have any other input. i.e. we have to get the closest match for the input string.
    How to do this??
    Edited by: user12240205 on May 3, 2012 10:51 PM

    user12240205 wrote:
    We don't have any other input. i.e. we have to get the closest match for the input string.You haven't explained what "closest match" actually means.
    Something as simple as this could do what you want with the sample data you've provided...
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t AS (SELECT 5418 sno, 4 cnt FROM DUAL UNION
      2             SELECT 54186, 5 FROM DUAL UNION
      3             SELECT 54321, 5 FROM DUAL)
      4      ,r as (select 541862356 as r from dual)
      5  --
      6  -- end of test data
      7  --
      8  select max(sno)
      9  from   t,r
    10* where  instr(r, sno) > 0
    SQL> /
      MAX(SNO)
         54186... without the need for any Henry Winkler matching stuff (as we affectionately call it round here).

  • How to transform this PL/SQL request into a PL/SQL function ?

    Hi nice people !
    I have a beautiful flash chart which SQL code is below. I want to display this chart only if this query returns results. I tried to put it as condition. How to transformt this SQL ciode as "SLQ Function returning a boolean" ?
    DECLARE
    X VARCHAR2 (4000);
    BEGIN
    x :=
    'SELECT NULL LINK, trunc(a.DATE1, '''||:P13_TRUNC||'''), avg(b.pourcentage_remplissage)
    FROM    EVV_'||:P13_SITE||' a, bassin_remplissage b
    WHERE   a.CLEF_VAR = (SELECT CLEF_VAR FROM SITE_ECHELLE WHERE SITE='''||:P13_SITE||''')
    AND     round(a.valeur, 1) = b.cote_ngf
    AND     DATE1 BETWEEN
    TO_DATE ('''||:P13_DATE_DEBUT||'000000'', ''DD/MM/YYYYHH24MISS'') AND
    TO_DATE ('''||:P13_DATE_FIN||'235959'', ''DD/MM/YYYYHH24MISS'')
    group by trunc(a.DATE1, '''||:P13_TRUNC||''')
    ORDER BY trunc(a.DATE1, '''||:P13_TRUNC||''')';
    RETURN (X);
    END;Thank you for your kind answers.
    Christian

    Hello Christian,
    >> I don't understand your input
    Your error is most likely because the dynamic select statement you are trying to build is not correct. As local variables, like the ‘x’ you are using, are not persistence, it’s probably hard for you to see the final value of x.
    You should define an application item, like TEMP1, and use it to hold the generated select statement:
    BEGIN
    :TEMP1 := 'SELECT COUNT(*) INTO WWV_FLOW.g_id
    FROM    EVV_'
          || :p13_site
          || ' a, bassin_remplissage b
    WHERE   a.CLEF_VAR = (SELECT CLEF_VAR FROM SITE_ECHELLE WHERE SITE='''
          || :p13_site
          || ''')
    AND     round(a.valeur, 1) = b.cote_ngf
    AND     DATE1 BETWEEN
    TO_DATE ('''
          || :p13_date_debut
          || '000000'', ''DD/MM/YYYYHH24MISS'') AND
    TO_DATE ('''
          || :p13_date_fin
          || '235959'', ''DD/MM/YYYYHH24MISS'')
    group by trunc(a.DATE1, '''
          || :p13_trunc
          || ''')
    ORDER BY trunc(a.DATE1, '''
          || :p13_trunc
          || ''')';
    return false;
    END;Now the process should be evaluated without errors, and TEMP1 should hold the select statement that produces the error. Now, using the developer toolbar, you can inspect the actual select statement ant analyze its syntax and format.
    Once you’ll find the error in the select statement, you can go back to Denes original code proposal.
    Regards,
    Arie.
    Please remember to mark appropriate posts as correct/helpful. For the long run, it will benefit us all.

  • How to directly edit PL/SQL Scripts

    I can't open PL/SQL source files directly into a PL/SQL editor - they're opened as SQL scripts. The tool still seems targeted towards database maintenance rather than developing code held in a separate version control system.
    Whilst technically the PL/SQL source files are just SQL scripts this doesn't make for easy PL/SQL development.
    A change to a PL/SQL source file involves:
    - Open source file into SQL script tab.
    - Choose connection to run script against.
    - Run as a SQL script.
    - Close source file.
    - Navigate to package spec/body in tree browser and open read-only in a view tab.
    - Click to edit object source - opens in new tab.
    - Edit source and compile.
    - Add 'create or replace' at top and '/' at end.
    - Select 'Save As...' and navigate to original source file.
    - Close script window that this automatically opens for newly saved file, and close edit source tab. Same task in PL/SQL developer:
    - Open source file into a Program Window.
    - Edit source and compile.
    - Click save.
    Making a single change is bad enough - try it when you're making substantial changes to a source file and you want to regularly check compilation and save changes.
    Can anyone help make this easier ?

    The tool still seems targeted towards database maintenance rather than developing code held in a separate version control system.They'll get to it eventually...
    I can't open PL/SQL source files directly into a PL/SQL editor - they're opened as SQL scriptsIs the extension .sql? Change it to .pls or .pks/.pkb. They seem to use the dumb extension-based system to find out what files they are dealing with, instead of parsing the contents.
    If the code doesn't get syntax coloured, open a function or procedure from database first, that seems to wake him up.
    Also make sure the files are as the same as in the DB (with create or replace, without final slash), so you can compile them directly from the same file-based editor.
    You still have to assign connection and navigate to the PL/SQL in the DB to view compilation errors and run/debug though. There are some requests on the announced SQL Developer Exchange for these (http://htmldb.oracle.com/pls/otn/f?p=42626:37:260789750723810::NO:::). Please vote for them to see them implemented.
    K.

  • How to Update this T-SQL Table?

    Hello,
    I am having trouble finding the correct syntax for updating this T-SQL table.
    Here is my Query which is adding an additional ' at the beginning and end.
    Query:
        Update [denodo].[dbo].[wrappers]
      SET wrapperWhereClause = '''AND County_Input=''Cheyenne & Arapaho Tribal Court'' AND WrapperID=729'''
      WHERE wrapperID = '729'
    The Outcome:
    'AND County_Input='Cheyenne & Arapaho Tribal Court' AND WrapperID=729'
    The Outcome I am hoping for:
    AND County_Input='Cheyenne & Arapaho Tribal Court' AND WrapperID=729
    Can anyone help me edit this so I can receive the outcome I am hoping for?

    >> Please learn
    I am having trouble finding the correct syntax for updating this T-SQL table. Here is my Query [sic] which is adding an additional ' at the beginning and end. <<
    Please learn basic terms so
    we can communicate. UPDATE is a statement and not a query. You do not know ISO-11179 rules, etc.
    Why are you building statements in SQL? You are doing bad SQL programming
    and need to stop. Your silly meta-data “wrapper_where_clause” and “county_input” is so fundamentally wrong I would fire you. Look at what I have done
    with SQL and see what a criticism that is!
    UPDATE Wrappers
    SET ??
    WHERE county_something =
    'Cheyenne & Arapaho Tribal Court'
    AND wrapper_id = '729';
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • How to improve this timer code ??

    Dear Sir:
    I have following code, it can works and if I remove the line "new JFrame("New Test Timer").setVisible(true);"
    Then It did not show up timer message,
    What is wrong here??
    I want to remove this line but still keep timer moving,
    How to do this??
    Thanks
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    public class TimerSample {
      public static void main(String args[]) {
        new JFrame("New Test Timer").setVisible(true);
        ActionListener actionListener = new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Hello World Timer");
        Timer timer = new Timer(1000, actionListener);
        timer.start();
    }

    Swing timers are called in the event dispatch thread. If you don't start the event dispatch thread (by setting your GUI visible) then they don't get called.
    If you want a timer but you don't want a GUI, then use a java.util.Timer.

  • How to do this in sql?

    m doing a quarterly report and my database only collects the year and month fields.
    i currently have this condition in my periodic/monthly report
    WHERE
    period= '@Request.Period~' and
    year = '@Request.Year~'
    how do you do this so that when the user inputs quarter 1, the condition becomes "from period 1 to 3". when the user inputs quarter 2, the condition becomes "from period 4 to 6" etc?

    >well, the users input would only be 1,2,3,4 which stands for the quarter.
    I misunderstood your original post. Please try;
    old: with t as (
       select to_date('01-JAN-2007','DD-MON-YYYY') dt from dual union all
       select to_date('01-FEB-2007','DD-MON-YYYY') dt from dual union all
       select to_date('28-FEB-2007','DD-MON-YYYY') dt from dual union all
       select to_date('31-MAR-2007','DD-MON-YYYY') dt from dual union all
       select to_date('01-APR-2007','DD-MON-YYYY') dt from dual union all
       select to_date('30-APR-2007','DD-MON-YYYY') dt from dual)
    select dt
    from t
    where trunc(dt,'Q')= add_months(trunc(to_date('&year','YYYY'),'y'),(&quarter-1)*3)
    new: with t as (
       select to_date('01-JAN-2007','DD-MON-YYYY') dt from dual union all
       select to_date('01-FEB-2007','DD-MON-YYYY') dt from dual union all
       select to_date('28-FEB-2007','DD-MON-YYYY') dt from dual union all
       select to_date('31-MAR-2007','DD-MON-YYYY') dt from dual union all
       select to_date('01-APR-2007','DD-MON-YYYY') dt from dual union all
       select to_date('30-APR-2007','DD-MON-YYYY') dt from dual)
    select dt
    from t
    where trunc(dt,'Q')= add_months(trunc(to_date('2007','YYYY'),'y'),(1-1)*3)
    SQL> with t as (
       select to_date('01-JAN-2007','DD-MON-YYYY') dt from dual union all
       select to_date('01-FEB-2007','DD-MON-YYYY') dt from dual union all
       select to_date('28-FEB-2007','DD-MON-YYYY') dt from dual union all
       select to_date('31-MAR-2007','DD-MON-YYYY') dt from dual union all
       select to_date('01-APR-2007','DD-MON-YYYY') dt from dual union all
       select to_date('30-APR-2007','DD-MON-YYYY') dt from dual)
    select dt
    from t
    where trunc(dt,'Q')= add_months(trunc(to_date('2007','YYYY'),'y'),(1-1)*3)
    DT      
    01-JAN-07
    01-FEB-07
    28-FEB-07
    31-MAR-07
    4 rows selected.

Maybe you are looking for