How to optimize the select query executed in a cursor for loop?

Hi friends,

I run the code below and clocked at the same time for each line of code using DBMS_PROFILER.
CREATE OR REPLACE PROCEDURE TEST
AS
   p_file_id              NUMBER                                   := 151;
   v_shipper_ind          ah_item.shipper_ind%TYPE;
   v_sales_reserve_ind    ah_item.special_sales_reserve_ind%TYPE;
   v_location_indicator   ah_item.exe_location_ind%TYPE;

   CURSOR activity_c
   IS
      SELECT *
        FROM ah_activity_internal
       WHERE status_id = 30
         AND file_id = p_file_id;
BEGIN
   DBMS_PROFILER.start_profiler ('TEST');

   FOR rec IN activity_c
   LOOP
      SELECT DISTINCT shipper_ind, special_sales_reserve_ind, exe_location_ind
                 INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
                 FROM ah_item --464000 rows in this table
                WHERE item_id_edw IN (
                         SELECT item_id_edw
                           FROM ah_item_xref --700000 rows in this table
                          WHERE item_code_cust = rec.item_code_cust
                            AND facility_num IN (
                                   SELECT facility_code
                                     FROM ah_chain_div_facility --17 rows in this table
                                    WHERE chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
                                      AND div_id = (SELECT div_id
                                                      FROM ah_div --8 rows in this table 
                                                     WHERE division = rec.division)));
   END LOOP;

   DBMS_PROFILER.stop_profiler;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      NULL;
   WHEN TOO_MANY_ROWS
   THEN
      NULL;
END TEST;
The SELECT inside the LOOP FOR cursor query took 773 seconds.
I tried to use COLLECT in BULK instead of a cursor for loop, but it did not help.
When I took the select query separately and executed with a value of the sample, and then he gave the results in a Flash of a second.

All tables have primary key index.
Any ideas what can be done to make this code more efficient?

Thank you
Raj.
DECLARE
  v_chain_id ah_chain_div_facility.chain_id%TYPE := ah_internal_data_pkg.get_chain_id (p_file_id);

  CURSOR cur_loop IS
  SELECT * -- better off explicitly specifying columns
  FROM ah_activity_internal aai,
  (SELECT DISTINCT aix.item_code_cust, ad.division, ai.shipper_ind, ai.special_sales_reserve_ind, ai.exe_location_ind
     INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
     FROM ah_item ai, ah_item_xref aix, ah_chain_div_facility acdf, ah_div ad
    WHERE ai.item_id_edw = aix.item_id_edw
      AND aix.facility_num = acdf.facility_code
      AND acdf.chain_id = v_chain_id
      AND acdf.div_id = ad.div_id) d
  WHERE aai.status_id = 30
    AND aai.file_id = p_file_id
    AND d.item_code_cust = aai.item_code_cust
    AND d.division = aai.division;         

BEGIN
  FOR rec IN cur_loop LOOP
    ... DO your stuff ...
  END LOOP;
END;  

Published by: Dave hemming on December 4, 2008 09:17

Tags: Database

Similar Questions

  • How to write the select query for it

    Hello

    I had an html form and the area I drop down and he needs to select several values in the drop-down box. When I select multiple values then I have to write the query to SQL select statement.

    When I try to write the select statement and trying to run I get the error message.

    Select * from Table

    where emo_no = '1,2,3 '.

    That's how I write the query please suggest me how to write the query to select several values in the drop-down box.

    Thank you

    Use the keyword sql 'in '.  If you don't know how, I've heard good things about the book Teach Yourself SQL in 10 Minutes by Ben Forta.

  • How to optimize the query with a join of virtual tables

    I'm working on a query that is get the data of virtual tables 2 and b
    one is formed by the Union, all say 4 queries and b is formed by the Union, all say 3 queries
    then these two virtual tables and b are joined on a column common and data are extracted from their part.
    Problem is that there is about 1 minutes each in the two virtual tables has and b. If individual a and b queries virtual takes about 5 seconds to retrieve data
    but the join on column takes about 25 seconds to retrieve data.
    Can someone guide me how to optimize the recovery of joining 2 virtual tables having large data

    Thank you

    Please read these:

    When your query takes too long
    When your query takes too long...

    How to post a SQL tuning request
    HOW to: Validate a query of SQL statement tuning - model showing

  • How to store results of the select query in the tables.

    I created a variable varray type and now want to assign some data of output of the select query in pl/SQL, as well as in reports 6i.

    You are in the wrong forum (this is for problems with the SQL Developer tool). You were the one where you have published first on the right, but not to reuse independent threads as you did.

    Kind regards
    K.

  • as with the select query

    Hello

    I have a query something like...

    Select...

    ....

    where

    b.ProdName as 'GEAR of the TREE %' or b.prodname like '% FRONT GEAR %' or b.prodname like '% BREAK 4 %k '.

    and

    b.ProdName as

    (select the name of the society of companyms

    where < some comdition >

    )

    order of 3.6

    I have blocked in:

    b.ProdName as

    (select the name of the society of companyms

    where < some comdition >

    )

    How to give a select query with like operator.

    In above query I want to b.prodname as above and the name of the company for which I give condition.  Company names are stored in another table and I want to select and use them with like operator.

    Thank you.

    Hello

    It is not clear what you want to do.

    One possibility is an EXISTS subquery, like this:

    ...

    WHERE (b.prodname LIKE "% of the CYCLE of the TREE" - begins with this text

    OR b.prodname LIKE '% FRONT GEAR %' - contains this text

    OR b.prodname LIKE '% BREAK 4 %k' - contains this text

    )

    AND THERE ARE)

    SELECT 1

    OF companynms

    WHERE--a condition

    AND b.prodname AS business

    )

    I hope that answers your question.
    If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

  • The select query-based search page...

    I want to create a basic search page at the base on a select query.

    The select query will not be based on Table.Its based on functions.

    I created a view like this object

    Select xx_test_accruals(:p_date) of ACCUMULATION: p_date double DT

    I created a search page

    In the area of provision of Home Page, I added a region of the query. In this region of request I created a result based research Table.

    When I run the page opens with the field

    DT

    When I get back the date and click go, I get the error message like this...

    oracle.apps.fnd.framework.OAException: oracle.jbo.SQLStmtException: 27122 Houston: SQL error in the preparation of the statement. Statement: SELECT * FROM (select xx_test_accruals(:p_date) of ACCUMULATION: p_date double DT) QRSLT WHERE ((UPPER (DT) like: 1 AND (DT as: 2 OR DT as: 3 OR DT as: 4 OR DT as: 5)))



    Help, please

    Published by: [email protected] on October 4, 2009 14:04

    Hello

    I tried it on my machine... There is a little change on the approach.

    See we have a display object: XxAccrualsSummaryVO

    the query is

    Select xx_test_accruals (to_date (: 1, 'YYYY-MM-DD')) DT ACCRUAL, double to_date(:2,'YYYY-MM-DD')

    Create a view property object: say DateTestPVO and create a transitional DATE type attribute say dated. MinValue. How to create the View Object(read in dev guide) property.

    Link the date to DateTestPVO text entry and the date attribute. MinValue.

    Now that the page will load in process request initialize the PVO not XxAccrualsSummaryVO.

    Public Sub initialisePVO()
    {

    OAViewObject vo = (OAViewObject) getDatePVO1 ();
    If (! vo.isPreparedForExecution ())
    {
    vo.setMaxFetchSize (0);
    }
    Line = vo.createRow ();
    vo.insertRow (row);
    row.setNewRowState (Row.STATUS_INITIALIZED);

    }

    And when you press the Go button and then enter the date entered and set the location where the XxAccrualsSummaryVO clause.

    If (PageContext.GetParameter ("Go")! = null)
    {
    Get the value entered by the user in the date field.

    DateField OAMessageDateFieldBean = (OAMessageDateFieldBean) webBean.findIndexedChildRecursive ("text");
    String datetest = (String) dateField.getValue (pageContext) m:System.NET.SocketAddress.ToString ();
    Now set the where clause VO settings.
    OAViewObject vodate = (OAViewObject) am.findViewObject ("invoicetestVO1");
    vodate.setWhereClauseParams (null);
    vodate.setWhereClauseParam(0,datetest);
    vodate.setWhereClauseParam(1,datetest);
    vodate.executeQuery ();
    }

    It works perfectly... I tested it.

    Thank you
    Gerard

  • How to convert the hierarchical query of SQL Server (CTE) to Oracle?

    How to convert the hierarchical query of SQL Server (CTE) to Oracle?

    WITH cte (col1, col2) AS
    (
    SELECT col1, col2
    FROM dbo. [tb1]
    WHERE col1 = 12
    UNION ALL
    SELECT c.col1, c.col2
    FROM dbo. [tb1] AS c INNER JOIN cte AS p ON c.col2 = p.col1
    )
    DELETE one
    FROM dbo. [tb1] AS an INNER JOIN b cte
    ON a.col1 = b.col1

    Hello
    Something like this maybe:

    DELETE FROM dbo.tb1 a
     WHERE EXISTS (
      SELECT 1
        FROM dbo.tb1 b
      WHERE a.co11 = b.col1
          AND a.col2 = b.col2
       START WITH b.col1 = 12
      CONNECT BY b.col2 = PRIOR b.col1)
    

    Although you need to do here is to check that CONNECT it BY SELECT, returns records you wait first, then the DELETION should work too.

  • passing parameters to the select query in DB link to call him...

    I have a DB link that executes the select query based on user specified parameter.

    Now, I created an invoke activity and specified input [which must be passed to the query] and output parameters [it is the result of the query]. That invoke link activity DB calls in which a parameter was created to be used in the select query. So my question is, DB link partner take the input parameter of call activity automatically or do we need to specify a mapping for this?

    When you create a partnerlink db the setting that you create for your chosen matches the entry and when you create the variable entry in the invoke for this partnerlink is created with the type of the parameter.

    Create an assignment before your invoke and assign a value to the input variable that you created in the invoke. This value will be passed and used as a parameter in the selection.

    Heidi.

  • How to get the 'selection' illuminated

    I have a vista 64. I had a few printing problem which I found out wasa because google chrom. I had one tech outside and the problem has been corrected. However, I have an impression left of problem that I hope someone can help me with. I'm trying to print part of a page. I am the part children and then go to the print page. However, I cannot print print the part because the only 'all' and 'pages' are turned on. "selection is not lit, so I can't click. Can someone tell me how to recover the 'selection '.

    Hey, Dolores,.

    Your computer is configured to display pages in full screen? When this happens press F11 on your keyboard and see if the button reduce reappears.

  • How to get the selected row in a programmatic table of the ADF (table generated from a bean)

    Hi all


    We use JDeveloper Studio Edition Version 12.1.3.0.0 and deployed on GlassFish Server Open Source Edition 3.1.2.2 (build 5) and connect to the SQLServer database.


    How to get the selected line in a programmatic (using a SortableModel custom not a display object) ADF table (generated from a bean)?


    We are trying to get the selected row in a bean of a programmatic ADF table to retrieve data based on the selected line.


    Any idea?

    Well, you can use selectionListener to set the selected line to a variable of bean (but this bean must be in extended view or some superiors)

    Something like this: http://www.awasthiashish.com/2015/07/get-selected-row-singlemultiple-from.html

    Dario

  • How to use the selection tool to resize a text box? Before I was able to click on the text box with the selection too in order to resize, but now I click on it and it only gives me 'path' or 'anchor' options.

    How to use the selection tool to resize a text box? Before I was able to click on the text box with the selection too in order to resize, but now I click on it and it only gives me 'path' or 'anchor' options.

    Ashley,

    What about window > show the rectangle enclosing (Ctrl / Cmd + Shift + B to toggle)?

  • DVT:pivotFilterBar - how to get the selected values of the filter

    Hi all

    I have a question: how to get the selected values from the pivot table filter bar programmatically?

    I tried to use
    pivotTable.getDataModel().getDataAccess().getValueQDR(startRow, startCol, DataAccess.QDR_WITH_PAGE);
    but to the edge of the side DATA INCORRECTESdeclarations page, it seems that it will return the cached values.

    Environment: JDev 11.1.1.3.0 without tasks.

    Thank you
    Miroslaw

    Hello

    You can retrieve the value selected in the PivotFilterBar through the PivotFilterBar model, instead of dataaccess:

    Download the template of the bar pivot filter instance
    QueryDescriptior queryDescriptor = (QueryDescriptor) pivotFilterBar.getValue ();

    retrieve a list of criterion, each of them is used to fill each lov in the pivot filter bar
    ConjunctionCriterion conjunctionCriterion = queryDescriptor.getConjunctionCriterion ();
    List criterionList = conjunctionCriterion.getCriterionList ();
    for (int i = 0; i)<_criterionList.size(); i++)="">
    AttributeCriterion = (AttributeCriterion) criterionList.get (i) criterion.

    _selected is the currently selected value
    Selected object = criterion.getValues () .get (0);

    System.out.println (_selected);
    }

    Hope that helps,
    Chadwick

  • How to retrieve the selected values of the selectOrderShuttle using ADF 11 g

    Hello world

    Did anyone has idea how to retrieve the selected items by using the shuttle and the order of the elements by using the 'SelectOrderShuttle '?

    Thank you

    valuechangeevent of the shuttle would fire when you shuttle points back.

        public void selectOrderShuttle1_valueChangeListener(ValueChangeEvent valueChangeEvent) {
    
            ArrayList list = new ArrayList(Arrays.asList(valueChangeEvent.getNewValue()));
    
            if (list != null){
                for (int i=0; i
    
  • I have a LOV custom, but I don't know how to recover the selection?

    Hi all
    I want to recover my lov user selection, but I don't know much about the syntax.
    Please advice.
    Currently, I have attached a script to a button so that I can bring out a LOV.

    ______________________________________________________________


    DECLARE BOOLEAN a_value_chosen;

    BEGIN
    Message ("LOV is gg to pop up.");
    a_value_chosen: = Show_Lov ('LOV27');

    IF this is a_value_chosen
    Message ("you have not selected a value.");
    Bell; RAISE Form_Trigger_Failure;
    ON THE OTHER
    / * I don't know how to recover the selection of the user here... * /.
    Message ("you selected a value!' |") SELECTION);

    END IF;
    END;

    There is another way to get your selection of LOV... you can not use this concept...
    You must return value LOV to some parameter/item, while creating LOV. in order to check if its recoveries or not...

    Suppose that you have returned the value to a parameter named RETURN_VAL then...

    DECLARE
      A_VALUE_CHOSEN BOOLEAN;
    BEGIN
      MESSAGE('LOV IS GG TO POP UP.');
      A_VALUE_CHOSEN := SHOW_LOV('LOV27');
    
    IF :PARAMETER.RETURN_VAL IS NOT NULL THEN
      MESSAGE('YOU HAVE NOT SELECTED A VALUE.');
      BELL; RAISE FORM_TRIGGER_FAILURE;
    ELSE
      MESSAGE('YOU HAVE SELECTED A VALUE!!!' || :PARAMETER.RETURN_VAL);
    END IF;
    END;
    

    -Aamir Arif

  • How to get the selected DataGrid item?

    Hello

    I have a datagrid

    < mx:DataGrid = "10" XY = dataProvider "36" = "{acEmaillist}" width = "319" >
    < mx:columns >
    < mx:DataGridColumn headerText = "Full name" dataField = "cn" width = "150" / >
    < mx:DataGridColumn headerText = "Email" dataField = "mail" width = "150" / >
    < / mx:columns >
    < / mx:DataGrid >

    I want to select a name, click it, get the selected name and save it in a string variable. Please let me know how to get the selected name when I click it. Thank you very much.

    M

    This code can help.


    http://www.Adobe.com/2006/mxml"width ="100% ">

    Import mx.controls.DataGrid;
    Import mx.events.ListEvent;

    [Bindable]
    private var nameStr:String;

    public void getName(event:ListEvent):void {}
    var dg:DataGrid = DataGrid (event.target);
    nameStr = dg.selectedItem.cn;
    }
    ]]>



    Bob Smith
    [email protected]


    Ted Alan
    [email protected]


    Fred Tobs
    [email protected]








Maybe you are looking for