简体   繁体   中英

Delete records from internal table and commit every 5000 record

Original statement [delete all record from ldt_log_data and commit only once]

DATA:ldt_log_data TYPE STANDARD TABLE OF zwfm_t_logs.
DELETE zwfm_t_logs FROM TABLE ldt_log_data.

COMMIT WORK.

To optimize the performance of the delete statement, im trying to delete the record with the key COMM_GUID and commit every 5000 record.

ZWFM_T_LOGS 表

My attempt:

LOOP AT ldt_log_data into w_ldt_log_data.
 
 delete table zwfm_t_logs from ldt_log_data with table key COMM_GUID = i_COMM_GUID.

COMMIT WORK.

I am getting stuck at how to delete the first 5000 and commit and continue this process until the deletion is completed. Any comment is greatly appreciated. Thanks

When you do a DELETE FROM TABLE , then the whole table gets sent to the database, and the program execution continues when the database reports back. So you can't sneak a COMMIT WORK in there*.

An alternative would be to loop through the whole table doing single line DELETE's and do a COMMIT WORK whenever sy-tabix MOD 5000 = 0 , but if your problem is performance, then that would be counter-productive because your program needs to wait for a database response for every single delete.

A compromise would be to copy the lines of your table into a new table in packets of 5000, send that table to the database and then do a database commit. You can copy a number of lines from one internal table to another by line index using the syntax APPEND LINES OF itab_1 FROM idx1 TO idx2 TO itab_2 .

DATA: ldt_log_data TYPE STANDARD TABLE OF zwfm_t_logs,
      lt_tmp LIKE ldt_log_data,
      lv_from TYPE i.

CONSTANTS cv_package_size TYPE i VALUE 5000.

" code to fill ldt_log_data with the data you want to delete

lv_from = 1.

WHILE lv_from <= lines( ldt_log_data).
   APPEND LINES OF ldt_log_data 
          FROM lv_from TO lv_from + cv_package_size - 1
          TO lt_tmp.
   DELETE TABLE zwfm_t_logs FROM lt_tmp.
   COMMIT WORK.
   CLEAR lt_tmp.
   lv_from = lv_from + cv_package_size.
ENDWHILE.

* I could imagine that there might be databases which allow to do that through %_HINTS , but you didn't say which database you are using. And anyway, relying on db-specific hints should generally be avoided, because you don't know if you want to change your database backend some day.

You could use the following pattern:

* Loop at your table
LOOP AT ...
  IF sy-tabix MOD 5000 = 0.
    COMMIT WORK.
  ENDIF.

* Delete the current record from DB
  DELETE ...
ENDLOOP:

COMMIT WORK.

A faster way to delete ALL records would be the following:

CALL FUNCTION 'DB_TRUNCATE_TABLE'
  EXPORTING
    tabname = 'ZWFM_T_LOGS'.

COMMIT WORK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM