‎2006 May 31 12:57 PM
Hi experts
it seems in the following code there some costliest statements
can u plz analyse the code and correct the code so that performance improves
*& TABLES
&----
TABLES: AUFK, "Order master data
T001W, "PLANTS/BRANCHES
KNA1, "General Data in Customer Master
T003O, "ORDER TYPES
PMSDO, "PM Organizational data for sales documents
AFKO, "Order header data PP orders
VBAP, "Sales Document Item Data
FPLA, "Billing Plan
FPLT, "Billing Plan Dates
VBRK, "Billing Document Header data
VBAK, "Sales Document Header data
KONV, "Condition-Transaction Data
IHPA, "Plant maintenance
VIQMEL, "Notification
CAUFVD.
&----
*& TYPE POOLS
&----
TYPE-POOLS SLIS. "Global types for generic building blocks
&----
*& PROGRAM VARIABLES
&----
DATA: G_COL TYPE I, "Column position for fieldcat
G_SUM LIKE FPLT-FPROZ, "Billing %
G_TIME LIKE AFKO-GSTRP, "Time Period
G_DATE LIKE FPLT-AFDAT,
G_FKSAF LIKE FPLT-FKSAF,
G_PARNR LIKE IHPA-PARNR,
G_NAME1 LIKE KNA1-NAME1,
G_BSTKD LIKE PMSDO-BSTKD,
g_gltrp LIKE caufvd-gltrp, "Period of end time
g_gstrp LIKE caufvd-gstrp, "Period of start time
lv_lines type i.
&----
*& STRUCTURES
&----
DATA: I_FIELDCATTAB_WA TYPE SLIS_FIELDCAT_ALV, "Fieldcat Work Area for ALV
I_TITLE_WA TYPE SLIS_LISTHEADER. "Title Work Area for ALV
&----
*& INTERNAL TABLES
&----
" Internal table containg data of table VBAP
DATA I_VBAK LIKE VBAK OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF it_plant OCCURS 0,
werks like t001w-werks,
END OF it_plant.
" Internal table containing final data for the Report screen
DATA: BEGIN OF I_FINTAB OCCURS 0,
KUNUM LIKE VIQMEL-KUNUM,
NAME1 LIKE KNA1-NAME1,
AUFNR LIKE AUFK-AUFNR,
KTEXT LIKE AUFK-KTEXT,
BSTKD LIKE PMSDO-BSTKD,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBAP-POSNR,
KDMAT LIKE VBAP-KDMAT,
ZMENG LIKE VBAP-ZMENG,
KBETR LIKE KONV-KBETR,
NETWR LIKE VBAP-NETWR,
ARKTX LIKE VBAP-ARKTX,
AFDAT LIKE FPLT-AFDAT,
FPROZ LIKE AUFK-USER4,
USER4 LIKE AUFK-USER4,
FKSAF TYPE CHAR30,
WERKS LIKE MARC-WERKS,
AUART LIKE AUFK-AUART,
GSTRP LIKE AFKO-GSTRP,
GLTRP LIKE AFKO-GSTRP,
END OF I_FINTAB.
DATA I_FINALTAB LIKE I_FINTAB OCCURS 0 WITH HEADER LINE.
" Internal table containing AUFK table contents
DATA: I_AUFKTAB LIKE AUFK OCCURS 0 WITH HEADER LINE,
" Internal table containing FPLA table contents
I_FPLATAB LIKE FPLA OCCURS 0 WITH HEADER LINE,
" Internal table containing FPLT table contents
I_FPLTTAB LIKE FPLT OCCURS 0 WITH HEADER LINE,
" Internal table containing VBAPtable contents
I_VBAP LIKE VBAP OCCURS 0 WITH HEADER LINE,
" Fieldcat Internal table for ALV
I_FIELDCATTAB TYPE SLIS_T_FIELDCAT_ALV ,
" Title Internal table for ALV
I_TITLE TYPE SLIS_T_LISTHEADER,
I_FPLA LIKE FPLA OCCURS 0 WITH HEADER LINE.
DATA I TYPE N.
&----
*& SELECT-OPTIONS
&----
SELECT-OPTIONS: S_WERKS FOR T001W-WERKS, "Plant
S_AUART FOR AUFK-AUART, "Order Type
S_GSTRP FOR CAUFVD-GSTRP, "Period of Time
S_KUNUM FOR VIQMEL-KUNUM, "Customer Number
S_NAME1 FOR KNA1-NAME1, "Customername
S_AUFNR FOR AUFK-AUFNR MATCHCODE OBJECT ORDP, "Job Number
S_KTEXT FOR AUFK-KTEXT, "Job Description
S_BSTKD FOR PMSDO-BSTKD. "Purchase order no
***********************************************************************
AT SELECTION-SCREEN
***********************************************************************
Event which occurs each time the user hits enter on the selection
screen.
AT SELECTION-SCREEN.
perform plant_authorization_check.
perform validate_select_options.
***********************************************************************
START-OF-SELECTION
***********************************************************************
START-OF-SELECTION.
"Retrieves the data from the table AUFK,VBAP,VBAK,FPLT into internal table I_AUFKTAB
PERFORM SUB_SELECTION_TAB.
"Internal table containing the report fields
PERFORM SUB_SELECTION_FINALTAB1.
"Defining the fieldcatalog to be used for the report
PERFORM SUB_FIELDCAT_BUILD.
"Define the Top-Of-Page contents
PERFORM SUB_FIELDCAT_TITLE.
"Mapping the internal table I_FINALTAB with fieldcat
PERFORM SUB_FIELDCAT_DISPLAY.
&----
*& Form SUB_SELECTION_TAB
&----
Retrieves the data from the table AUFK into internal table I_AUFKTAB
----
FORM SUB_SELECTION_TAB.
SELECT AUFNR KTEXT OBJNR USER4 WERKS AUART INTO CORRESPONDING FIELDS OF TABLE
I_AUFKTAB FROM AUFK
FOR ALL ENTRIES in it_plant
WHERE AUFNR IN S_AUFNR
AND KTEXT IN S_KTEXT
AND WERKS IN S_WERKS
AND AUART IN S_AUART
AND werks = it_plant-werks.
AND ERDAT IN S_GSTRP.
IF SY-SUBRC <> 0.
MESSAGE S017.
ENDIF.
ENDFORM. "SUB_SELECTION_TAB
&----
*& Form SUB_SELECTION_FINALTAB1
&----
Final Internal table containing the report fields
----
FORM SUB_SELECTION_FINALTAB1.
" Retrieve the sales line items from the table VBAK
*IF NOT I_AUFKTAB[] IS INITIAL.
*loop at i_aufktab.
IF S_AUFNR IS INITIAL.
SELECT * FROM VBAK INTO TABLE I_VBAK
WHERE AUFNR = I_AUFKTAB-AUFNR.
ELSE.
*LOOP AT I_AUFKTAB.
SELECT * FROM VBAK INTO TABLE I_VBAK FOR ALL ENTRIES IN I_AUFKTAB
WHERE AUFNR = I_AUFKTAB-AUFNR.
APPEND I_VBAK.
ENDSELECT.
*ENDLOOP.
ENDIF.
IF I_VBAK IS INITIAL.
SELECT * FROM VBAK INTO TABLE I_VBAK.
WHERE AUFNR = I_AUFKTAB-AUFNR.
ENDIF.
*ENDIF.
*LOOP AT I_AUFKTAB.
LOOP AT I_VBAK.
" Retrieve the sales line items from the table VBAP
SELECT * INTO TABLE I_VBAP FROM VBAP
WHERE VBELN = I_VBAK-VBELN.
AND AUFNR = I_AUFKTAB-AUFNR.
" Retrieve the billing plan dates from the table FPLA
SELECT * FROM FPLA INTO TABLE I_FPLA
WHERE VBELN = I_VBAK-VBELN.
I = 1.
LOOP AT I_FPLA.
" Retrieve the billing plan dates from the table FPLT
SELECT AFDAT FPROZ FKSAF INTO CORRESPONDING FIELDS OF TABLE I_FPLTTAB
FROM FPLT
WHERE FPLNR = I_FPLA-FPLNR.
LOOP AT I_FPLTTAB.
AT END OF FPLNR.
SUM.
MOVE I_FPLTTAB-FPROZ TO G_SUM.
ENDAT.
I_FINTAB-FPROZ = G_SUM.
ENDLOOP.
IF I_FPLTTAB-FKSAF = 'A'.
CONCATENATE TEXT-001 '(' I_FPLTTAB-FKSAF ')' INTO I_FINTAB-FKSAF.
ELSEIF I_FPLTTAB-FKSAF = 'B'.
CONCATENATE TEXT-002 '(' I_FPLTTAB-FKSAF ')' INTO I_FINTAB-FKSAF.
ELSEIF I_FPLTTAB-FKSAF = 'C'.
CONCATENATE TEXT-003 '(' I_FPLTTAB-FKSAF ')' INTO I_FINTAB-FKSAF.
ELSEIF I_FPLTTAB-FKSAF = ' '.
CONCATENATE ' ' '(' I_FPLTTAB-FKSAF ')' INTO I_FINTAB-FKSAF.
ENDIF.
SORT I_FPLTTAB BY AFDAT DESCENDING .
READ TABLE I_FPLTTAB INDEX 1.
IF SY-SUBRC = 0.
I_FINTAB-AFDAT = I_FPLTTAB-AFDAT.
ENDIF.
READ TABLE I_VBAP INDEX I.
IF SY-SUBRC = 0.
MOVE : I_VBAP-VBELN TO I_FINTAB-VBELN,
I_VBAP-POSNR TO I_FINTAB-POSNR,
I_VBAP-KDMAT TO I_FINTAB-KDMAT,
I_VBAP-ZMENG TO I_FINTAB-ZMENG,
I_VBAP-NETPR TO I_FINTAB-KBETR,
I_VBAP-NETWR TO I_FINTAB-NETWR,
I_VBAP-AUFNR TO I_FINTAB-AUFNR,
I_VBAP-ARKTX TO I_FINTAB-ARKTX.
CLEAR I_AUFKTAB.
READ TABLE I_AUFKTAB WITH KEY AUFNR = I_VBAP-AUFNR.
IF SY-SUBRC = 0.
I_FINTAB-KTEXT = I_AUFKTAB-KTEXT.
I_FINTAB-USER4 = I_AUFKTAB-USER4.
I_FINTAB-WERKS = I_AUFKTAB-WERKS.
I_FINTAB-AUART = I_AUFKTAB-AUART.
"Retrieving customer number from VIQMEL into final internal table
SELECT SINGLE PARNR FROM IHPA INTO G_PARNR WHERE OBJNR = I_AUFKTAB-OBJNR
AND PARVW = TEXT-110
AND PARNR IN S_KUNUM.
IF SY-SUBRC = 0.
I_FINTAB-KUNUM = G_PARNR.
ENDIF.
"Retrieving customer name from KNA1 into final internal table
SELECT SINGLE NAME1 FROM KNA1 INTO G_NAME1 WHERE KUNNR = G_PARNR.
IF SY-SUBRC = 0.
I_FINTAB-NAME1 = G_NAME1.
ENDIF.
"Retrieving purchase order number from PMSDO into final internal table
SELECT SINGLE BSTKD FROM PMSDO INTO G_BSTKD WHERE OBJNR = I_AUFKTAB-OBJNR .
IF SY-SUBRC = 0.
I_FINTAB-BSTKD = G_BSTKD.
ENDIF.
" Retrieving Required end date from AFKO into final internal table
SELECT SINGLE gltrp gstrp FROM afko INTO (g_gltrp,g_gstrp) WHERE aufnr = i_aufktab-aufnr.
IF sy-subrc = 0. "AFKO
i_fintab-gltrp = g_gltrp.
i_fintab-gstrp = g_gstrp.
endif.
ENDIF. "AUFKTAB
ENDIF. "VBAP
APPEND I_FINTAB.
CLEAR I_FINTAB.
I = I + 1.
ENDLOOP. "FPLA
ENDLOOP. "I_VBAK
*DELETE I_FINTAB WHERE NOT AUFNR IN S_AUFNR.
*DELETE I_FINTAB WHERE NOT KUNUM IN S_KUNUM.
LOOP AT i_fintab WHERE kunum IN s_kunum
AND gstrp IN s_gstrp
AND gltrp IN s_gstrp
AND werks IN s_werks
AND auart IN s_auart
AND name1 IN s_name1
AND aufnr IN s_aufnr
AND ktext IN s_ktext
AND bstkd IN s_bstkd.
i_finaltab = i_fintab.
APPEND i_finaltab.
CLEAR i_finaltab.
ENDLOOP.
I_FINALTAB[] = I_FINTAB[].
IF ( I_FINALTAB[] IS INITIAL ).
MESSAGE S010.
ENDIF.
SORT I_FINALTAB BY AUFNR.
*endloop.
ENDFORM. "SUB_SELECTION_FINALTAB1
&----
*& Form SUB_FIELDCAT_BUILD
&----
Defining the fieldcatalog to be used for the report
----
FORM SUB_FIELDCAT_BUILD.
PERFORM SUB_FIELDCAT USING '' 'KUNUM' TEXT-004 G_COL 'X'.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'NAME1' TEXT-005 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'AUFNR' TEXT-006 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'KTEXT' TEXT-007 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'BSTKD' TEXT-008 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'VBELN' TEXT-009 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'POSNR' TEXT-010 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'KDMAT' TEXT-011 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'ZMENG' TEXT-012 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'KBETR' TEXT-013 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'NETWR' TEXT-014 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'ARKTX' TEXT-015 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'AFDAT' TEXT-016 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'FPROZ' TEXT-017 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'USER4' TEXT-018 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'FKSAF' TEXT-019 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'WERKS' TEXT-020 G_COL ''.
ADD 1 TO G_COL.
PERFORM SUB_FIELDCAT USING '' 'AUART' TEXT-021 G_COL ''.
ADD 1 TO G_COL.
ENDFORM. "SUB_FIELDCAT_BUILD
&----
*& Form SUB_FIELDCAT
&----
Defining the fields in the fieldcatalog used for the report
----
--> L_TABNAME : Table name for the fieldcat
--> L_FNAME : Field name for the fieldcat
--> L_SEL : Field Description for the fieldcat
--> L_COL : Column position for the fieldcat
--> L_KEY : Key for the fieldcat
----
FORM SUB_FIELDCAT USING L_TABNAME TYPE SLIS_TABNAME
L_FNAME TYPE SLIS_FIELDNAME
L_SEL TYPE DD03P-SCRTEXT_L
L_COL TYPE I
L_KEY TYPE C.
I_FIELDCATTAB_WA-REF_TABNAME = L_TABNAME.
I_FIELDCATTAB_WA-FIELDNAME = L_FNAME.
I_FIELDCATTAB_WA-SELTEXT_L = L_SEL.
I_FIELDCATTAB_WA-COL_POS = L_COL.
I_FIELDCATTAB_WA-KEY = L_KEY.
APPEND I_FIELDCATTAB_WA TO I_FIELDCATTAB.
ENDFORM. " SUB_FIELDCAT
&----
*& Form SUB_FIELDCAT_DISPLAY
&----
Mapping the internal table I_FINALTAB with fieldcat
----
FORM SUB_FIELDCAT_DISPLAY.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = 'ZPSPAR11_SCHD_RATES'
IT_FIELDCAT = I_FIELDCATTAB
I_CALLBACK_TOP_OF_PAGE = 'TOP_OF_PAGE'
I_SAVE = 'A'
TABLES
T_OUTTAB = I_FINALTAB.
ENDFORM. "SUB_FIELDCAT_DISPLAY
&----
*& Form SUB_FIELDCAT_TITLE
&----
Define the Top-Of-Page contents
----
FORM SUB_FIELDCAT_TITLE.
I_TITLE_WA-TYP = TEXT-023.
I_TITLE_WA-INFO = TEXT-022.
APPEND I_TITLE_WA TO I_TITLE.
I_TITLE_WA-TYP = TEXT-024.
I_TITLE_WA-KEY = TEXT-025.
I_TITLE_WA-INFO = SY-UNAME.
APPEND I_TITLE_WA TO I_TITLE.
I_TITLE_WA-TYP = TEXT-024.
I_TITLE_WA-KEY = TEXT-026.
CONCATENATE SY-DATUM6(2) TEXT-027 SY-DATUM4(2) TEXT-027 SY-DATUM+0(4) INTO I_TITLE_WA-INFO.
APPEND I_TITLE_WA TO I_TITLE.
I_TITLE_WA-TYP = TEXT-024.
I_TITLE_WA-KEY = TEXT-028.
CONCATENATE S_GSTRP9(2) TEXT-027 S_GSTRP7(2) TEXT-027 S_GSTRP+3(4) INTO I_TITLE_WA-INFO.
IF NOT ( S_GSTRP-HIGH IS INITIAL ).
CONCATENATE S_GSTRP-LOW6(2) TEXT-027 S_GSTRP-LOW4(2) TEXT-027 S_GSTRP-LOW+0(4) TEXT-029
S_GSTRP-HIGH6(2) TEXT-027 S_GSTRP-HIGH4(2) TEXT-027 S_GSTRP-HIGH+0(4) INTO I_TITLE_WA-INFO.
ENDIF.
APPEND I_TITLE_WA TO I_TITLE.
ENDFORM. "SUB_FIELDCAT_TITLE
&----
*& Form TOP_OF_PAGE
&----
TOP-OF-PAGE
----
FORM TOP_OF_PAGE.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY = I_TITLE.
ENDFORM. "TOP_OF_PAGE
INCLUDE ZPSPAR11_INCLUDE_AF01.
thank u in advance .max points will be awarded since this is very urgent.
regards
siri
‎2006 May 31 1:06 PM
Hi
-
>before using <b>FOR ALL ENTRIES</b>
check if the internal table is initial.
-
> move corresponding takes time to compare the fields of the table & the internal table so instaed declare the itab in the same order as the fields in the data base table
-
> select * can b avoided (depends on ur req)
-
> avoid select statements in loop.
-
> <b>TRANSPORTING NO FIELDS</b> & <b>binary search</b> can b used along with <b>read</b> statement for more efficiency
‎2006 May 31 1:11 PM
Try to avoid select within loop. Try to avoid nested loops. Check if not all the select-option is initial.
Peter
‎2006 May 31 1:13 PM
hii
1) Remove corresponding from select satement
2) Remove * from select
<b>sort the table first before using for all entries, so that you can use binary search in the read command</b>
3) Select field in sequence as defined in database
4) Avoid unnecessary selects
i.e check for internal table not initial
5) Use all entries and sort table by key fields
6) Remove selects ferom loop and use binary search
7) Try to use secondary index when you don't have
full key.
😎 Modify internal table use transporting option
9) Avoid nested loop . Use read table and loop at itab
from sy-tabix statement.
10) free intrenal table memory wnen table is not
required for further processing.
In <b>SE30</b> ( ABAP Runtime Analysis),
First give the program name and click execute.
Now come back and click on Analyse.
This will give you the performance of the program.
Anything given in <b>RED barchart</b> means you need to fine tune it.
<b>Code Inspector</b> inspects your code and gives you information regarding the type of issue that you are facing(like performance, syntax error) so that you can correct it later.
Reward points if helpful
Regards
Naresh
‎2006 May 31 1:14 PM
Hi Sireesha,
I have very good PDF file on performance optimization.
Please give me your email id, I will forward the same to you.
<b>Also, reward points if it helps.</b>
‎2006 May 31 1:18 PM
Hi,
1) Remove move corresponding from select query
that is select the column in the order of internal
table field or rearrange the internal table fields
2) avoid selection in loop; because it connect the
database server in loop.
3) when ever u use for all entries check wheater it is
initial of not (dont perform selection initail table).
4) when ever ur going to read any value for the internal
table use binary search ( for binary search sort table on that key)
<b>Refer this link for further optimization tech.</b>
1. Debugger
http://help.sap.com/saphelp_47x200/helpdata/en/c6/617ca9e68c11d2b2ab080009b43351/content.htm
http://www.cba.nau.edu/haney-j/CIS497/Assignments/Debugging.doc
http://help.sap.com/saphelp_erp2005/helpdata/en/b3/d322540c3beb4ba53795784eebb680/frameset.htm
2. Run Time Analyser
http://help.sap.com/saphelp_47x200/helpdata/en/c6/617cafe68c11d2b2ab080009b43351/content.htm
3. SQL trace
http://help.sap.com/saphelp_47x200/helpdata/en/d1/801f7c454211d189710000e8322d00/content.htm
4. CATT - Computer Aided Testing Too
http://help.sap.com/saphelp_47x200/helpdata/en/b3/410b37233f7c6fe10000009b38f936/frameset.htm
5. Test Workbench
http://help.sap.com/saphelp_47x200/helpdata/en/a8/157235d0fa8742e10000009b38f889/frameset.htm
6. Coverage Analyser
http://help.sap.com/saphelp_47x200/helpdata/en/c7/af9a79061a11d4b3d4080009b43351/content.htm
7. Runtime Monitor
http://help.sap.com/saphelp_47x200/helpdata/en/b5/fa121cc15911d5993d00508b6b8b11/content.htm
8. Memory Inspector
http://help.sap.com/saphelp_47x200/helpdata/en/a2/e5fc84cc87964cb2c29f584152d74e/content.htm
9. ECATT - Extended Computer Aided testing tool.
http://help.sap.com/saphelp_47x200/helpdata/en/20/e81c3b84e65e7be10000000a11402f/frameset.htm
<b>Mark helpfull answers</b>
Regards
‎2006 May 31 1:46 PM
Hi Sireesha,
I had gone thru your code, there are lot of statement which need to change,may be rewriting the code is easier one . A few points you can consider in your code .
<u>In FORM SUB_SELECTION_TAB</u>.
1.create ranges for the plant and validate your select with ranges for the plant.
2.Create internal table i_aufk with your required fields only so that you can avoid into corresponding fields statement.
<u>In FORM SUB_SELECTION_FINALTAB1.</u>
1.Better YOu can use inner join for VBAK and VBAP , with your validations.
2.Wherever you are using Select * , try to avoid that and use your required fields only.
3.At the maximum try to avoid use select statements in side the loops.
4.Wherever possible user READ statements with Binary search.
5.You are using select Single statement inside the loop , better you can select the data outside the loop and with the help of READ statement you can achieve your requirement.
<i>
Hope This Info Helps YOU.</i>
Regards,
Raghav
‎2006 May 31 1:58 PM
1. Do not use like, its obsolete. Instead use type and data element.
2. Do not declare internal table with occurs 0. Also declare work area seperately, do not declare internal table with header line.
3. Do not use corresponding fields of in select statements. Its a very expensive SQL statement.
4.Do not use select * instead write all the fields which you need.
5. Avoid nested loops. Instead use loop at... read table ..... Also try to avoid select statements inside loops.
6. Use SQL trace, SLIN test, Runtime analysis to find the performance of your code.