BSEG select performance improvements

Advertisements

If you are trying to select document number details (i.e. BELNR) from table BSEG based on customer number (i.e. KUNNR) you are going to find the performance of this to be very slow and inefficient. You can do it and it might work ok in your test system where there is less data but often there are other tables available that you can retrieve the same data from in a much more efficient manner. Or alternatively, there are times when you can access data from other tables which then provides you with values that form part of a bseg index, which will also speed the select up a lot.

Advertisements

select document details (i.e. BELNR) from table BSEG based on customer number


  TYPES: BEGIN OF t_bseg,
     bukrs TYPE bsid-bukrs,
     belnr TYPE bsid-belnr,
     gjahr TYPE bsid-gjahr,
   END OF t_bseg.

  DATA: it_bseg TYPE STANDARD TABLE OF t_bseg,
        wa_bseg LIKE LINE OF it_docs.


LOOP AT it_custdetails INTO wa_custdetails.

*    In a production environment, this select will probably take a very long time 
*    as you are not using any of the key fields of bseg 
*    SELECT *
*      FROM bseg
*      INTO CORRESPONDING FIELDS OF TABLE it_bseg
*    WHERE bukrs EQ  wa_custdetails-bukrs
*      AND gjahr EQ  wa_custdetails-gjahr
*      AND kunnr EQ  wa_custdetails-kunnr.


*   ...but if you are selecting document data for a customer you can get the same 
*   data as bseg from BSID AND BSAD but much quicker as it keyed on customer (i.e. KUNNR)
    SELECT *
      FROM bsid
      INTO CORRESPONDING FIELDS OF TABLE it_bseg
     WHERE bukrs EQ  wa_custdetails-bukrs
       AND gjahr EQ  wa_custdetails-gjahr
       AND kunnr EQ  wa_custdetails-kunnr.

    SELECT *
      FROM bsad
      APPENDING CORRESPONDING FIELDS OF TABLE it_bseg
    WHERE bukrs EQ  wa_custdetails-bukrs
      AND gjahr EQ  wa_custdetails-gjahr
      AND kunnr EQ  wa_custdetails-kunnr.

endloop
Advertisements