简体   繁体   English

从内部表中删除记录并每 5000 条记录提交一次

[英]Delete records from internal table and commit every 5000 record

Original statement [delete all record from ldt_log_data and commit only once]原始语句[从ldt_log_data中删除所有记录并且只提交一次]

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.为了优化删除语句的性能,我尝试使用键 COMM_GUID 删除记录并每 5000 条记录提交一次。

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.我被困在如何删除前 5000 个并提交并继续此过程,直到删除完成。 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.当您执行DELETE FROM TABLE ,整个表将被发送到数据库,当数据库返回报告时,程序继续执行。 So you can't sneak a COMMIT WORK in there*.所以你不能在那里偷偷地进行COMMIT WORK *。

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.另一种方法是在整个表中循环执行单行 DELETE 并在sy-tabix MOD 5000 = 0时执行 COMMIT WORK,但如果您的问题是性能,那么这将适得其反,因为您的程序需要等待每次删除的数据库响应。

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.一种折衷方法是将表中的行以 5000 个数据包的形式复制到一个新表中,将该表发送到数据库,然后进行数据库提交。 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 .您可以使用语法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. * 我可以想象可能有允许通过%_HINTS执行此操作的数据库,但是您没有说明您正在使用哪个数据库。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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