Ovidiu Pocnet
Ovidiu Pocnet

Reputation: 589

When to use internal tables?

So, I have read that using internal tables increases the performance of the program and that we should make operations on DB tables as less as possible. But I have started working on a project that does not use internal tables at all.

Some details:

It is a scanner that adds or removes products in/from a store. First the primary key is checked (to see if that type of product exists) and then the product is added or removed. We use ‘Insert Into’ and ‘Delete From’ to add/remove the products directly from the DB table.

I have not asked why they do not use internal tables because I do not have a better solution so far.

Here’s what I have so far: Insert all products in an internal table, place the deleted products in another internal table.

Form update.
  Modify zop_db_table from table gt_table." – to add all new products
  LOOP AT gt_deleted INTO gs_deleted.
    DELETE FROM zop_db_table WHERE index_nr = gs_deleted-index_nr.
  ENDLOOP.  " – to delete products
Endform.

But when can I perform this update? I could set a ‘Save button’ to perform the update, but then there will be the risk that the user forgets to save large amounts of data, or drops the scanner, shutting it down or similar situations. So this is clearly not a good solution. My final question is: Is there a (good) way to implement internal tables in a project like this?

Upvotes: 2

Views: 1163

Answers (1)

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6023

internal tables should be used for data processing, like lists or arrays in other languages (c#, java...). From a performance and system load perspective it is preferred to first load all data you need into an internal table, then process that internal table instead of loading individual records from the database.

But that is mostly true for reporting, which is probably the most common type of custom abap program. You often see developers use select...endselect-statements, that in effect loop over a database table, transferring row after row to the report, one at a time. That is extremely slow compared to reading all records at once into an itab, then looping over the itab. More than once i've cut the execution time of a report down to a fraction by just eliminating roundtrips to the database.

If you have a good reason to read from the database or update records immediately, you should do so. If you can safely delay updates and deletes to a point in time where you can process all of them together, without risking inconsistencies, I'd consider than an improvement. But if there is a good reason (like consistency or data loss) to update immediately, do it.

Update: as @vwegert mentioned regarding the select-endselect statement, the statement doesn't actually create individual database queries for each row. The database interface of the application server optimizes the query, transferring rows in bulk to the application server. From there the records are transported to the abap report one by one (because in the report there is only the work area to store a single row), which has a significant performance impact especially for queries with large result sets. A select into an internal table can transport all rows directly to the abap report (as long as there is enough memory to hold them), as now there is the internal table to hold those records in the report.

Upvotes: 6

Related Questions