Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to optimize a simple loop over 200,000 records

I am wondering if anyone can optimize following code:

LOOP AT dispinstock.
  SELECT matnr SUM( gesme ) SUM( verme ) SUM( einme ) SUM( ausme )
         INTO (dispinstock-matnr, dispinstock-gesme,
               dispinstock-verme, dispinstock-einme, dispinstock-ausme)
         FROM lqua
         WHERE matnr = dispinstock-matnr
         AND lgnum = 'xxxxx'
         AND ( lgtyp IN zsd_t301_n
          OR ( lgtyp >= '900' AND lgtyp <= '903' ) )
          GROUP BY matnr.
    MODIFY dispinstock.
  ENDSELECT.
ENDLOOP.

dispinstock 170.000 records,

LQUA 210.000 records (will be larger > 1.500.000 records soon)

This loop take more than 3 minutes. Would it be better to use hashed table instead? Any help or idea would be appreciated.

like image 658
praethorian Avatar asked Dec 22 '22 11:12

praethorian


1 Answers

Take the select out of the loop - pull all the data you need from lqua into a separate internal table in a single select statement. Then do a read on the second table inside the loop. Use a hash/sorted table or use a binary search.

like image 96
Bryan Cain Avatar answered Dec 26 '22 11:12

Bryan Cain