Optimize this

Hi,
how can we optimize this code?
  SELECT mara~matnr  "Material NO
         matkl  "Material group
         wrkst  "Basic Material
         prdha  "Product hierarchy
         marc~werks   "Plant
         fevor   "Production Scheduler
         dismm   "MRP Type
         dispo   "MRP Controller
         plifz   "planned delivery time in days
         ekgrp   "Purchasing Group
         beskz   "Procurement Type
         prctr   "Profit Center
         FROM mara INNER JOIN marc
         ON mara~matnr = marc~matnr
         INTO TABLE itab_marc
         WHERE mara~matnr IN s_matnr
         AND werks IN s_plant
         AND fevor IN s_schdlr
         AND dispo IN s_mrp
         AND ekgrp IN s_purgrp
         AND beskz IN s_prtype
         AND prdha IN s_proj.
  IF sy-subrc EQ 0.
    SORT itab_marc BY matnr werks fevor dispo ekgrp ekgrp beskz.
  ENDIF.
  IF itab_marc[] IS NOT INITIAL.
    SELECT matnr  "Material No
           bwkey  "Plant
           bwtar
           vprsv  "Price control indicator
           verpr  "Moving Average Price/Periodic Unit Price
           stprs  "Standard price
           peinh  "Price unit
           FROM mbew
           INTO TABLE itab_mbew
           FOR ALL ENTRIES IN itab_marc
          WHERE matnr EQ itab_marc-matnr
          AND bwkey EQ itab_marc-werks.
    IF sy-subrc EQ 0.
      SORT itab_mbew BY matnr bwkey.
    ENDIF.
  ENDIF.
  IF itab_marc[] IS NOT INITIAL.
    SELECT   matnr     "Material No
             werks     "Plant
             lgort     "Storage Location
             labst     "Valuated stock with unrestricted use
             umlme     "Stock in transfer
             insme     "Stock in quality inspection
             einme     "Total Stock of All Restricted Batches
             speme     "blocked state
             retme     "Blocked state return
             klabs     "Unrestricted-use consignment stock
             kinsm     "Consignment stock in quality inspection
             FROM mard
             INTO TABLE itab_mard
             FOR ALL ENTRIES IN itab_marc
             WHERE matnr EQ itab_marc-matnr
             AND werks   EQ itab_marc-werks.
    IF sy-subrc EQ 0.
      SORT itab_mard BY matnr werks.
    ENDIF.
  ENDIF.
Kate

I have 2 proposals.
First to make it more readable, write mara as a marc as b and denote each field with a~ or b~ in field list and where condition.
You should sort the internal tables by the full key, i.e. also bwtar and also lgort.
And if possible use sorted tables with unique keys.
Siegfried

Similar Messages

  • Trying to optimize this simple query

    Hi,
    I am trying to optimize this simple query but the two methods I am trying actually make things worse.
    The original query is:
    SELECT customer_number, customer_name
    FROM bsc_pdt_account_mv
    where rownum <= 100
    AND Upper(customer_name) like '%SP%'
    AND customer_id IN
    SELECT cust_id FROM bsc_pdt_assoc_sales_force_mv
    WHERE area_identifier IN (
    SELECT area_identifier FROM bsc_pdt_assoc_sales_force_mv
    WHERE ad_identifier = '90004918' or rm_identifier = '90004918' or tm_identifier = '90004918'
    The result set of this query returns me the first 100 rows in 88 seconds and they are all distinct by default (don't know why they are distinct).
    My first attempt was to try to use table joins instead of the IN conditions:
    SELECT
    distinct -- A: I need to use distinct now
    customer_number, customer_name
    FROM bsc_pdt_account_mv pdt,
    bsc_pdt_assoc_sales_force_mv asf,
    SELECT distinct area_identifier FROM bsc_pdt_assoc_sales_force_mv
    WHERE ad_identifier = '90004918' or rm_identifier = '90004918' or tm_identifier = '90004918'
    ) area
    where
    area.area_identifier = asf.area_identifier
    AND asf.cust_id = pdt.customer_id
    AND Upper(customer_name) like '%SP%'
    AND rownum <= 100 -- B: strange when I comment this out
    order by 1
    I dont understand two things with this query. First issue, I now need to put in the distinct because the result set is not distinct by default. Second issue (very strange), when I put the rownum condition (<100) I get two rows in 1.5 seconds. If I remove the condition, I get 354 rows (whole result set) in 326 seconds.
    My second attempt was to use EXISTS instead of IN:
    SELECT
    customer_number, customer_name
    FROM bsc_pdt_account_mv pdt
    where Upper(customer_name) like '%SP%'
    AND rownum <= 100
    AND EXISTS
    select 1 from
    bsc_pdt_assoc_sales_force_mv asf,
    SELECT distinct area_identifier FROM bsc_pdt_assoc_sales_force_mv
    WHERE ad_identifier = '90004918' or rm_identifier = '90004918' or tm_identifier = '90004918'
    ) area
    where
    area.area_identifier = asf.area_identifier
    AND asf.cust_id = pdt.customer_id
    This query returns a similar distinct result set as teh original one but takes pretty much the same time (87 seconds).

    The query below hangs when run in TOAD or PL/SQL Dev. I noticed there is no rows returned from the inner table for this condition.
    SELECT customer_number, customer_name
    FROM
    bsc_pdt_account_mv pdt_account
    where rownum <= 100
    AND exists (
    SELECT pdt_sales_force.cust_id
    FROM bsc_pdt_assoc_sales_force_mv pdt_sales_force
    WHERE pdt_account.customer_id = pdt_sales_force.cust_id
    AND (pdt_sales_force.rm_identifier = '90007761' or pdt_sales_force.tm_identifier = '90007761') )
    ORDER BY customer_name
    -- No rows returned by this query
    SELECT pdt_sales_force.cust_id
    FROM bsc_pdt_assoc_sales_force_mv pdt_sales_force
    WHERE pdt_sales_force.rm_identifier = '90007761' or pdt_sales_force.tm_identifier = '90007761'

  • How to optimize this select statement  its a simple select....

    how to optimize this select statement  as the records in earlier table is abt i million
    and this simplet select statement is not executing and taking lot of time
      SELECT  guid  
                    stcts      
      INTO table gt_corcts
      FROM   corcts
      FOR all entries in gt_mege
      WHERE  /sapsll/corcts~stcts = gt_mege-ctsex
      and /sapsll/corcts~guid_pobj = gt_Sagmeld-guid_pobj.
    regards
    Arora

    Hi Arora,
    Using Package size is very simple and you can avoid the time out and as well as the problem because of memory.  Some time if you have too many records in the internal table, then you will get a short dump called TSV_TNEW_PAGE_ALLOC_FAILED.
    Below is the sample code.
    DATA p_size = 50000
    SELECT field1 field2 field3
       INTO TABLE itab1 PACKAGE SIZE p_size
       FROM dtab
       WHERE <condition>
    Other logic or process on the internal table itab1
    FREE itab1.
    ENDSELECT.
    Here the only problem is you have to put the ENDSELECT.
    How it works
    In the first select it will select 50000 records ( or the p_size you gave).  That will be in the internal table itab1.
    In the second select it will clear the 50000 records already there and append next 50000 records from the database table.
    So care should be taken to do all the logic or process with in select and endselect.
    Some ABAP standards may not allow you to use select-endselect.  But this is the best way to handle huge data without short dumps and memory related problems. 
    I am using this approach.  My data is much more huge than yours.  At an average of atleast 5 millions records per select.
    Good luck and hope this help you.
    Regards,
    Kasthuri Rangan Srinivasan

  • Performance Tuning Issues  ( How to Optimize this Code)

    _How to Optimize this Code_
    FORM MATL_CODE_DESC.
      SELECT * FROM VBAK WHERE VKORG EQ SAL_ORG AND
                               VBELN IN VBELN AND
                               VTWEG IN DIS_CHN AND
                               SPART IN DIVISION AND
                               VKBUR IN SAL_OFF AND
                               VBTYP EQ 'C' AND
                               KUNNR IN KUNNR AND
                               ERDAT BETWEEN DAT_FROM AND DAT_TO.
        SELECT * FROM VBAP WHERE VBELN EQ VBAK-VBELN AND
                                 MATNR IN MATNR.
          SELECT SINGLE * FROM MAKT WHERE MATNR EQ VBAP-MATNR.
          IF SY-SUBRC EQ 0.
            IF ( VBAP-NETWR EQ 0 AND VBAP-UEPOS NE 0 ).
              IF ( VBAP-UEPVW NE 'B' AND VBAP-UEPVW NE 'C' ).
                MOVE VBAP-VBELN TO ITAB1-SAL_ORD_NUM.
                MOVE VBAP-POSNR TO ITAB1-POSNR.
                MOVE VBAP-MATNR TO ITAB1-FREE_MATL.
                MOVE VBAP-KWMENG TO ITAB1-FREE_QTY.
                MOVE VBAP-KLMENG TO ITAB1-KLMENG.
                MOVE VBAP-VRKME TO ITAB1-FREE_UNIT.
                MOVE VBAP-WAVWR TO ITAB1-FREE_VALUE.
                MOVE VBAK-VTWEG TO ITAB1-VTWEG.
                MOVE VBAP-UEPOS TO ITAB1-UEPOS.
              ENDIF.
            ELSE.
              MOVE VBAK-VBELN TO ITAB1-SAL_ORD_NUM.
              MOVE VBAK-VTWEG TO ITAB1-VTWEG.
              MOVE VBAK-ERDAT TO ITAB1-SAL_ORD_DATE.
              MOVE VBAK-KUNNR TO ITAB1-CUST_NUM.
              MOVE VBAK-KNUMV TO ITAB1-KNUMV.
             SELECT SINGLE * FROM KONV WHERE KNUMV EQ VBAK-KNUMV AND
                                             KSTEU = 'C' AND
                                             KHERK EQ 'A' AND
                                             KMPRS = 'X'.
             IF SY-SUBRC EQ 0.
               ITAB1-REMARKS = 'Manual Price Change'.
             ENDIF.
              SELECT SINGLE * FROM KONV WHERE KNUMV EQ VBAK-KNUMV AND
                                              KSTEU = 'C' AND
                                              KHERK IN ('C','D') AND
                                              KMPRS = 'X' AND
                                              KRECH IN ('A','B').
              IF SY-SUBRC EQ 0.
                IF KONV-KRECH EQ 'A'.
                  MOVE : KONV-KSCHL TO G_KSCHL.
                  G_KBETR = ( KONV-KBETR / 10 ).
                  MOVE G_KBETR TO G_KBETR1.
                  CONCATENATE G_KSCHL G_KBETR1 '%'
                              INTO ITAB1-REMARKS SEPARATED BY SPACE.
                ELSEIF KONV-KRECH EQ 'B'.
                  MOVE : KONV-KSCHL TO G_KSCHL.
                  G_KBETR = KONV-KBETR.
                  MOVE G_KBETR TO G_KBETR1.
                  CONCATENATE G_KSCHL G_KBETR1
                              INTO ITAB1-REMARKS SEPARATED BY SPACE.
                ENDIF.
              ELSE.
                ITAB1-REMARKS = 'Manual Price Change'.
              ENDIF.
              CLEAR : G_KBETR, G_KSCHL,G_KBETR1.
              MOVE VBAP-KWMENG TO ITAB1-QTY.
              MOVE VBAP-VRKME TO ITAB1-QTY_UNIT.
              IF VBAP-UMVKN NE 0.
                ITAB1-KLMENG = ( VBAP-UMVKZ / VBAP-UMVKN ) * VBAP-KWMENG.
              ENDIF.
              IF ITAB1-KLMENG NE 0.
                VBAP-NETWR = ( VBAP-NETWR / VBAP-KWMENG ).
                MOVE VBAP-NETWR TO ITAB1-INV_PRICE.
              ENDIF.
              MOVE VBAP-POSNR TO ITAB1-POSNR.
              MOVE VBAP-MATNR TO ITAB1-MATNR.
              MOVE MAKT-MAKTX TO ITAB1-MAKTX.
            ENDIF.
            SELECT SINGLE * FROM VBKD WHERE VBELN EQ VBAK-VBELN AND
                                            BSARK NE 'DFUE'.
            IF SY-SUBRC EQ 0.
              ITAB1-INV_PRICE = ITAB1-INV_PRICE * VBKD-KURSK.
              APPEND ITAB1.
              CLEAR ITAB1.
            ELSE.
              CLEAR ITAB1.
            ENDIF.
          ENDIF.
        ENDSELECT.
      ENDSELECT.
    ENDFORM.                               " MATL_CODE_DESC

    Hi Vijay,
    You could start by using INNER JOINS:
    SELECT ......
                FROM (                    VBAK
                             INNER JOIN VBAP
                                           ON VBAPVBELN = VBAKVBELN
                             INNER JOIN MAKT
                                           ON MAKTMATNR = VBAPMATNR AND
                                                 MAKT~SPRAS = SYST-LANGU )
                INTO TABLE itab
              WHERE VBAK~VBELN IN VBELN
                   AND VBAK~VTWEG IN DIS_CHN
                   AND VBAK~SPART IN DIVISION
                   AND VBAK~VKBUR IN SAL_OFF
                   AND VBAK~VBTYP EQ 'C'
                   AND VBAK~KUNNR IN KUNNR
                   AND VBAK~ERDAT BETWEEN DAT_FROM AND DAT_TO
                   AND VBAP~NETWR EQ 0
                   AND VBAP~UEPOS NE 0
    Regards,
    John.

  • I don't know how I can optimize this SQL

    Dear ALL:
    I don't know how I can optimize this SQL.
    Is it possible to make a better SQL or PL/SQL?
    Please let me know your good thought.
    Thank you.
    Sincerely,
    ===========================================================
    (SELECT     A, B, C, SUM(D) as D, AVG(E) as E
    FROM      T1, T2
    WHERE     T1.timestamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
    AND TO_DATE('01:00:00','HH24:MI:SS')
    GROUP BY A, B, C
    UNION ALL
    (SELECT     A, B, C, SUM(D) as D, AVG(E) as E
    FROM      T1, T2
    WHERE     T1.timestamp BETWEEN TO_DATE('01:00:01','HH24:MI:SS')
    AND TO_DATE('02:00:00','HH24:MI:SS')
    GROUP BY A, B, C
    UNION ALL
    (SELECT     A, B, C, SUM(D) as D, AVG(E) as E
    FROM      T1, T2
    WHERE     T1.timestamp BETWEEN TO_DATE('02:00:01','HH24:MI:SS')
    AND TO_DATE('03:00:00','HH24:MI:SS')
    GROUP BY A, B, C
    UNION ALL
    (SELECT     A, B, C, SUM(D) as D, AVG(E) as E
    FROM      T1, T2
    WHERE     T1.timestamp BETWEEN TO_DATE('03:00:01','HH24:MI:SS')
    AND TO_DATE('04:00:00','HH24:MI:SS')
    GROUP BY A, B, C
    ORDER BY A ASC, B ASC, C ASC
    ===========================================================

    Dear Warren:
    Actually, for this query, it takes a few second to complete the query.
    But, I have to create several time period and I have to optimize the SQL.
    Time period is 1 hour, 30 min, 15 min, 5 min.
    That is,
    ===========================================================
    WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
    AND TO_DATE('01:00:00','HH24:MI:SS')
    UNION ALL
    WHERE T1.timestamp BETWEEN TO_DATE('23:00:01','HH24:MI:SS')
    AND TO_DATE('24:00:00','HH24:MI:SS')
    ===========================================================
    WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
    AND TO_DATE('00:30:00','HH24:MI:SS')
    UNION ALL
    WHERE T1.timestamp BETWEEN TO_DATE('23:30:01','HH24:MI:SS')
    AND TO_DATE('24:00:00','HH24:MI:SS')
    ===========================================================
    WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
    AND TO_DATE('00:15:00','HH24:MI:SS')
    UNION ALL
    WHERE T1.timestamp BETWEEN TO_DATE('23:45:01','HH24:MI:SS')
    AND TO_DATE('24:00:00','HH24:MI:SS')
    ===========================================================
    WHERE T1.timstamp BETWEEN TO_DATE('00:00:00','HH24:MI:SS')
    AND TO_DATE('00:05:00','HH24:MI:SS')
    UNION ALL
    WHERE T1.timestamp BETWEEN TO_DATE('23:55:01','HH24:MI:SS')
    AND TO_DATE('24:00:00','HH24:MI:SS')
    ===========================================================
    Do you know how this SQL is optimized?
    Thank you,

  • How can i optimize this SQL

    Is there anyway i could optimize the below mentioned SQL. Since our test DB is down now, i'll be posting the EXPLAIN PLAN later.
    Is there anything i could do syntactically to optimize this sql?
    SELECT SUBSTR(STK_INF.TASK_GENRTN_REF_NBR,1,12) AS PICK_WAVE_NBR,
                     ( CASE WHEN ( SUM(STK_INF.QTY_ALLOC) > 0 ) THEN
            ROUND ( (SUM(CASE WHEN (STK_INF.NEXT_TASK_ID = 0 AND STK_INF.ALLOC_INVN_DTL_ID = 0) THEN
            STK_INF.QTY_ALLOC ELSE 0 END ) / (SUM(STK_INF.QTY_ALLOC))),2 ) * 100
            ELSE 0 END ) AS PICK_PER,
         ( CASE WHEN ( SUM(STK_INF.QTY_ALLOC) > 0 ) THEN
           ROUND( (SUM(CASE WHEN (STK_INF.NEXT_TASK_ID = 0 AND STK_INF.ALLOC_INVN_DTL_ID = 0) THEN
           STK_INF.QTY_PULLD ELSE 0 END ) / (SUM(STK_INF.QTY_ALLOC))),2 ) * 100
           ELSE 0 END ) AS TILT_PERCENT
         FROM STK_INF, TRK_DTL
         WHERE STK_INF.TASK_GENRTN_REF_CODE = '44' AND STK_INF.CARTON_NBR IS NOT NULL
          AND ((STK_INF.NEXT_TASK_ID = 0 AND STK_INF.STAT_CODE <= 90) OR (STK_INF.NEXT_TASK_ID > 0 AND STK_INF.STAT_CODE < 99))
          AND PULL_LOCN_ID = TRK_DTL.LOCN_ID(+) AND (TRK_DTL.AREA <>'C' OR TRK_DTL.AREA IS NULL)
         GROUP BY SUBSTR(STK_INF.TASK_GENRTN_REF_NBR,1,12)

    This is the EXPLAIN PLAIN i got after i issued EXECUTE DBMS_STATS.GATHER_TABLE_STATS ('schema_name','table_name'); for both the tables involved.
    The number of row returned is still 222 rows. But in the EXPLAIN PLAN it is shown as 3060 !!
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 3267659036
    | Id  | Operation               | Name     | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT        |          |  3060 |   194K|       | 16527  (10)| 00:03:19 |
    |   1 |  HASH GROUP BY          |          |  3060 |   194K|       | 16527  (10)| 00:03:19 |
    |*  2 |   FILTER                |          |       |       |       |            |          |
    |*  3 |    HASH JOIN RIGHT OUTER|          |   177K|    11M|  7344K| 16397  (10)| 00:03:17 |
    |   4 |     TABLE ACCESS FULL   | TRK_DTL |   313K|  3669K|       |  2378   (6)| 00:00:29 |
    |*  5 |     TABLE ACCESS FULL   | STK_INF |   177K|  9205K|       | 13030  (11)| 00:02:37 |
    PLAN_TABLE_OUTPUT
    Predicate Information (identified by operation id):
       2 - filter("TRK_DTL"."AREA"<>'C' OR "TRK_DTL"."AREA" IS NULL)
       3 - access("PULL_LOCN_ID"="TRK_DTL"."LOCN_ID"(+))
       5 - filter("STK_INF"."CARTON_NBR" IS NOT NULL AND
                  "STK_INF"."TASK_GENRTN_REF_CODE"='44' AND ("STK_INF"."NEXT_TASK_ID"=0 AND
                  "STK_INF"."STAT_CODE"<=90 OR "STK_INF"."NEXT_TASK_ID">0 AND
                  "STK_INF"."STAT_CODE"<99))
    22 rows selected.You mentioned about creating indexes in STK_INF table. STK_INF.NEXT_TASK_ID has 186385 distinct values out of a total 782087. Does NEXT_TASK_ID make a good candidate for a B-Tree index
    STK_INF.STAT_CODE has only four distinct values, would this make a good candidate for a Bitmap index?

  • How I can optimize this SQL query

    I require your help, I want to know how I can optimize this query
    SELECT
                    "F42119". "SDLITM" as "Code1"
                    "F42119". "SDAITM" as "Code2"
                    "F42119". "SDDSC1" as "Product"
                    "F42119". "SDMCU" as "Bodega"
                    Sum ("F42119". "SDSOQS" / 10000) as "Number",
                    Sum ("F42119". "SDUPRC" / 10000) as "preciou"
                    Sum ("F42119". "SDAEXP" / 100) as "Value",
                    Sum ("F42119". "SDUNCS" / 10000) as "CostoU"
                    Sum ("F42119". "SDECST" / 100) as "Cost"
                    "F4101". "IMSRP1" as "Division"
                    "F4101". "IMSRP2" as "classification",
                    "F4101". "IMSRP8" as "Brand"
                    "F4101". "IMSRP9" as "Aroma"
                    "F4101". "IMSRP0" as "Presentation"
                    "F42119". "SDDOC" as "Type",
                    "F42119". "SDDCT" as "Document",
                    "F42119". "SDUOM" as "Unit"
                    "F42119". "SDCRCD" as "currency"
                    "F0101". "ABAN8" as "ABAN8"
                    "F0101". "ABALPH" as "Customer"
                    "F0006". "MCRP22" as "Establishment"
    from          "PRODDTA". "F0101" "F0101"
                    "PRODDTA". "F42119" "F42119"
                    "PRODDTA". "F4101" "F4101"
                    "PRODDTA". "F0006" "F0006"
    where       "F42119". "SDAN8" = "F0101". "ABAN8"
      and         "F0006". "MCMCU" = "F42119". "SDMCU"
      and         "F4101". "IMITM" = "F42119". "SDITM"
      and         "F42119". "SDDCT" in ('RI', 'RM', 'RN')
      and         CAST (EXTRACT (MONTH FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM-                YYYY ') + substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in : Month
    and          CAST (EXTRACT (YEAR FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM-                YYYY ')+ Substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in: Year
    and          trim ("F0006". "MCRP22") =: Establishment
    and          trim ("F4101". "IMSRP1") =: Division
    Group By    "F42119". "SDLITM"
                    "F42119". "SDAITM"
                    "F42119". "SDDSC1"
                    "F4101". "IMSRP1"
                    "F42119". "SDDOC"
                    "F42119". "SDDCT"
                    "F42119". "SDUOM"
                    "F42119". "SDCRCD"
                    "F0101". "ABAN8"
                    "F0101". "ABALPH"
                    "F4101". "IMSRP2"
                    "F4101". "IMSRP8"
                    "F4101". "IMSRP9"
                    "F4101". "IMSRP0"
                    "F42119". "SDMCU"
                    "F0006". "MCRP22"
    I appreciate the help you can give me

    It seems to me that part of fixing it could be how you join the tables.
    Instead of the humongous where clause, put the applicable conditions on the join.
    You have
    from "PRODDTA". "F0101" "F0101"
    "PRODDTA". "F42119" "F42119"
    "PRODDTA". "F4101" "F4101"
    "PRODDTA". "F0006" "F0006"
    where "F42119". "SDAN8" = "F0101". "ABAN8"
    and "F0006". "MCMCU" = "F42119". "SDMCU"
    and "F4101". "IMITM" = "F42119". "SDITM"
    and "F42119". "SDDCT" in ('RI', 'RM', 'RN')
    and CAST (EXTRACT (MONTH FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM- YYYY ') + substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in : Month
    and CAST (EXTRACT (YEAR FROM TO_DATE (substr ((to_date ('01-01-'| | to_char (round (1900 + (CAST ("F42119". "SDDGL" as int) / 1000))),' DD- MM- YYYY ')+ Substr (to_char (CAST ("F42119". "SDDGL" as int)), 4,3) -1), 1,10))) AS INT) in: Year
    and trim ("F0006". "MCRP22") =: Establishment
    and trim ("F4101". "IMSRP1") =: Division
    INSTEAD try something like
    from JOIN "PRODDTA". "F0101" "F0101" ON "F42119". "SDAN8" = "F0101". "ABAN8"
    JOIN "PRODDTA". "F42119" "F42119" ON "F0006". "MCMCU" = "F42119". "SDMCU"
    JOIN "PRODDTA". "F4101" "F4101" ON join condition
    JOIN "PRODDTA". "F0006" "F0006" ON join condition.
    Not sure exactly how you need things joined, but above is the basic idea. Remove criteria for joining the tables from the WHERE clause and put them
    in the join statements. That might clean things up and make it more efficient.

  • Can anyone tell me how can i optimize this query...

    Can anyone tell me how can i optimize this query ??? :
    Select Distinct eopersona.numident From rscompeten , rscompet , rscv , eopersona , rscurso , rseduca , rsexplab , rsinteres
    Where ( ( (LOWER (rscompeten.nombre LIKE '%caracas%') AND ( rscompeten.id = rscompet.idcompeten ) AND ( rscv.id = rscompet.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscurso.nombre) LIKE '%caracas%') AND ( rscv.id = rscurso.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscurso.lugar) LIKE '%caracas%') AND ( rscv.id = rscurso.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rseduca.univinst) LIKE '%caracas%)' AND ( rscv.id = rseduca.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rsexplab.nombempre) LIKE '%caracas%' AND ( rscv.id = rsexplab.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rsinteres.descrip) LIKE '%caracas%' AND ( rscv.id = rsinteres.idcv ) AND ( eopersona.id = rscv.idpersona ) )
    OR ( (LOWER (rscv.cargoasp) LIKE '%caracas%' AND ( eopersona.id = rscv.idpersona ) )
    OR ( LOWER (eopersona.ciudad) LIKE '%caracas%' AND ( eopersona.id = rscv.idpersona )
    PLEASE IF YOU FIND SOMETHING WRONG.. PLEASE HELP ME.. this query takes me aproximatelly 10 minutes and the database is really small ( with only 200 records on each table )

    You are querying eight tables, however in any of your OR predicates you're only restricting 3 or 4 of those tables. That means that the remaining 4 or 5 tables are generating cartesian products. (n.b. the cartesian product of 5 tables with 200 rows each results in g 200^5 = 320,000,000,000 rows) Then you casually hide this behind "distinct".
    A simple restatement of your requirements looks like this:
    Select eopersona.numident
      From rscompeten,
           rscompet,
           rscv,
           eopersona
    Where LOWER (rscompeten.nombre) LIKE '%caracas%'
       AND rscompeten.id = rscompet.idcompeten
       AND rscv.id = rscompet.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    Select eopersona.numident
      From rscurso ,
           rscv,
           eopersona
    Where LOWER (rscurso.nombre) LIKE '%caracas%'
       AND rscv.id = rscurso.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    Select eopersona.numident
      From rscurso ,
           rscv,
           eopersona
    Where LOWER (rscurso.lugar) LIKE '%caracas%'
       AND rscv.id = rscurso.idcv
       AND eopersona.id = rscv.idpersona
    UNION
    ...From there you can eliminate redundancies as desired, but I imagine that the above will perform admirably with the data volumes you describe.

  • Please help me optimize this

    1. This code fragment:
    declare
    procedure t(ent in varchar)
    is
    begin
    for reg in (
    select
    /*+nocache*/
    opds_psap_numero,
    min(opds_data_inicio_tarde) as opds_data_inicio_tarde
    from operacoes_diagrama_sap, projeto_sap
    where
    opds_psap_numero = psap_numero and
    psap_pep like ent
    group by
    opds_psap_numero
    ) loop
    dbms_output.put_line('just teste');
    end loop;
    end;
    begin
    t('PR42E-075001%');
    end;
    Execution Time
    ***SCRIPT START 29-mar-2007 17:33:25 ***
    ***SCRIPT END 29-mar-2007 17:35:13 ***
    Time = 2 minutes (aprox)
    ==============================================
    2. But this code fragment (the same as before but with static sql):
    declare
    procedure t(ent in varchar)
    is
    begin
    for reg in (
    select
    /*+nocache*/
    opds_psap_numero,
    min(opds_data_inicio_tarde) as opds_data_inicio_tarde
    from operacoes_diagrama_sap, projeto_sap
    where
    opds_psap_numero = psap_numero and
    psap_pep like 'PR42E-075001%'
    group by
    opds_psap_numero
    ) loop
    dbms_output.put_line('just teste');
    end loop;
    end;
    begin
    t('PR42E-075001%');
    end;
    Execution Time
    ***SCRIPT START : 29-mar-2007 17:39:29***
    ***SCRIPT END 29-mar-2007 17:39:30***
    Time 1 sec
    ================================================
    I need the code of the first example (I must pass an 'in" parameter to the procedure), but with performance of the second.
    This query runs very fast when posted in sqlnavigator.
    it returns approx 1000 lines and i have an index at projeto_sap.psap_pep field
    What am i doing wrong?
    Please if you dont understand let me know. English is not my first language. From hot Brazil hehe

    Your second fragment has a string literal 'PR42E-075001%' for the LIKE operator in your WHERE clause. The first fragment uses a PL/SQL parameter, effectively a bind variable.
    The optimizer can choose the index for the second fragment because it knows the index will be effective. But the first fragment could have any value for the variable. The optimizer chooses a path that is best for "any value" AND YOU WIND UP WITH A PLAN THAT ...
    Hmm, I hit the caps lock by accident there, but it seems strangely appropriate.
    Anyway, you can try adding a hint to your query:
    /*+ nocache index( projeto_sap index_name */

  • Optimize this code?

    DATA: v_title(245) TYPE c,
    v_butxt(245) TYPE c,
    v_tot_recs LIKE sy-tabix,
    v_total_pages LIKE sy-tabix,
    v_tot_qty LIKE mseg-menge.
    TABLES: mara, makt, marc, mard, mkpf, mseg, t001, t001w,t001l.
    TABLES: sscrfields.
    *TYPE_POOLS
    TYPE-POOLS: slis. "ALV Display
    TYPES : BEGIN OF stype_mseg_lean,
    mblnr LIKE mkpf-mblnr,
    mjahr LIKE mkpf-mjahr,
    budat LIKE mkpf-budat,
    xblnr LIKE mkpf-xblnr,
    bukrs LIKE t001-bukrs,
    zeile LIKE mseg-zeile,
    bwart LIKE mseg-bwart,
    matnr LIKE mseg-matnr,
    werks LIKE mseg-werks,
    lgort LIKE mseg-lgort,
    shkzg LIKE mseg-shkzg,
    menge LIKE mseg-menge,
    meins LIKE mseg-meins,
    dmbtr LIKE mseg-dmbtr,
    dmbum LIKE mseg-dmbum,
    insmk LIKE mseg-insmk,
    aufnr LIKE mseg-aufnr,
    kostl LIKE mseg-kostl,
    anln1 LIKE mseg-anln1,
    kdauf LIKE mseg-kdauf.
    TYPES : END OF stype_mseg_lean.
    TYPES: stab_mseg_lean TYPE STANDARD TABLE OF stype_mseg_lean
    WITH KEY mblnr mjahr.
    TYPES: BEGIN OF stype_fields,
    fieldname TYPE name_feld,
    END OF stype_fields.
    TYPES: stab_fields TYPE STANDARD TABLE OF stype_fields
    WITH KEY fieldname.
    DATA: g_t_mseg_fields TYPE stab_fields.
    DATA: i_mseg_lean TYPE stype_mseg_lean OCCURS 0 WITH HEADER LINE.
    DATA: BEGIN OF i_show OCCURS 0,
    matnr LIKE mseg-matnr,
    maktx(150) type c,
    meins LIKE mseg-meins,
    op_stock LIKE mseg-menge,
    stock_in_a_prd LIKE mseg-menge,
    stock_in_a_cstn LIKE mseg-menge,
    stock_in_b_issue LIKE mseg-menge,
    stock_in_b_trn LIKE mseg-menge,
    stock_in_r LIKE mseg-menge,
    stock_toprd LIKE mseg-menge,
    stock_out_a LIKE mseg-menge,
    stock_out_b_issue LIKE mseg-menge,
    stock_out_b_trn LIKE mseg-menge,
    stock_out_r LIKE mseg-menge,
    cl_stock LIKE mseg-menge,
    END OF i_show.
    DATA : i_show_alv LIKE i_show OCCURS 0 WITH HEADER LINE.
    types: BEGIN OF t_mseg_lean_new ,
    matnr LIKE mseg-matnr, "Material Number
    bwart LIKE mseg-bwart, "Movement Type (Inventory Management)
    dmbtr LIKE mseg-dmbtr, "Amount in Local Currency
    shkzg LIKE mseg-shkzg, "Debit/Credit Indicator
    dmbum LIKE mseg-dmbum, "Revaluation amount on back
    menge LIKE mseg-menge, "Quantity
    meins LIKE mseg-meins, "Base Unit of Measure
    insmk LIKE mseg-insmk, "Stock Type
    aufnr LIKE mseg-aufnr, "Order Number
    kostl LIKE mseg-kostl, "Cost Center
    anln1 LIKE mseg-anln1, "Main Asset Number
    lgort LIKE mseg-lgort, "Storage Location
    kdauf LIKE mseg-kdauf, "Sales Order Number
    budat LIKE mkpf-budat, "Posting Date in the Document
    xblnr LIKE mkpf-xblnr, "Reference Document Number
    END OF t_mseg_lean_new.
    DATA: i_mseg_lean_new type t_mseg_lean_new OCCURS 0
    WITH HEADER LINE.
    DATA: i_mseg_lean_cl type t_mseg_lean_new OCCURS 0
    WITH HEADER LINE.
    DATA: i_mseg_lean_cl_final type sorted table of t_mseg_lean_new
    with unique key matnr with header line.
    DATA: wa_op_stock LIKE mseg-menge,
    wa_stock_in_a LIKE mseg-menge,
    wa_stock_in_b LIKE mseg-menge,
    wa_stock_in_r LIKE mseg-menge,
    wa_stock_out_a LIKE mseg-menge,
    wa_stock_out_b LIKE mseg-menge,
    wa_stock_out_r LIKE mseg-menge,
    wa_cl_stock LIKE mseg-menge, "closing stock
    wa_stock_321s LIKE mseg-menge, "Debit
    wa_stock_321h LIKE mseg-menge. "Credit
    DATA: wa_stock_in_a_prd LIKE mseg-menge,
    wa_stock_in_a_cstn LIKE mseg-menge,
    wa_stock_in_b_issue LIKE mseg-menge,
    wa_stock_in_b_trn LIKE mseg-menge,
    wa_stock_out_b_issue LIKE mseg-menge,
    wa_stock_out_b_trn LIKE mseg-menge.
    addition ends
    types: BEGIN OF t_mard ,
    matnr LIKE mard-matnr,
    labst LIKE mard-labst, "UNRESTRICTED STOCK
    insme LIKE mard-insme, "STOCK IN QUALITY INSP
    cl_stock LIKE mseg-menge, "QTY
    END OF t_mard.
    data: i_mard type sorted table of t_mard with unique key matnr with
    header line.
    DATA: v_meins TYPE mseg-meins.
    DATA: i_tabb LIKE bdcdata OCCURS 0 WITH HEADER LINE,
    wa_options LIKE ctu_params.
    DATA: v_objnm TYPE stxh-tdname,
    v_object TYPE stxh-tdobject,
    v_objid TYPE stxh-tdid.
    DATA: n TYPE i.
    DATA : v_long_text(22) TYPE c.
    DATA : V_DESC(150) TYPE C.
    DATA : i_tline LIKE tline OCCURS 0
    WITH HEADER LINE.
    DATA : BEGIN OF i_text_tab OCCURS 0,
    out_lines(150),
    END OF i_text_tab.
    DATA : i_text_tab1 LIKE i_text_tab OCCURS 0 WITH HEADER LINE.
    DATA : BEGIN OF i_tab_text OCCURS 0,
    ltext(150),
    END OF i_tab_text.
    DATA : BEGIN OF i_tab_text1 OCCURS 0,
    ltext1(100),
    END OF i_tab_text1.
    SELECTION-SCREEN BEGIN OF BLOCK one WITH FRAME TITLE text-001.
    SELECT-OPTIONS: s_matnr FOR mara-matnr." OBLIGATORY.
    PARAMETERS: "p_bukrs LIKE t001-bukrs OBLIGATORY,
    p_werks LIKE t001w-werks OBLIGATORY.
    SELECT-OPTIONS: s_lgort FOR t001l-lgort.
    SELECT-OPTIONS: s_budat FOR mkpf-budat OBLIGATORY.
    SELECTION-SCREEN END OF BLOCK one.
    PARAMETERS p_alv AS CHECKBOX . "For ALV
    If no date is given at all, the range is set to the maximum
    extend (1.1.0000 - 31.12.9999).
    If only s_budat-low is set, it is interpreted as the day for
    which the analysis is wanted --> s_budat-high is filled up.
    SELECTION-SCREEN FUNCTION KEY 1.
    IF s_budat-low IS INITIAL.
    s_budat-low = '00000101'.
    IF s_budat-high IS INITIAL.
    s_budat-high = '99991231'.
    ENDIF.
    ELSE.
    IF s_budat-high IS INITIAL.
    s_budat-high = s_budat-low.
    ENDIF.
    ENDIF.
    RANGES s_werks FOR t001w-werks.
    s_werks-sign = 'I'.
    s_werks-option = 'EQ'.
    s_werks-low = p_werks.
    APPEND s_werks.
    **********ALV Setting starts***************
    *ALV Data Declaration.
    DATA : i_events TYPE slis_t_event,
    i_list_top_of_page TYPE slis_t_listheader,
    g_top_of_page TYPE slis_formname VALUE 'TOP_OF_PAGE',
    i_layout TYPE slis_layout_alv,
    i_fieldcat TYPE slis_t_fieldcat_alv ,
    gs_print TYPE slis_print_alv,
    wa_fieldcat LIKE LINE OF i_fieldcat.
    DATA: v_repid TYPE sy-repid.
    DATA: gs_variant LIKE disvariant,
    first(01) TYPE c,
    g_save.
    *Initialization for ALV.
    INITIALIZATION.
    v_repid = sy-repid.
    i_layout-detail_popup = 'X'."PERFORM layout_init USING i_layout.
    PERFORM layout_init USING i_layout.
    PERFORM eventtab_build USING i_events[].
    gs_variant-report = v_repid.
    g_save = 'A'.
    *TOP-OF-PAGE, END-OF-PAGE, END-OF-LIST are called
    *dynamically due to PERFORM eventtab_build
    **********ALV Setting Ends***************
    **For Documentation
    INITIALIZATION.
    MOVE text-002 TO sscrfields-functxt_01.
    AT SELECTION-SCREEN.
    **For Documentation
    IF sscrfields-ucomm = 'FC01'.
    CALL FUNCTION 'RS_TOOL_ACCESS'
    EXPORTING
    operation = 'DOCS'
    object_name = 'ZMMR0100'
    object_type = 'PROG'
    EXCEPTIONS
    not_executed = 1
    invalid_object_type = 2
    OTHERS = 3.
    ENDIF.
    START-OF-SELECTION.
    PERFORM mkpf_mseg.
    PERFORM set_for_segregation.
    PERFORM segregation.
    PERFORM cl_stock.
    PERFORM op_stock.
    Added By Essam on 25.02.2008
    PERFORM i_show_alv_populate.
    Addition Ends
    i_show[] = i_show_alv[].
    END-OF-SELECTION.
    **ADDED BY ESSAM FOR ALV ON 23-FEB-2008
    PERFORM SHOW.
    IF p_alv = 'X'.
    PERFORM display_alv.
    ELSE.
    PERFORM display_list.
    ENDIF.
    **ADDITION ENDS FOR ALV
    SET TITLEBAR 'ZT1' WITH s_budat-low s_budat-high.
    PERFORM show.
    SET PF-STATUS 'ZP1'.
    TOP-OF-PAGE.
    PERFORM top_page.
    *AT LINE-SELECTION.
    PERFORM next_screen.
    *& Form mkpf_mseg
    text
    --> p1 text
    <-- p2 text
    FORM mkpf_mseg.
    SELECT * INTO CORRESPONDING FIELDS OF TABLE i_mseg_lean
    FROM mkpf AS mkpf JOIN mseg AS mseg
    ON mkpfmandt = msegmandt AND
    mkpfmblnr = msegmblnr AND
    mkpfmjahr = msegmjahr
    WHERE mseg~matnr IN s_matnr
    AND mseg~werks IN s_werks
    AND mseg~lgort IN s_lgort
    AND mkpf~budat GE s_budat-low.
    AND mkpf~budat IN s_budat.
    *added by ish on 30.6.04
    BELOW CODE CHANGED BY ESSAM ON 19-FEB-2008
    SELECT msegmatnr mkpfbudat mkpfxblnr msegbwart
    msegdmbtr msegshkzg
    msegdmbum msegmenge msegwerks mseglgort
    msegmeins mseginsmk msegaufnr msegkostl mseganln1 msegmblnr
    mseg~zeile
    mseg~kdauf
    INTO CORRESPONDING FIELDS OF TABLE i_mseg_lean
    FROM mkpf AS mkpf JOIN mseg AS mseg
    ON msegmandt = mkpfmandt AND
    msegmblnr = mkpfmblnr AND
    msegmjahr = mkpfmjahr
    WHERE mseg~matnr IN s_matnr
    AND mseg~werks IN s_werks
    AND mseg~lgort IN s_lgort
    AND mkpf~budat IN s_budat.
    IF sy-subrc <> 0.
    ENDIF.
    ****testing for optimization.
    SELECT msegmatnr mkpfbudat mkpfxblnr msegbwart
    msegdmbtr msegshkzg
    msegdmbum msegmenge msegwerks mseglgort
    msegmeins mseginsmk msegaufnr msegkostl mseganln1 msegmblnr
    mseg~zeile
    mseg~kdauf
    INTO CORRESPONDING FIELDS OF TABLE i_mseg_lean
    FROM mkpf AS mkpf JOIN mseg AS mseg
    ON msegmandt = mkpfmandt AND
    msegmblnr = mkpfmblnr AND
    msegmjahr = mkpfmjahr
    WHERE mseg~matnr IN s_matnr
    AND mseg~werks IN s_werks
    AND mseg~lgort IN s_lgort
    AND mkpf~budat IN s_budat.
    IF sy-subrc <> 0.
    ENDIF.
    *adding other materials whose transaction is not made during the
    *selected period
    CLEAR: i_mseg_lean.
    SELECT matnr werks lgort FROM mard INTO CORRESPONDING FIELDS OF
    i_mseg_lean WHERE
    matnr IN s_matnr AND
    werks IN s_werks AND
    lgort IN s_lgort .
    APPEND i_mseg_lean.
    ENDSELECT.
    *addition ends
    SORT i_mseg_lean BY matnr.
    ENDFORM. " mkpf_mseg
    *& Form segregation
    text
    --> p1 text
    <-- p2 text
    FORM segregation.
    CLEAR: wa_stock_in_a_prd,wa_stock_in_a_cstn, wa_stock_in_b_issue,
    wa_stock_in_b_trn,wa_stock_out_a, wa_stock_out_b_issue,
    wa_stock_out_b_trn,wa_stock_in_r, wa_stock_out_r.
    LOOP AT i_mseg_lean_new WHERE matnr NE ''.
    xx
    **As per change log(6)
    IF ( ( i_mseg_lean_new-bwart = '101' OR
    i_mseg_lean_new-bwart = '102' ) AND i_mseg_lean_new-lgort = ''
    AND i_mseg_lean_new-kdauf NE '' ).
    do not take into consideration but display the op&cl stock
    **ends
    ****STOCK IN_A
    ELSEIF ( i_mseg_lean_new-shkzg = 'S' AND i_mseg_lean_new-aufnr CA '-'
    AND i_mseg_lean_new-bwart NE '261' AND
    i_mseg_lean_new-bwart NE '262' )
    OR ( i_mseg_lean_new-bwart = '521' AND i_mseg_lean_new-shkzg = 'S' )
    OR ( i_mseg_lean_new-bwart = '309' AND
    i_mseg_lean_new-xblnr CA '-' AND
    i_mseg_lean_new-shkzg = 'S' )
    OR ( i_mseg_lean_new-bwart = '310' AND
    i_mseg_lean_new-xblnr CA '-' AND
    i_mseg_lean_new-shkzg = 'S' )
    **adeed as per change log(5)
    OR i_mseg_lean_new-bwart = '131' OR i_mseg_lean_new-bwart = '531'.
    **ends
    wa_stock_in_a_prd = wa_stock_in_a_prd + i_mseg_lean_new-menge.
    XY
    ELSEIF
    ( i_mseg_lean_new-shkzg = 'H' AND i_mseg_lean_new-aufnr CA '-'
    AND i_mseg_lean_new-bwart NE '261' AND
    i_mseg_lean_new-bwart NE '262' )
    OR ( i_mseg_lean_new-bwart = '522' AND i_mseg_lean_new-shkzg = 'H' )
    OR ( i_mseg_lean_new-bwart = '309' AND
    i_mseg_lean_new-xblnr CA '-' AND
    i_mseg_lean_new-shkzg = 'H' )
    OR ( i_mseg_lean_new-bwart = '310' AND
    i_mseg_lean_new-xblnr CA '-' AND
    i_mseg_lean_new-shkzg = 'H' )
    **adeed as per change log(5)
    OR i_mseg_lean_new-bwart = '132' OR i_mseg_lean_new-bwart = '532'.
    **ends
    wa_stock_in_a_prd = wa_stock_in_a_prd - i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '261' AND i_mseg_lean_new-aufnr CA '-' ).
    wa_stock_in_a_cstn = wa_stock_in_a_cstn + i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '262' AND i_mseg_lean_new-aufnr CA '-' ) .
    wa_stock_in_a_cstn = wa_stock_in_a_cstn - i_mseg_lean_new-menge.
    ****STOCK IN_B
    ELSEIF
    i_mseg_lean_new-bwart = '101' OR
    ( i_mseg_lean_new-bwart = '321' AND i_mseg_lean_new-shkzg = 'S' ).
    IF ( i_mseg_lean_new-bwart EQ '101' AND i_mseg_lean_new-shkzg = 'S' )
    AND ( i_mseg_lean_new-kostl NE ' ' OR
    i_mseg_lean_new-anln1 NE ' ' OR
    ( i_mseg_lean_new-aufnr NA '-' AND
    i_mseg_lean_new-aufnr NE ' ' ) ).
    wa_stock_out_b_issue = wa_stock_out_b_issue + i_mseg_lean_new-menge.
    ENDIF.
    wa_stock_in_b_issue = wa_stock_in_b_issue + i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '321' AND i_mseg_lean_new-shkzg = 'H' ) OR
    i_mseg_lean_new-bwart = '102' OR
    i_mseg_lean_new-bwart = '122' OR
    i_mseg_lean_new-bwart = '922'.
    wa_stock_in_b_issue = wa_stock_in_b_issue - i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '309' AND i_mseg_lean_new-shkzg = 'S' ) OR
    ( i_mseg_lean_new-bwart = '311' AND i_mseg_lean_new-shkzg = 'S' ) OR
    ( i_mseg_lean_new-bwart = '301' AND i_mseg_lean_new-shkzg = 'S' ) OR
    i_mseg_lean_new-bwart = '701'.
    wa_stock_in_b_trn = wa_stock_in_b_trn + i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '310' AND i_mseg_lean_new-shkzg = 'S' ) OR
    ( i_mseg_lean_new-bwart = '312' AND i_mseg_lean_new-shkzg = 'S' ) OR
    ( i_mseg_lean_new-bwart = '302' AND i_mseg_lean_new-shkzg = 'S' ) OR
    i_mseg_lean_new-bwart = '702'.
    wa_stock_in_b_trn = wa_stock_in_b_trn - i_mseg_lean_new-menge.
    ELSEIF
    ****STOCK OUT_A
    ( i_mseg_lean_new-bwart = '601' AND i_mseg_lean_new-shkzg = 'H' )
    OR ( i_mseg_lean_new-bwart = '654' AND i_mseg_lean_new-shkzg = 'H' )
    *As per change log(7)
    OR ( i_mseg_lean_new-bwart = '251' AND i_mseg_lean_new-shkzg = 'H' ).
    *Ends
    wa_stock_out_a = wa_stock_out_a + i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '602' AND i_mseg_lean_new-shkzg = 'S' )
    OR ( i_mseg_lean_new-bwart = '653' AND i_mseg_lean_new-shkzg = 'S' )
    *As per change log(7)
    OR ( i_mseg_lean_new-bwart = '252' AND i_mseg_lean_new-shkzg = 'S' ).
    *Ends
    wa_stock_out_a = wa_stock_out_a - i_mseg_lean_new-menge.
    *****STOCK OUT_B
    ELSEIF
    ( i_mseg_lean_new-bwart = '261' OR
    i_mseg_lean_new-bwart = '241' OR
    i_mseg_lean_new-bwart = '201' ).
    wa_stock_out_b_issue = wa_stock_out_b_issue + i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '202' OR
    i_mseg_lean_new-bwart = '242' OR
    i_mseg_lean_new-bwart = '262' ).
    **The following line for subtraction was missed out in the
    **original coding.
    **Added on 16.10.2005 by Firoz.
    wa_stock_out_b_issue = wa_stock_out_b_issue - i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '309' AND i_mseg_lean_new-shkzg = 'H' ) OR
    ( i_mseg_lean_new-bwart = '311' AND i_mseg_lean_new-shkzg = 'H' ) OR
    ( i_mseg_lean_new-bwart = '301' AND i_mseg_lean_new-shkzg = 'H' ).
    wa_stock_out_b_trn = wa_stock_out_b_trn + i_mseg_lean_new-menge.
    ELSEIF
    ( i_mseg_lean_new-bwart = '310' AND i_mseg_lean_new-shkzg = 'H' ) OR
    ( i_mseg_lean_new-bwart = '312' AND i_mseg_lean_new-shkzg = 'H' ) OR
    ( i_mseg_lean_new-bwart = '302' AND i_mseg_lean_new-shkzg = 'H' ).
    wa_stock_out_b_trn = wa_stock_out_b_trn - i_mseg_lean_new-menge.
    ENDIF.
    *****addition Ends
    v_meins = i_mseg_lean_new-meins.
    AT END OF matnr.
    if v_meins is initial.
    SELECT SINGLE meins FROM mara INTO i_show-meins
    WHERE matnr = i_mseg_lean_new-matnr.
    endif.
    i_show-matnr = i_mseg_lean_new-matnr.
    ****ADDED BY ESSAM ON 24-FEB-2008
    v_long_text = i_show-matnr.
    v_objnm = v_long_text.
    v_object = 'MATERIAL'.
    v_objid = 'GRUN'.
    PERFORM read_text TABLES i_tline
    USING v_objnm
    v_objid
    v_object.
    CLEAR: v_long_text.
    PERFORM split_texts TABLES i_tline i_text_tab1.
    DATA : v_lines TYPE sy-tabix,
    v_charno TYPE i.
    DESCRIBE TABLE i_text_tab1 LINES v_lines.
    IF v_lines NE 0.
    DO v_lines TIMES.
    READ TABLE i_text_tab1 INDEX sy-index.
    IF sy-subrc = 0.
    i_tab_text-ltext = i_text_tab1-out_lines.
    v_desc = i_tab_text-ltext.
    ENDIF.
    ENDDO.
    CLEAR:i_tab_text-ltext,i_tline[],v_charno,i_text_tab1[],v_lines.
    ENDIF.
    i_show-maktx = v_desc.
    i_show-stock_in_a_prd = wa_stock_in_a_prd.
    i_show-stock_in_a_cstn = wa_stock_in_a_cstn.
    i_show-stock_in_b_issue = wa_stock_in_b_issue.
    i_show-stock_in_b_trn = wa_stock_in_b_trn.
    i_show-stock_in_r = wa_stock_in_r.
    i_show-stock_toprd = wa_stock_321h - wa_stock_321s.
    i_show-stock_out_a = wa_stock_out_a.
    i_show-stock_out_b_issue = wa_stock_out_b_issue.
    i_show-stock_out_b_trn = wa_stock_out_b_trn.
    i_show-stock_out_r = wa_stock_out_r.
    APPEND i_show.
    CLEAR: wa_stock_in_a_prd,wa_stock_in_a_cstn,
    wa_stock_in_b_issue,wa_stock_in_b_trn,
    wa_stock_out_a,
    wa_stock_out_b_issue, wa_stock_out_b_trn,
    wa_stock_in_r, wa_stock_out_r,
    wa_stock_321s, wa_stock_321h.
    ENDAT.
    ENDLOOP.
    ENDFORM. " segregation
    *& Form set_for_segregation
    text
    --> p1 text
    <-- p2 text
    FORM set_for_segregation.
    *i_mseg_lean_new[] = i_mseg_lean[] .
    LOOP AT i_mseg_lean .
    i_mseg_lean_new-matnr = i_mseg_lean-matnr.
    i_mseg_lean_new-budat = i_mseg_lean-budat.
    i_mseg_lean_new-bwart = i_mseg_lean-bwart.
    i_mseg_lean_new-dmbtr = i_mseg_lean-dmbtr.
    i_mseg_lean_new-shkzg = i_mseg_lean-shkzg.
    i_mseg_lean_new-dmbum = i_mseg_lean-dmbum.
    i_mseg_lean_new-menge = i_mseg_lean-menge.
    i_mseg_lean_new-meins = i_mseg_lean-meins.
    i_mseg_lean_new-insmk = i_mseg_lean-insmk.
    i_mseg_lean_new-aufnr = i_mseg_lean-aufnr.
    i_mseg_lean_new-xblnr = i_mseg_lean-xblnr.
    i_mseg_lean_new-kostl = i_mseg_lean-kostl.
    i_mseg_lean_new-anln1 = i_mseg_lean-anln1.
    i_mseg_lean_new-kdauf = i_mseg_lean-kdauf.
    i_mseg_lean_new-lgort = i_mseg_lean-lgort.
    APPEND i_mseg_lean_new.
    ENDLOOP.
    ENDFORM. " set_for_segregation
    *& Form display_list
    text
    --> p1 text
    <-- p2 text
    FORM display_list.
    DATA: v_count TYPE i,
    v_div TYPE i,
    v_op_stock TYPE mseg-menge,
    v_in_a TYPE mseg-menge,
    v_in_b TYPE mseg-menge,
    v_in_r TYPE mseg-menge,
    v_out_a TYPE mseg-menge,
    v_out_b TYPE mseg-menge,
    v_out_r TYPE mseg-menge,
    v_cl_stock TYPE mseg-menge,
    v_toprd TYPE mseg-menge.
    DATA: v_in_a_prd TYPE mseg-menge,
    v_in_a_cstn TYPE mseg-menge,
    v_in_b_issue TYPE mseg-menge,
    v_in_b_trn TYPE mseg-menge,
    v_out_b_issue TYPE mseg-menge,
    v_out_b_trn TYPE mseg-menge.
    LOOP AT i_show.
    IF NOT i_show-op_stock IS INITIAL OR
    NOT i_show-cl_stock IS INITIAL OR
    NOT i_show-stock_in_a_prd IS INITIAL OR
    NOT i_show-stock_in_a_cstn IS INITIAL OR
    NOT i_show-stock_in_b_issue IS INITIAL OR
    NOT i_show-stock_in_b_trn IS INITIAL OR
    NOT i_show-stock_out_a IS INITIAL OR
    NOT i_show-stock_out_b_issue IS INITIAL OR
    NOT i_show-stock_out_b_trn IS INITIAL.
    v_tot_recs = v_tot_recs + 1.
    ENDIF.
    ENDLOOP.
    IF s_lgort-low IS INITIAL AND s_lgort-high IS INITIAL.
    v_tot_recs = v_tot_recs + 3.
    v_tot_qty = v_tot_recs MOD 51.
    IF v_tot_qty NE '0'.
    CLEAR v_tot_qty.
    v_tot_qty = v_tot_recs / 51.
    v_total_pages = v_tot_qty.
    IF v_total_pages GT v_tot_qty.
    v_total_pages = v_total_pages - 1.
    ENDIF.
    v_total_pages = v_total_pages + 1.
    ELSE.
    CLEAR v_tot_qty.
    v_total_pages = v_tot_recs / 51.
    ENDIF.
    ELSE.
    v_tot_recs = v_tot_recs + 3.
    v_tot_qty = v_tot_recs MOD 50.
    IF v_tot_qty NE '0'.
    CLEAR v_tot_qty.
    v_tot_qty = v_tot_recs / 50.
    v_total_pages = v_tot_qty.
    IF v_total_pages GT v_tot_qty.
    v_total_pages = v_total_pages - 1.
    ENDIF.
    v_total_pages = v_total_pages + 1.
    ELSE.
    CLEAR v_tot_qty.
    v_total_pages = v_tot_recs / 50.
    ENDIF.
    ENDIF.
    LOOP AT i_show.
    FORMAT COLOR COL_NORMAL INTENSIFIED OFF.
    WRITE:/ '|'.
    pstl 15 i_show-matnr. HIDE i_show-matnr.
    pstl 1 '|'. pstl 150 i_show-maktx.
    pstl 1 '|'.pstl 5 i_show-meins.
    pstl 1 '|'.pstr 16 i_show-op_stock.
    pstl 1 '|'.pstr 16 i_show-stock_in_a_prd.
    pstl 1 '|'.pstr 16 i_show-stock_in_a_cstn.
    pstl 1 '|'.pstr 16 i_show-stock_in_b_issue.
    pstl 1 '|'.pstr 16 i_show-stock_in_b_trn.
    pstl 1 '|'.pstr 16 i_show-stock_out_a.
    pstl 1 '|'.pstr 16 i_show-stock_out_b_issue.
    pstl 1 '|'.pstr 16 i_show-stock_out_b_trn.
    pstl 1 '|'.pstr 16 i_show-cl_stock.
    pstl 1 '|'.
    v_count = v_count + 1.
    v_op_stock = v_op_stock + i_show-op_stock.
    v_in_a_prd = v_in_a_prd + i_show-stock_in_a_prd.
    v_in_a_cstn = v_in_a_cstn + i_show-stock_in_a_cstn.
    v_in_b_issue = v_in_b_issue + i_show-stock_in_b_issue.
    v_in_b_trn = v_in_b_trn + i_show-stock_in_b_trn.
    v_in_r = v_in_r + i_show-stock_in_r.
    v_toprd = v_toprd + i_show-stock_toprd.
    v_out_a = v_out_a + i_show-stock_out_a.
    v_out_b_issue = v_out_b_issue + i_show-stock_out_b_issue.
    v_out_b_trn = v_out_b_trn + i_show-stock_out_b_trn.
    v_out_r = v_out_r + i_show-stock_out_r.
    v_cl_stock = v_cl_stock + i_show-cl_stock.
    ENDLOOP.
    ULINE AT (351).
    FORMAT COLOR COL_TOTAL INTENSIFIED OFF.
    WRITE:/ '|'.
    pstl 15 'Totals:'. pstl 152 ''.
    pstl 1 '|'.pstl 5 ''.
    pstl 1 '|'.pstr 16 v_op_stock.
    pstl 1 '|'.pstr 16 v_in_a_prd.
    pstl 1 '|'.pstr 16 v_in_a_cstn.
    pstl 1 '|'.pstr 16 v_in_b_issue.
    pstl 1 '|'.pstr 16 v_in_b_trn.
    pstl 1 '|'.pstr 16 v_out_a.
    pstl 1 '|'.pstr 16 v_out_b_issue.
    pstl 1 '|'.pstr 16 v_out_b_trn.
    pstl 1 '|'.pstr 16 v_cl_stock.
    pstl 1 '|'.
    ULINE AT (351).
    FORMAT COLOR OFF.
    ENDFORM. " show
    *& Form cl_stock
    text
    --> p1 text
    <-- p2 text
    FORM cl_stock.
    op (1 a add)
    *selecting the current availabe stock in the system for requested
    materials
    SELECT matnr sum( labst ) sum( insme ) FROM mard INTO TABLE i_mard
    WHERE matnr in s_matnr and
    werks IN s_werks AND
    lgort IN s_lgort group by matnr.
    op (1 z add)
    IF s_budat-high NE '99991231' AND s_budat-high NE s_budat-low.
    *added on 25.01.2004
    IF s_budat-high NE '99991231' AND s_budat-high NE sy-datum.
    PERFORM mkpf_mseg_closing.
    ELSE.
    *added on 08.02.2004
    LOOP AT i_show.
    i_mseg_lean_cl_final-matnr = i_show-matnr.
    APPEND i_mseg_lean_cl_final.
    CLEAR i_mseg_lean_cl_final.
    ENDLOOP.
    *addition ends
    ENDIF.
    CLEAR: wa_cl_stock, v_meins.
    IF NOT i_mseg_lean_cl[] IS INITIAL.

    You should use SE30 or ST05 to find the problem area before posting this.
    Rob

  • How to optimize this??

    hello,
    i wrote this for extracting 9th highest salary from emp table.
    SQL> select min(sal) from
    2 (select distinct sal from emp
    3 where sal is not null
    4 order by sal desc)
    5 where rownum<=9;
    MIN(SAL)
    1250
    But It has limitation. when we enter rownum more than existing rows then it gives minimum sal which is at the last position. rather than giving error or any different sense.
    following is the query which has not satisfied its use in proper.
    SQL> select min(sal) from
    2 (select distinct sal from emp
    3 where sal is not null
    4 order by sal desc)
    5 where rownum<=40
    6 /
    MIN(SAL)
    800
    where 800 is on last positon in 14 rows table.
    please optimize and solve my problem.

    user8710598 wrote:
    only optimize or edit posted query...i knw the rank is given correct ans.That's a little demanding and rude of you. People are trying to help by understanding what you are actually trying to achieve.
    Anyway...
    SQL> ed
    Wrote file afiedt.buf
      1  select sal
      2  from (
      3    select sal, rownum as rn
      4    from (
      5      select distinct sal
      6      from emp
      7      order by sal desc
      8      )
      9    )
    10* where rn = 9
    SQL> /
           SAL
          1250
    SQL> ed
    Wrote file afiedt.buf
      1  select sal
      2  from (
      3    select sal, rownum as rn
      4    from (
      5      select distinct sal
      6      from emp
      7      order by sal desc
      8      )
      9    )
    10* where rn = 40
    SQL> /
    no rows selected
    SQL>

  • How to optimize this SQL. Help needed.

    Hi All,
    Can you please help with this SQL:
    SELECT /*+ INDEX(zl1 zipcode_lat1) */
    zl2.zipcode as zipcode,l.location_id as location_id,
    sqrt(POWER((69.1 * ((zl2.latitude*57.295779513082320876798154814105) - (zl1.latitude*57.295779513082320876798154814105))),2) + POWER((69.1 * ((zl2.longitude*57.295779513082320876798154814105) - (zl1.longitude*57.295779513082320876798154814105)) * cos((zl1.latitude*57.295779513082320876798154814105)/57.3)),2)) as distance
    FROM location_atao l, zipcode_atao zl1, client c, zipcode_atao zl2
    WHERE zl1.zipcode = l.zipcode
    AND l.client_id = c.client_id
    AND c.client_id = 306363
    And l.appType = 'HOURLY'
    and c.milessearchzipcode >= sqrt(POWER((69.1 * ((zl2.latitude*57.295779513082320876798154814105) - (zl1.latitude*57.295779513082320876798154814105))),2) + POWER((69.1 * ((zl2.longitude*57.295779513082320876798154814105) - (zl1.longitude*57.295779513082320876798154814105)) * cos((zl1.latitude*57.295779513082320876798154814105)/57.3)),2))
    I tried to optimize it by adding country column in zipcode_atao table. So that we can limit the search in zipcode_atao table based on country.
    Any other suggestions.
    Thanks

    Welcome to the forum.
    Please follow the instructions given in this thread:
    How to post a SQL statement tuning request
    HOW TO: Post a SQL statement tuning request - template posting
    and add the nessecary details we need to your thread.
    Depending on your database version (the result of: select * from v$version; ):
    Have you tried running the query without the index-hint?
    Are your table (and index) statatistics up-to-date?

  • How to optimize this sql by writing MINUS function.

    Hi all,
    how to optimize the sql by writing MINUS function.
    these are my tables
    1. CREATE TABLE POSTPAID
    RECORD VARCHAR2(2000 BYTE),
    FLAG NUMBER
    Record format:
    Mobile no in 1:10 of that length
    2. CREATE TABLE SUBSCRIBER
    PHONE_NO VARCHAR2(10 BYTE)
    My requirement is following sql need write using ‘minus’ as this one is very slow
    select record record from POSTPAID where substr(record,9,10) NOT in (select PHONE_NO from SUBSCRIBER)
    Thanks

    Why are you very particular about using "MINUS". You can optimize the sql by using "NOT EXISTS" instead of "NOT IN" as below:
    SELECT RECORD FROM POSTPAID A WHERE NOT EXISTS (SELECT 1 FROM SUBSCRIBER B WHERE SUBSTR(A.RECORD,9,10) = B.PHONE_NO)

  • Is there a way to optimize this request ?

    Hi,
    The above function returns the YEARS of the lines of a table. Each line contains a date. Data Sample :
    3487     22/06/2003 17:19:53     2,288
    3487     22/06/2003 17:20:54     8,277
    3467     22/06/2003 17:21:32     40,1378
    3487     22/06/2003 17:23:32     2,386
    3467     22/06/2003 17:23:57     38,9814
    The table contains 4 060 521 lines....
    DECLARE
    X VARCHAR2 (4000);
    BEGIN
    X := 'SELECT distinct(TO_CHAR(DATE1, ''YYYY'')) d, (TO_CHAR(DATE1, ''YYYY'')) r
    FROM EVV_'||:p4_site ||' order by d';
    RETURN X ;
    END;Is there a way to reduce the time taken by the request ? kind of optimization ?
    Thank you for you kind help.
    Christian.

    >
    Salut,
    First things first DB version/OS/Hardware - Disk/CPU.
    The above function returns the YEARS of the lines of a table. Each line contains a date. Data Sample :
    3487     22/06/2003 17:19:53     2,288
    3487     22/06/2003 17:20:54     8,277
    3467     22/06/2003 17:21:32     40,1378
    3487     22/06/2003 17:23:32     2,386
    3467     22/06/2003 17:23:57     38,9814
    The table contains 4 060 521 lines....
    DECLARE
    X VARCHAR2 (4000);
    BEGIN
    X := 'SELECT distinct(TO_CHAR(DATE1, ''YYYY'')) d, (TO_CHAR(DATE1, ''YYYY'')) r
    FROM EVV_'||:p4_site ||' order by d';
    RETURN X ;
    END;
    Is there a way to reduce the time taken by the request ? kind of optimization ?Furthermore, we don't know the time taken, you haven't told us!
    Put in a query plan for this if you want more information.
    Use a functional index? Put an index on the TO_CHAR of the year for that
    field of that table? BTW, this is just a guess.
    Thank you for you kind help.De rien.
    Paul...
    Christian.--
    When asking database related questions, please give other posters
    some clues, like OS (with version), version of Oracle being used and DDL.
    Other trivia such as CPU, RAM + Disk configuration might also be useful.
    The exact text and/or number of error messages is useful (!= "it didn't work!"). Thanks.
    Furthermore, as a courtesy to those who spend time analysing and attempting to help,
    please do not top post and do try to trim your replies!

  • Please help me optimize this query

    Hi,
    Can you’ll please recommend me if there is any way to optimize the SQL statement below.
    I have 3 tables:
    item_master: contains master list of items
    item_rentout: items currently out on rent
    item_missing: items having special status like missing, withdrawn and so on.
    I want to find items that are currently available.
    Which is items in table item_master –(minus) items in table item_rentout –(minus) items in table in item_missing.
    This is how my sql statement is:
    select item_master.item_id from item_master where item_master.item_id not in
    ((select item_id from item_rentout) union (select item_id from item_missing where status in (‘missing’,withdrawn’)))Regards,
    Raja

    In addition to what Pointless posted (which is very valuable information!) you could always try and write a logical equivalent SQL statement. It may generate a more favorable plan.
    I have written the following that I think may work but they are untested:
    SELECT  ITEM_MASTER.ITEM_ID     FROM ITEM_MASTER
    MINUS
    SELECT  ITEM_ID FROM ITEM_RENTOUT
    MINUS
    SELECT  ITEM_ID FROM ITEM_MISSING WHERE STATUS IN ('missing','withdrawn')
    SELECT  item_master.item_id
    FROM    ITEM_MASTER IM
    WHERE   NOT EXISTS
                    SELECT  NULL
                    FROM    ITEM_RENTOUT IR
                    WHERE   IR.ITEM_ID = IM.ITEM_ID
    AND     NOT EXISTS
                    SELECT  NULL
                    FROM    ITEM_MISSING IIM
                    WHERE   IIM.ITEM_ID = IM.ITEM_ID       
            )

Maybe you are looking for

  • Close popup with branch

    I have created a page with a "New party" button that opens a popup. It clears that cache for the popup and works fine. The new party can be entered and "create"d. The page branches to itself and the create button is not displayed. The new party data

  • CS5 (camera raw 8.7.1) not reading Nikon d810 NEF files

    I just purchased Nikon d810. I have CS5. All updates are current and I've downloaded Camera Raw 8.7.1 but Photoshop/Bridge still can not read NEF files. What am I doing wrong?

  • Products not copied from Sales Quotation to Service Order

    Hi All, I am trying to create a Service Quotation as a follow up to a Sales order. There are multiple line items in the Sales Order - ex: 1 sales item and 1 service item. I want to create a service order as a follow up to the sales order and want the

  • Content Engine Problem

    Dear All, My apologies if this posting is on the wrong board. I've a problem with our content engine returning an error of: "Tre reply from server is not valid" The URL of the site is http://hiring.monster.co.uk/jobs/createtitle.aspx?mode=qb. I've at

  • Airport Extreme runs slow on iMac desktop but twice as fast on MacBook Pro - how can I improve upload speed on iMac?

    I have already done a complete clean of the iMac, to no avail. Have read the recommendations for adjusting chaneels, etc and I've done that too. The iMac has clear line of sight and is 20 feet away distance from Airport Extreme. My MacBook Pro runs v