Вы находитесь на странице: 1из 32

RFC select Function Module

Performance Improvement
Technique

Applies to:
Release SAP 4.6B and greater.

Summary
Scenario: Existing application for many years ran successfully. It came to a point where this was no longer
possible due to the size of the database tables used in the program.
To achieve the running of a report to completion without having a time-out event occur, I found that it was
necessary to change the affected inner-join select to be converted into multiple RFC (remote function call)
contained function module calls. The design is to obtain the header level data first, then use this in multiple
smaller iterations in slice form for the select statement ‘for all entries’ clause.

Author: Daniel M. Perecky


Company: EDS
Created on: 20 January 2008

Author Bio
Started technical career in 1982 at IBM as a mainframe Technician. Went to programming and
SAP ABAP Application Development in 1990 and 1994 timeframes respectively. Holds B.A. in
Computer Science- 1998.
Company: EDS – Electronic Data Systems.
Work: 100% travel contractor working in SAP ABAP Application Development, with focus on
performance tuning.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 1
RFC select Function Module Performance Improvement Technique

Table of Contents
Methodology Used:.............................................................................................................................................3
Analysis of Bottleneck in Original Program.....................................................................................................3
Development Environment Overview..............................................................................................................3
Performance Enhancements Accomplished: ..................................................................................................3
Other Performance Enhancements to Consider:............................................................................................4
Development overview diagram...................................................................................................................................5
Select Statement ‘For All Entries’ Slice Size ..................................................................................................5
Slice Size Scope and Automation...................................................................................................................6
RFC Function Module Logging Details ...........................................................................................................6
Some perFormance Numbers (Comparisons Done on Development System): ................................................7
Development and Debug Hints...........................................................................................................................8
External Parameters .......................................................................................................................................8
Debug..............................................................................................................................................................8
This is a Multi-Session Debugging Environment..........................................................................................................8
Anomaly ..........................................................................................................................................................9
Example Code:..............................................................................................................................................10
Main Program Selection Screen ...................................................................................................................10
Function Group .............................................................................................................................................13
Top include File from Function Group...........................................................................................................14
ZMSEG_MKPF_Select .................................................................................................................................15
ZMKPF_Select Function Module ..................................................................................................................22
ZMSEG_Select Function Module..................................................................................................................24
ZSplitDates_to_Table Function Module........................................................................................................26
Miscellaneous Structures ..............................................................................................................................29
ZMatnr_Range ...........................................................................................................................................................29
ZMSEG ......................................................................................................................................................................29
ZMKPF_T...................................................................................................................................................................30
ZDatum_Range..........................................................................................................................................................30
Related Content................................................................................................................................................31
Disclaimer and Liability Notice..........................................................................................................................32

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 2
RFC select Function Module Performance Improvement Technique

Methodology Used:
• Perform many smaller select statements using smaller portions of the first extracted header level
data in RFC function modules, in order for each select to run concurrently in its own memory space
and system CPU process.
o The longer running select statement processes are viewable via transaction SM50 on a real-
time basis.

Analysis of Bottleneck in Original Program


The original program was attempting to process much more database table data compared to when the
program was originally developed, due to db table growth and longevity of the SAP implementation.

Development Environment Overview


• During development of the performance improvement, report output data output comparisons were
processed and compared for accuracy and completeness in the Dev system, which had less data
overall.
o In Dev, the original program was still able to run to completion.
• Performance tests were processed and measured in the QA system, which is more equivalent to the
production environment with respect to volume of data.

Performance Enhancements Accomplished:


• Export of select statement into a custom RFC function module. The main advantage is that many
iterative select statement processes run simultaneously. The general concept is:

o Previous inner-join select statement configuration:

Typical two-table inner join Select Statement

Large table to be
selected using small
Output data
table in an inner-join
Small Table
extracted select statement.
from header One table containing
level results data from the
For every row in the matching and combining
small table, find one or of rows from the two
tables.
more matching rows in
database table.

The above would work efficiently, unless the table sizes involved cause the program to time out.

The above design has approached the limitation of its usefulness in the program. For example, in QA1
the original select MKPF/MSEG inner-join would process approximately thirty six million header rows in
MKPF. From this, six million rows of MKPF data would be extracted for a typical program date range of
one month. This would be represented by the small table to the left above. This six million rows of MKPF
data would be used as to cross-reference to check against each row in the MSEG table. MSEG was
approximately one hundred and sixteen million rows in size (the middle box, above). The intention of the
design would be to ‘get in, and get out’, all in one database call, which minimizes the impact to the
database. With the number of rows that these tables contain, the processing of the data was not
impossible to do so in the time allotted. The result would be program time out conditions in QA1 and in
the production systems.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 3
RFC select Function Module Performance Improvement Technique

Other Performance Enhancements to Consider:


• DB Indexes
Table lock mechanisms
Dynamic select
• rsdb/max_blocking_factor - DB specific setting

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 4
RFC select Function Module Performance Improvement Technique

Development overview diagram


• The new RFC Function modules split up the above statement into multiple processes. Because we
cannot control the size of the big database table used in the select statement, the intent is to convert
the inner-join select to be multiple smaller sized for-all-entries type selects, with the ‘slice size’ as the
tool of reference. The configuration is:

Remote Function Module Select Statement Configuration


Smaller
Concurrent results table
Processes Large table to be from smaller
Small
Table selected from using select stmnt
extracted smaller slices of the
Slice of small table
from from left. original ‘for all entries’
header table. MSEG, etc.
level first. Smaller
Multiple processes are results table
Slice of small table generated to extract a from smaller
Can be from left. select stmnt
portion each of the
RFC.
original select results
data.
Slice of small table Smaller
from left. For every row in the results table
smaller sliced table, find from smaller
Slice size
one or more matching select stmnt
rows here.

The resultant output tables are combined in the function


module. This data is the same data that would have been
selected if the original inner-join select would have
completed in its allotted time.

Select Statement ‘For All Entries’ Slice Size


The number of concurrent processes is determined by the user-specified slice size parameter.
• The smaller the slice size yields:
o A greater number of smaller-sized concurrent processes.
o Smaller amount of data in each process.
• The larger slice size yields:
o Less concurrent processes to run.
o More data in each process.
o The overall run-time increasing and approaching the original time limitation that the
former select statement was conflicting with.
• The slice value is typically very dependent upon how the program is run from the selection
screen.
o Typical production environment is to set the slice size from a field available on the
selection screen field- to an optimum value, then periodically readjust this to
compensate for growing database and internal table sizes.
o The size of the initial ‘for all entries’ table, number of slices, rows per slice, rows returned
from each separate RFC process, total rows returned from all RFC processes, total rows
returned from main RFC function module back to the program are visible via an RFC-
enabled function module custom developed.
• This is outside the scope of this document.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 5
RFC select Function Module Performance Improvement Technique

As in indexes, there is an optimal balance for each select statement, internal table, and for each SAP
system. As a starting reference, it is acknowledged that 9-10 processes is the goal achieve. In terms of
database parameters:

• More processes result in database contention.


• Less processes would result in an excessive run time scenario.

If the data selected is large enough, transaction SM50 can be used to display process running times and
table name for each RFC function module call.

Slice Size Scope and Automation


To extend this further, one proposed methodology:
• The slice size parameter can be incorporated into the TVARV table with other program table
slice size parameters.
o These can then be further packaged into the context of an overall database monitoring
and adjusting program. This program can conceivably automatically adjust each slice
size parameter, to achieve the optimal efficiency of the programs and the database.
• This is presently outside the scope of this document.

RFC Function Module Logging Details


In the original code, there was a logging facility available that logs how many rows all the slice sizes are,
how many rows of data is returned per slice, with a timestamp for each row and the CPU run time
associated with each RFC function module call. From the main program, the total result row count
returned is logged for comparison purposes.
• This can be set to be enabled or disabled from a checkbox parameter on the selection screen.
• The log is inserted into a database table with row numbers ordered by occurrence via a static
function module counter.
• To avoid the collection of redundant data, the original version was designed to erase all the rows
in the log table from the previous run of the program.
o This was implemented in the Initialization event in the program.
• To remain focused on the RFC utilization methodology, the expansion of this concept would
remain outside the scope of this document.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 6
RFC select Function Module Performance Improvement Technique

Some perFormance Numbers (Comparisons Done on Development System):

Z_Original_Version Z_Slice_Performance enhanced version

Variant 1 – Jan 2, 2006 to Jan 31, 07 Variant 1 – Jan 2, 2006 to Jan 31, 07

• ABAP- 452,127 microsecs • ABAP-5,675,890 microsecs

• DB- 382,248,113 microscs • DB- 287,923 microsecs

• System - negligible • System - negligible

• Ttl- 382,714,590 • Ttl- 5,963,813

Variant 2 – Jan 2, 2005 to Jan 31, 05 Variant 2 – Jan 2, 2005 to Jan 31, 05

• ABAP- 97,034 microsecs • ABAP- 153,740 microsecs

• DB- 383,613,944 microsecs • DB- 25,317 microsecs

• System- 14,009 microsecs • System- 15,346 microsecs

• Ttl- 383,724,987 • Ttl- 194,403

Variants data used was the same, and program data output compared to be equal for both.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 7
RFC select Function Module Performance Improvement Technique

Development and Debug Hints


External Parameters
1. Passing parameters to RFC function modules require that the fields and structures are defined
externally in the data dictionary.
2. Similarly, using common structures in the function module group for RFC are required to be defined
in a common file such as the function module group ‘top’ include file.
• i.e. Using ‘type-pools rsds’ in a function module will not provide rsds related values visibility
to all forms in one function module.

Debug
Write statements, or Pop-up to confirm type function modules in the ‘Performing xxFormxx at end of task’
forms do not execute.

This is a Multi-Session Debugging Environment


• When debugging, ensure that you are not almost to the limit with regard to SAP sessions. If you are,
then when the RFC session is to be generated, a SAP Resource Error message will be generated.
• To go to the called RFC function module session in debug mode:
o Place a break-point on the statement immediately before the call to the RFC function module
o Single-step into the RFC function module call
ƒ The SAP system will place the program execution into some system level code.
o Hit F7 button until you have returned to the original main calling statement of the RFC
function module in the calling program.
o While the program execution is still on the call to the RFC function module, use F5, to
single-step into the RFC debug session. A new debug session for this RFC process will be
generated.
o When the RFC session is complete, resume program execution in the original calling
program debug session.
• To debug in ‘Performing xxFormxx at end of task’ form:
o A break-point needs to be in that data-gathering form.
ƒ Otherwise the debugger will execute it, but not let the application developer step
through the code.
• If you are in a sub-debug session for a called RFC function module, ensure that the timing of the
main debug session is not exceeded. In other words, the RFC sub-debug sessions are to be active
only for a limited time.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 8
RFC select Function Module Performance Improvement Technique

Anomaly
Once in a while, it seems that the divide calculation adds one more to the result amount:

NumSteps_Gjahr – the number of steps for a given year, should be ‘1’ in the above program debug state.

If the above does occur, the code in this example is able to handle this without selecting repeating rows of
data. This is accomplished by: Extra iterations result in the x and y indexes incrementing past the point of the
main ‘for all entries’ table rows to select slices. In our example, this is it_mkpf_mjahr. In this case, the
statement:
• Append lines of it_mkpf_mjahr from indx to indy to it_mkpf_slice will be of no effect.
The other part of the built-in safeguard is that the table select RFC function module (in here, ZMSEG_Select)
will perform the select statement only if the ‘for all entries’ table contains at least one row.

Note 02-21-08: This problem is further resolved by using the variable:


NumRowsCovered_Prelim Type I, “ new. Check initial rows total.

The code for this does a preliminary check to see if the initially calculated NumSteps, multiplied by the slice
size, is less than the total number of rows of the internal table that is being sliced. If so, then the mod division
calculation is done.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 9
RFC select Function Module Performance Improvement Technique

Example Code:
This code is designed to retrieve an aggregated set MKPF and MSEG table data for multiple posting date
years.

The example code is simplified to focus only on the multiple RFC performance enhancement technique. It is
composed of:
• a main report type program
• a main RFC enabled function module
• two table-select RFC enabled function modules
• a utility function module
• various structures defined in the data dictionary.

Main Program Selection Screen

<html>
<head>
<title>SDN – Report for RFC source code example</title>
<link rel="stylesheet" type="text/css" href="/sdn/css/csin.css" />
<link rel="stylesheet" type="text/css" href="/sdn/css/sdn.css" />
</head>
<body>
*&---------------------------------------------------------------------*
*& Report for RFC source code example
*&
*&---------------------------------------------------------------------*
*& Author: Daniel M. Perecky
*& Date : 11-12-2007
*&---------------------------------------------------------------------*
REPORT ZTEST0011.
Tables: Mseg, Mkpf.

Parameter: p_spost like syst-datum obligatory,


p_epost like syst-datum obligatory.
Selection-Screen skip.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 10
RFC select Function Module Performance Improvement Technique

Select-Options S_Matnr for Mseg-Matnr.


Selection-Screen skip.
Parameter: p_TblSlc(6) type N Default 160 memory id p_tblslc.
Selection-Screen skip.
*----------------------------------------------------------------------
Data: Lns type I.
Data: MSEG_MKPF_SELECT_Flg Type I.
Data: It_Mseg type ZMseg Occurs 0 with header line.
*----------------------------------------------------------------------
Initialization.
p_spost = Syst-Datum.
p_epost = Syst-Datum + 30.
*----------------------------------------------------------------------
Start-of-Selection.
*Call RFC function code from main program:
MSEG_MKPF_SELECT_Flg = 0.
CALL FUNCTION 'ZMSEG_MKPF_SELECT'
Starting new task 'MSEG_MKPF_SELECT'
Destination in Group Default
Performing Get_MSEG_MKPF_SELECT on End of Task
* IMPORTING " cannot use in this statement. Import at End of
Task form.
EXPORTING
P_SPOST = p_spost " start post date
P_EPOST = p_epost " end post date
P_TblSlice = p_TblSlc " slice size
TABLES
IT_MSEG = it_mseg " returned data
S_MATNR = s_matnr " matnr table from selection screen
EXCEPTIONS
COMMUNICATION_FAILURE = 1
SYSTEM_FAILURE = 2
RESOURCE_FAILURE = 3.
CASE SY-SUBRC.
WHEN 0.
Wait Until MSEG_MKPF_SELECT_Flg eq 1. " halts program execution until flag is
incremented
Commit Work.
WHEN 1 OR 2. "Handling of communication and system failure
.
. " handle comm. or system error

.
Message Id 'SY' Type 'E' Number 003 with 'an appropriate' 'error' 'msg'.
WHEN 3. "No resources available at present
.
. " handle resource limitation error

.
Message Id 'SY' Type 'E' Number 003 with 'an appropriate' 'error' 'msg'.
EndCase.
*----------------------------------------------------------------------
Describe table It_Mseg Lines Lns.
Write: / 'It_Mseg table size retrieved:', Lns.
Exit.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 11
RFC select Function Module Performance Improvement Technique

*&---------------------------------------------------------------------*
*& Form Get_MSEG_MKPF_SELECT
*&---------------------------------------------------------------------*
* Author: Daniel Perecky
*&---------------------------------------------------------------------*
* Purpose: This is the form specified in the call RFC function module
* statement.
* Note: Once completed, program execution resumes after the wait
* statement condition is met, above.
*----------------------------------------------------------------------*
Form Get_MSEG_MKPF_SELECT Using Name.
Data: Lns Type i.

If Name = 'MSEG_MKPF_SELECT'.
Receive results from function 'ZMSEG_MKPF_SELECT'
Tables it_mseg = it_mseg.
Add 1 to MSEG_MKPF_SELECT_Flg. " When data is received, increment flag for wait,
above.
If Syst-Subrc = 0.
Describe table it_mseg lines lns.
Write: / 'Get_Mseg_Mkpf_Select returned:', Lns, 'Lines'.
Endif.
Endif.
EndForm.
</body>

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 12
RFC select Function Module Performance Improvement Technique

Function Group

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 13
RFC select Function Module Performance Improvement Technique

Top include File from Function Group


• Holds variables common to forms in various function modules.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 14
RFC select Function Module Performance Improvement Technique

ZMSEG_MKPF_Select
Function module is RFC enabled, per the attributes tab:

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 15
RFC select Function Module Performance Improvement Technique

<html>
<head>
<title>Function Module ZMSEG_MKPF_Select</title>
<link rel="stylesheet" type="text/css" href="/sdn/css/csin.css" />
<link rel="stylesheet" type="text/css" href="/sdn/css/sdn.css" />
</head>
<body>
FUNCTION ZMSEG_MKPF_SELECT.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(P_SPOST) TYPE BUDAT
*" VALUE(P_EPOST) TYPE BUDAT
*" VALUE(P_TBLSLICE) TYPE SU_BSEG DEFAULT 250
*" TABLES
*" S_MATNR STRUCTURE ZMATNR_RANGE
*" IT_MSEG STRUCTURE ZMSEG
*"----------------------------------------------------------------------
* Date: 11/14/07 *
* Author: D Perecky *
* Title: RFC Function module code example. Main function module *
*----------------------------------------------------------------------*
* Change History: *
* Author Date Request# Description *
* ---------------------------------------------------------------------*

Data: IT_MKPF Type ZMKPF_T Occurs 0 with header line,


IT_MKPF_Slice Type ZMKPF_T Occurs 0 with header line,
IT_MKPF_Stg Type ZMKPF_T Occurs 0 with header line.

Data: DTRnge Type ZDatum_Range Occurs 0 with header line.


Data: MKPFLock Type I Value 0.
Data: MatnrLns Type I.
Data: Lns Type I,
Sz type I.
Field-Symbols <fs>.

Data: AddPrevious Type I Value 1.


Data: Indx Like Syst-Tabix,

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 16
RFC select Function Module Performance Improvement Technique

Indy like Syst-Tabix.


Data: NumSteps_Gjahr Type I, " ZMSEG_Select call counter for 1 year of data
NumRowsCovered_Prelim Type I, “ new. Check initial rows total.
NumSteps_Total Type I,
ModRslt Type I.

Data: N_Loop(4) type N,


NumSteps_N(7) Type N,
Lns_n(7) Type N,
i_TblSlice type I.

Data: MKPF_RC Like Syst-Subrc.


Data: SetRc Like Syst-Subrc.

Data: CurrMjahr Type Mjahr.

Data: N_Subrc(7) Type N.

Data: Rfc_Dest Like RFCDes-RFCDest.

*+--------------------------------------------------------------------+*
*| Start of Selection |*
*+--------------------------------------------------------------------+*
Clear: it_mseg, it_mkpf_mjahr, it_Mkpf_Slice, DtRnge, CurrMjahr,
Numsteps_Gjahr, Numsteps_Total.

Refresh: it_mkpf_mjahr, it_Mseg, it_Mkpf_Slice, DtRnge.

MKPF_Select_Flg = 0.

* Need to select bkpf bsim data by 1 year at a time: large table size.
* Bkpf size on QA- Unknown. always times out using 'num of rows'.

CALL FUNCTION 'ZSPLITDATES_TO_TABLE'


EXPORTING
P_SPOST = P_SPOST
P_EPOST = P_EPOST
TABLES
T_DATE_RANGE = DtRnge.

n_Loop = 1. " ZMSEG_Select loop num calls (not Mjahrloop ctr)


Loop at DtRnge. " The main loop.
Clear: it_mseg, it_Mkpf_Slice, it_mkpf_Mjahr, CurrMjahr.
Refresh: it_mseg, it_Mkpf_Slice, it_Mkpf_Mjahr.

AddPrevious = 1.

CurrMjahr = DtRnge-Low+0(4).
* If we are looking for older data, make sure that it exists.
If CurrMjahr <= 2005.
Select Single Mjahr from MKPF
Into MKPF-Mjahr
Where Mjahr = CurrMjahr.
If Syst-Subrc = 4.
AddPrevious = 0.
Endif.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 17
RFC select Function Module Performance Improvement Technique

Endif.
If AddPrevious = 1 or CurrMjahr > 2005.

* Generate new task name for each call, if there is > 1.


Concatenate 'MKPFSLC' CurrMjahr+3(1) into Syst-Lisel.
Sz = Strlen( Syst-Lisel ).
Assign Syst-Lisel+0(Sz) to <fs>. " Task/<fs> is length of 8 max

CALL FUNCTION 'ZMKPF_SELECT'


Starting new task <fs> " Task name is length of 8 max
Destination in Group Default "Mandatory for parallel asynch
Performing Get_RFC_MKPF on End of Task
EXPORTING " Can export, but not doing in this case
Yr_MJahr = CurrMjahr
* IMPORTING " Cannot import or specify importing parameter
TABLES
IT_MKPF = IT_MKPF_Mjahr " results table for 1 year
EXCEPTIONS
COMMUNICATION_FAILURE = 1
SYSTEM_FAILURE = 2
RESOURCE_FAILURE = 3.
MKPF_RC = Syst-Subrc.
CASE MKPF_RC.
WHEN 0.
Wait Until MKPF_Select_Flg eq 1.
Commit Work.
Clear MKPF_Select_Flg. " for the next year's select
WHEN 1 OR 2.
" Handle general RFC Error

* Handling of communication and system failures


Message id 'SY' Type 'E' Number '002' with 'General RFC Error'.
When 3. " insufficient resources- wait to see if they free up

* Wait again until MKPF_Select_Flg eq 1 up to 298 seconds.


If Syst-Subrc = 0.
"
Else.
" Handle SAP Resource failure.
Endif.

When Others.
" Handle other RFC failure.
Endcase.

*---------------------------------------------------------------
If MKPF_RC = 0. " Any data for this MjahrLoop year?

* Append current year it_mkpf_mjahr data to main it_mkpf_Stg tbl.


Append lines of it_mkpf_mjahr to it_Mkpf_Stg.

Clear: it_mseg, it_mkpf_mjahr.

*---------------------------------------------------------------
Numsteps_Gjahr = 0.
i_TblSlice = p_TblSlice.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 18
RFC select Function Module Performance Improvement Technique

Describe table it_mkpf_mjahr Lines Lns.


If i_TblSlice <= 0.
Message id 'SY' Type 'E' Number '002' with
'passed in Table Slice value too small'.
Endif.
If i_TblSlice > Lns.
i_TblSlice = Lns.
* Message- optional
Endif.
* Calculate number of segments to process it_mkpf_mjahr.
If i_TblSlice < Lns.
NumSteps_Gjahr = Lns / i_TblSlice.
* The above should suffice, but checking results first before
* calculating the Mod value just in case.
NumRowsCovered_Prelim = i_TblSlice * NumSteps_Mjahr.
If NumRowsCovered_Prelim < Lns.
ModRslt = Lns Mod i_TblSlice. " Any remaining rows?
If ModRslt <> 0.
Add 1 to Numsteps_Gjahr.
Endif.
Endif.
Else.
Numsteps_Gjahr = 1.
Endif.
* Set initial indexes
Indx = 1.
Indy = Indx + i_TBLSLICE - 1.

numsteps_n = Numsteps_Gjahr.

* Start Do loop.
Do Numsteps_Gjahr times.

* Process selection criteria for all sliced entries of it_mkpf_mjahr.


Append lines of it_mkpf_mjahr from indx to indy to it_mkpf_slice.

* Generate unique task name for each process.


Concatenate Syst-Repid+2(3) CurrMjahr+3(1) n_loop
into Syst-Lisel.
Sz = Strlen( Syst-Lisel ).
Assign Syst-Lisel+0(Sz) to <fs>. " Task/<fs> is length of 8 max

CALL FUNCTION 'ZMSEG_SELECT'


Starting new task <fs>
Destination in Group Default "Default - mandatory for parallel asynch
Performing Get_RFC_MSEG on End of Task
TABLES
MatnrTbl = S_Matnr
IT_MKPF_SLICE = It_Mkpf_Slice
IT_MSEG = It_Mseg
IT_Matnr = S_Matnr
Exceptions
Communication_Failure = 1 Message Msg
System_Failure = 2 Message Msg.
SetRc = Syst-Subrc.
Case SetRc.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 19
RFC select Function Module Performance Improvement Technique

When 0.
Append <fs> to T_Tasks.
Add 1 to n_loop. " For next loop.
When 1 or 2.
Message id 'SY' Type 'E' Number '002' with Msg.
When 3. " insufficient resources- wait to see if they free up
Wait Until n_task => n_loop up to 298 seconds. " max is 5 min.
If Syst-Subrc = 0.
Add 1 to n_loop. " For next loop.
Else.
Concatenate 'SAP Resource Failure, RFC Dest: ' rfc_Dest
into Syst-lisel.
Message id 'SY' Type 'E' Number '002' with Syst-Lisel.
Endif.
When Others.
N_subrc = Syst-Subrc.
Concatenate 'Other RFC Failure, Syst-Subrc: ' n_subrc
into Syst-lisel.
Message id 'SY' Type 'E' Number '002' with Syst-Lisel.
Endcase.

Refresh it_Mkpf_slice.
Indx = Indx + i_TblSlice. " Increment start index.
Indy = Indy + i_TblSlice. " Increment end index.

EndDo. " Do NumTimes times to process it_mkpf_mjahr in slices


Endif. " If Mjahrlns > 0.
Numsteps_Total = Numsteps_Total + Numsteps_Gjahr.
Endif. " If first it_mkpf_mjahr select is good...
EndLoop. " Loop at MjahrLoop internal table.

Wait Until n_task => NumSteps_Total. "no explicit limit: can be up to 298 seconds.
"Wait statement only works in Asynchronous RFC
* Continue looking for data until table t_Tasks is empty.

* Done with RFC select loop. Combine table fields for program.
It_Mseg[] = It_Mseg_Stg[].
Refresh: It_Mseg_Stg.
Free: It_Mseg_Stg. "free for performance
it_Mkpf[] = it_Mkpf_Stg[].
Refresh: it_Mkpf_Stg.
Free: it_Mkpf_Stg. "free for performance
SORT: it_mseg BY mjahr mblnr, it_Mkpf by mjahr mblnr.

* Combine MKPF and MSEG data into an aggregated result.


Loop at it_mseg.
* Binary Search yields significant performance gain.
Read table it_Mkpf with key
mjahr = it_mseg-mjahr mblnr = it_mseg-mblnr Binary Search.
If Syst-Subrc = 0.
move it_Mkpf-Budat to it_mseg-budat.
move it_Mkpf-xblnr to it_mseg-xblnr.
move it_Mkpf-bktxt to it_mseg-bktxt.
Modify it_mseg.
Endif.
EndLoop.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 20
RFC select Function Module Performance Improvement Technique

ENDFUNCTION.

*&---------------------------------------------------------------------*
*& Form Get_RFC_MKPF
* Purpose: Data retrieval from RFC function module.
*&---------------------------------------------------------------------*
* Author: Daniel M. Perecky
*----------------------------------------------------------------------*
Form Get_RFC_MKPF Using Name.
If Name+0(7) = 'MKPFSLC'.
Receive results from function 'ZMKPF_SELECT'
Tables IT_MKPF = it_mkpf_mjahr.
Add 1 to MKPF_Select_Flg.
Endif.
EndForm. "Get_RFC_MKPF

*---------------------------------------------------------------------*
* Form Gather_Mseg_Info *
* Purpose: Data retrieval from RFC function module.
*&--------------------------------------------------------------------*
* Author: Daniel M. Perecky
*---------------------------------------------------------------------*
Form Get_RFC_MSEG Using Name Type C.
Data: Lns Type I.

If Name+0(3) = Syst-Repid+2(3) and Name+3 ca '0123456789'.


Add 1 to n_task. " Process/add if there is data or not
Describe table T_Tasks Lines Lns.
If Lns > 0.
Read table T_Tasks with key Spawnedt = Name.
If Syst-Subrc = 0.

Receive results from function 'ZMSEG_SELECT'


Tables It_Mseg = It_Mseg
EXCEPTIONS
COMMUNICATION_FAILURE = 1 MESSAGE MSG
SYSTEM_FAILURE = 2 MESSAGE MSG.
reT_SUBRC = SY-SUBRC.
If Ret_Subrc <> 0.
Write: /.
Write: / Msg.
Write: /.
Else.
Endif.
Delete T_Tasks Index Syst-Tabix.
Append Lines of It_Mseg to It_Mseg_Stg.
Refresh It_Mseg.
Endif.
Endif.
Endif.
EndForm. "Get_RFC_MSEG
</body>

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 21
RFC select Function Module Performance Improvement Technique

ZMKPF_Select Function Module


• RFC Enabled

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 22
RFC select Function Module Performance Improvement Technique

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 23
RFC select Function Module Performance Improvement Technique

ZMSEG_Select Function Module


• RFC Enabled

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 24
RFC select Function Module Performance Improvement Technique

<html>
<head>
<title>ZMSEG_Select</title>
<link rel="stylesheet" type="text/css" href="/sdn/css/csin.css" />
<link rel="stylesheet" type="text/css" href="/sdn/css/sdn.css" />
</head>
<body>
FUNCTION ZMSEG_SELECT.
*"----------------------------------------------------------------------
*"Local Interface:
*" TABLES
*" IT_MKPF_SLICE STRUCTURE ZMKPF_T
*" IT_MSEG STRUCTURE ZMSEG
*" IT_MATNR STRUCTURE ZMATNR_RANGE
*"----------------------------------------------------------------------
Data: MKPF_Slice_Lns Type I.
Describe table it_Mkpf_Slice Lines MKPF_Slice_Lns.
If Mkpf_Slice_Lns > 0.
SELECT mblnr
mjahr
zeile " item
bwart
matnr
werks
lgort
Into TABLE it_mseg
FROM mseg
For all entries in it_Mkpf_Slice
WHERE
mblnr = it_Mkpf_Slice-mblnr and
mjahr = it_Mkpf_Slice-mjahr and
matnr in it_matnr and
sobkz EQ space.
Write: / 'ZMSEG_Select Subrc:', Syst-Subrc, '/ lines:', Syst-DBCnt.
Endif.
ENDFUNCTION.
</body>

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 25
RFC select Function Module Performance Improvement Technique

ZSplitDates_to_Table Function Module


• non-RFC

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 26
RFC select Function Module Performance Improvement Technique

<html>
<head>
<title>ZSplitDates_to_Table source code</title>
<link rel="stylesheet" type="text/css" href="/sdn/css/csin.css" />
<link rel="stylesheet" type="text/css" href="/sdn/css/sdn.css" />
</head>
<body>
FUNCTION ZSPLITDATES_TO_TABLE.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(P_SPOST) TYPE BUDAT
*" VALUE(P_EPOST) TYPE BUDAT
*" TABLES
*" T_DATE_RANGE STRUCTURE ZDATUM_RANGE
*"----------------------------------------------------------------------
*" Purpose: Take a range of post dates- BUDAT, and break up in yearly
* increments into separate table rows.
*"----------------------------------------------------------------------
*" Author: Daniel M. Perecky 11-14-07
*"----------------------------------------------------------------------
Data: NumYears Type I.
Data: P_SPostMjahr(4) Type C,
P_EPostMjahr(4) Type C,
CurrMjahr Type I,
CurrMjahr_N(4) Type N.

Data: YrCtr Type I.

NumYears = P_EPost+0(4) - P_Spost+0(4).


Add 1 to NumYears.

If NumYears = 1.
T_DATE_RANGE-Sign = 'I'. T_DATE_RANGE-Option = 'BT'.
T_DATE_RANGE-Low = P_Spost. T_DATE_RANGE-High = P_EPost.
Append T_Date_Range.
*----------------------------------------------------------------
ElseIf NumYears = 2.
* Get year range

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 27
RFC select Function Module Performance Improvement Technique

P_SPostMjahr = P_Spost+0(4).
P_EPostMjahr = P_EPost+0(4).
* Start Year
T_DATE_RANGE-Sign = 'I'. T_DATE_RANGE-Option = 'BT'.
T_DATE_RANGE-Low = P_SPost.
Concatenate P_SPostMjahr '1231' into T_DATE_RANGE-High.
Append T_Date_Range.
* End Year
T_DATE_RANGE-Sign = 'I'. T_DATE_RANGE-Option = 'BT'.
Concatenate P_EPostMjahr '0101' into T_DATE_RANGE-Low.
T_DATE_RANGE-High = P_EPost.
Append T_Date_Range.
*----------------------------------------------------------------
ElseIf NumYears > 2.
YrCtr = 1.
* Get year range
P_SPostMjahr = P_Spost+0(4).
P_EPostMjahr = P_EPost+0(4).

CurrMjahr = P_SPostMjahr.
Do NumYears Times.
If YrCtr = 1.
* Start Year
T_DATE_RANGE-Sign = 'I'. T_DATE_RANGE-Option = 'BT'.
T_DATE_RANGE-Low = P_SPost.
Concatenate P_SPostMjahr '1231' into T_DATE_RANGE-High.
Append T_Date_Range.
Endif.

If YrCtr > 1 and YrCtr < NumYears.


CurrMjahr_N = CurrMjahr.
T_DATE_RANGE-Sign = 'I'. T_DATE_RANGE-Option = 'BT'.
Concatenate CurrMjahr_N '0101' into T_DATE_RANGE-Low.
Concatenate CurrMjahr_N '1231' into T_DATE_RANGE-High.
Append T_Date_Range.
Endif.

If YrCtr = NumYears.
* End Year
T_DATE_RANGE-Sign = 'I'. T_DATE_RANGE-Option = 'BT'.
Concatenate P_EPostMjahr '0101' into T_DATE_RANGE-Low.
T_DATE_RANGE-High = P_EPost.
Append T_Date_Range.
Endif.

Add 1 to YrCtr.
Add 1 to CurrMjahr.
EndDo.
Endif.
ENDFUNCTION.
</body>

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 28
RFC select Function Module Performance Improvement Technique

Miscellaneous Structures

ZMatnr_Range

ZMSEG

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 29
RFC select Function Module Performance Improvement Technique

ZMKPF_T

ZDatum_Range

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 30
RFC select Function Module Performance Improvement Technique

Related Content
Calling RFC Function Modules locally

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 31
RFC select Function Module Performance Improvement Technique

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not
supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document,
and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or
code sample, including any liability resulting from incompatibility between the content within this document and the materials and
services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this
document.

SAP DEVELOPER NETWORK | sdn.sap.com BUSINESS PROCESS EXPERT COMMUNITY | bpx.sap.com


© 2008 SAP AG 32