Showing posts with label performance. Show all posts
Showing posts with label performance. Show all posts

Wednesday, September 16, 2009

when and where we can use parallel cursor


Loop AT i_primary into wa_primary.
Loop AT i_secondary into wa_secondary from lv_index.
If ( wa_primary-field1 NE wa_secondary-field1 ) or
( wa_primary-field2 NE wa_secondary-field2 ).
Lv_index = sy-tabix.
Endif.
“ your code comes here “
Endloop.
Endloop.
To use this following should be true
1)      Both tables should be sorted with the same key ( in the above case field1 and field2 )
2)      The mapping between primary to secondary tables should be
a.       one to many ( for one record in primary there are multiple records in secondary )
b.      one to one (for one record in primary there is only one in secondary )
Please note that other than the above 2 other 2 scenarios (many to many & many to one) will not work with the above logic and make sure you don’t use in such case.
3)      The secondary table should not contain a record which does not map to the primary table ( this will not happen if the secondary is fetched using for all entries from primary )
4)      Please make sure you do not delete any record from primary before using the logic which will make the point 3 false  
5)      If you were to use a 'AND' in where statement of the inner loop you should use or in the inner loop if condition in the above logic. 
 

Monday, June 8, 2009

only way of parallel or Multi Threading on SAP

Sometimes you get programs where you might have done almost all the programming best practices and Indexing and other stuff but still the program will run very slow.On a time like this sometimes we might wonder is there anyway to do parallel processing or something like multi treading on SAP.
Hmm.... there is only one way that I can think about in SAP and we can get all most all features on Parallel processing from this way.


CALL FUNCTION 'Fetch_Data'
STARTING NEW TASK 'Fetch'
performing RETURN_FROM_WAIT ON END OF TASK
     EXPORTING
input            = input
TABLES
output             = output
EXCEPTIONS
COMMUNICATION_FAILURE = 1
SYSTEM_FAILURE        = 2.





Important : you cant use any import parameters on the function no this
method the only option is to using the RETURN_FROM_WAIT which is a
perform.


For Eg when you want to fetch data parallel do it using 2 functions and using the perform
you can have  a flag to make sure that fetching is completed.
  
This is just a over view I got this document from Daniel Perecky
Multi Threading

this has lots of information no how to program this way.
I would love to see some comments about this :)

Thursday, May 7, 2009

best way to code nested loops SAP


REPORT  ZPARALLEL_CURSOR.
TABLES:
  likp,
  lips.

DATA:
      t_likp  
TYPE TABLE OF likp,
      t_lips  
TYPE TABLE OF lips,
      t_lips1 
TYPE SORTED TABLE OF lips
      
WITH NON-UNIQUE KEY vbeln.




DATA:
      w_runtime1 
TYPE I,
      w_runtime2 
TYPE I,
      w_counter 
TYPE I,
      w_index 
LIKE sy-INDEX.

DATA:
      likp_l 
TYPE I,
      lips_l 
TYPE I.

PARAMETERS: rows TYPE i OBLIGATORY.

START-
OF-SELECTION.
SELECT *
FROM likp
INTO TABLE t_likp up to rows ROWS.



DESCRIBE TABLE t_likp LINES likp_l.

WRITE:/ 'records in likp: ' .
WRITE likp_l.

SELECT *
INTO TABLE t_lips
FROM lips
  
FOR ALL ENTRIES IN t_likp
  
WHERE vbeln = t_likp-vbeln.


DESCRIBE TABLE t_lips LINES lips_l.


WRITE:/ 'records in lips: ' .
WRITE lips_l.


**********************************************************************
**********************************************************************
**********************************************************************

GET RUN TIME FIELD w_runtime1.

LOOP AT t_likp INTO likp.
  
LOOP AT t_lips INTO lips WHERE vbeln EQ likp-vbeln.

  
ENDLOOP.
ENDLOOP.

GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

WRITE:/.
WRITE:/'Time taken on normal where condition:'.
WRITE: w_runtime2.

**********************************************************************
**********************************************************************
**********************************************************************
GET RUN TIME FIELD w_runtime1.
SORT t_likp BY vbeln.
SORT t_lips BY vbeln.

LOOP AT t_likp INTO likp.

  
LOOP AT t_lips INTO lips FROM w_index.
    
IF likp-vbeln NE lips-vbeln.
      w_index = sy-tabix.
      
EXIT.
    
ENDIF.

  
ENDLOOP.
ENDLOOP.


GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.


WRITE:/.
WRITE:/.
WRITE:/'Time taken with parallel cursor:' , w_runtime2.



**********************************************************************
**********************************************************************
**********************************************************************

GET RUN TIME FIELD w_runtime1.

SORT t_lips BY vbeln.
t_lips1[] = t_lips[].




LOOP AT t_likp INTO likp.
  
LOOP AT t_lips1 INTO lips WHERE vbeln EQ likp-vbeln.
  
ENDLOOP.

ENDLOOP.

GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

WRITE:/.
WRITE:/.
WRITE:/'Time taken for a sorted table with where condition:' , w_runtime2.





Thursday, April 23, 2009

Use of binary search option

When a programmer uses the read command, the table is sequentially searched. This slows down the processing. Instead of this, use the binary search addition. The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search. Binary search repeatedly divides the search interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.

Not Recommended

Read table int_fligh with key airln = ‘LF’.

Recommended

Read table int_fligh with key airln = ‘LF’ binary search.


source: http://www.thespot4sap.com/Articles/SAPABAPPerformanceTuning_BinarySearch.asp

Modifying a group of lines of an internal table

Use the variations of the modify command to speed up this kind of processing.

Not recommended

Loop at int_fligh.

If int_fligh-flag is initial.

Int_fligh-flag = ‘X’.

Endif.

Modify int_fligh.

Endloop.

Recommended

Int_fligh-flag = ‘X’.

Modify int_fligh transporting flag where flag is initial.


source: http://www.thespot4sap.com/Articles/SAPABAPPerformanceTuning_ModifyingGroupOfLines.asp


Monday, April 6, 2009

Using Sorted table and Index while processing Internal tables

There would have been many instances where we would have to process large entries in an internal table with a WHERE condition. This article is intended to demonstrate the comparison between three different methods in handling this situation.

First Method: The normal method used by most of us. Standard internal table processing using WHERE condition

Second Method: Same as above, but here we would be using the Sorted table

Third Method: Sorted table and using the Index

Following is the demo program illustrating the above three methods:

REPORT ZINTERNAL_TABLE_OPERATIONS.

* Program to find the best method in reading the internal tables

* Author: Suresh Kumar Parvathaneni

* Type declaration

TYPES:

BEGIN OF TY_MARA,

MATNR LIKE MARA-MATNR,

MTART LIKE MARA-MTART,

END OF TY_MARA.

* Internal table declaration

DATA:

T_MARA TYPE STANDARD TABLE OF TY_MARA,

T_MARA1 TYPE SORTED TABLE OF TY_MARA

WITH NON-UNIQUE KEY MTART.

* Variable declaration

DATA:

W_COUNTER TYPE I,

W_RUNTIME1 TYPE I,

W_RUNTIME2 TYPE I,

W_TABIX LIKE SY-TABIX.

* Table workarea definition

DATA:

WA_MARA TYPE TY_MARA.

SELECT MATNR " Material Number

MTART " Material Type

FROM MARA

INTO TABLE T_MARA.

T_MARA1[] = T_MARA[].

* CASE 1: Processing internal table using LOOP..WHERE Condition

GET RUN TIME FIELD W_RUNTIME1.

LOOP AT T_MARA INTO WA_MARA WHERE MTART EQ 'FHMI'.

ADD 1 TO W_COUNTER.

ENDLOOP.

GET RUN TIME FIELD W_RUNTIME2.

* Calculate Runtime

W_RUNTIME2 = W_RUNTIME2 - W_RUNTIME1.

WRITE W_RUNTIME2.

CLEAR W_COUNTER.

* CASE 2: Using a Sorted table

GET RUN TIME FIELD W_RUNTIME1.

LOOP AT T_MARA1 INTO WA_MARA WHERE MTART EQ 'FHMI'.

ADD 1 TO W_COUNTER.

ENDLOOP.

GET RUN TIME FIELD W_RUNTIME2.

* Calculate Runtime

W_RUNTIME2 = W_RUNTIME2 - W_RUNTIME1.

WRITE W_RUNTIME2.

CLEAR W_COUNTER.

* CASE 3: Using INDEX on a sorted table

GET RUN TIME FIELD W_RUNTIME1.

READ TABLE T_MARA1 INTO WA_MARA WITH KEY MTART = 'FHMI'.

IF SY-SUBRC EQ 0.

W_TABIX = SY-TABIX + 1.

ADD 1 TO W_COUNTER.

LOOP AT T_MARA1 INTO WA_MARA FROM W_TABIX.

IF WA_MARA-MTART NE 'FHMI'.

EXIT.

ENDIF.

ADD 1 TO W_COUNTER.

ENDLOOP.

ENDIF.

GET RUN TIME FIELD W_RUNTIME2.

* Calculate Runtime

W_RUNTIME2 = W_RUNTIME2 - W_RUNTIME1.

WRITE W_RUNTIME2.

source http://www.saptechnical.com/Tutorials/ABAP/InternalTable/Processing.htm


Performance Tuning using Parallel Cursor

check link


Google