While writing programs that access huge database tables or access db tables repeatedly (in a loop) etc., it is a good idea to build a buffer internally within your program (in an internal table) and then access these tables rather than going to the database for each loop. One way to do this would be to build up an internal table at each call to the database (while looping) and check in this buffer internal table for a corresponding entry before actually going into the database tables to retrieve entries. A second way could be to dump selected entries into an internal table (using select f1, f2.....fn into table itab where <conditions> ) and then loop on the internal table to retrieve data rather than accessing the database at each loop.
loop at itab1. read table itab_buffer key f1 = k1 ,,,,, fn = kn (binary search). If sy-subrc ne 0. select single relevant fields from dbtable where f1 =k1 , ,,,, fn = kn. move dbtable-fields to itab_buffer-fields. append itab_buffer. move dbtable-fields to itab1-fields. else. move itab_buffer to itab1-fields. modify itab1.