需要优化超过 200,000 条记录的简单循环

发布于 2024-11-26 10:57:47 字数 650 浏览 2 评论 0原文

我想知道是否有人可以优化以下代码:

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 条记录,

LQUA 210.000 条记录(很快就会更大> 1.500.000 条记录)

这个循环需要超过3分钟。 使用哈希表会更好吗? 任何帮助或想法将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-12-03 10:57:48

将 select 从循环中取出 - 在单个 select 语句中将您需要的所有数据从 lqua 提取到单独的内部表中。然后在循环内读取第二个表。使用哈希/排序表或使用二分搜索。

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.

小草泠泠 2024-12-03 10:57:48

您还应该考虑使用字段符号而不是使用修改。

field-symbols: <dispinstock> like line of dispinstock.
loop at dispinstock assigning <dispinstock>.
  " some work
  <dispinstock>-gesme = new value..
  "...
 endloop

这样您就可以减少读取 dispinstock 表并直接更改值的次数。

You should also think about using a field symbol rather than using the modify.

field-symbols: <dispinstock> like line of dispinstock.
loop at dispinstock assigning <dispinstock>.
  " some work
  <dispinstock>-gesme = new value..
  "...
 endloop

This way you reduce the number of times you read the dispinstock table and change the value directly.

一百个冬季 2024-12-03 10:57:48

我确信您的内部表 dispinstock 不包含 170.000 种不同的材料!
开始选择

因此,我建议建立一个不同 MATNR 的表,并以 FOR ALL ENTRIES IN ... ... 。
AND ( lgtyp IN zsd_t301_n OR ( lgtyp >= '900' AND lgtyp <= '903' ) )

在范围对象 zsd_t301_n 中插入一行并删除 OR 语句

Sign OPTION LOW HIGH

I BT 900 903

I´m sure that your internal table dispinstock does not contain 170.000 distinct materials!
So I suggest to build a table of distinct MATNRs and start the select with a FOR ALL ENTRIES IN ...

...
AND ( lgtyp IN zsd_t301_n OR ( lgtyp >= '900' AND lgtyp <= '903' ) )

Insert one row into your range object zsd_t301_n and delete the OR statement

Sign OPTION LOW HIGH

I BT 900 903

美羊羊 2024-12-03 10:57:48

如果“MODIFY dispinstock”行的意思是“用刚刚从 SELECT 获取的值更新 dispinstock 表中的一行”,那么您可能可以用单个 MERGE 语句替换 LOOP 和 SELECT。

像这样的东西

MERGE INTO dispinstock
USING (  SELECT matnr, SUM( gesme ) gesme,  SUM( verme ) verme, SUM( einme ) einme,  SUM( ausme ) ausme
         FROM lqua
         WHERE lgnum = 'xxxxx'
         AND ( lgtyp IN zsd_t301_n
          OR ( lgtyp >= '900' AND lgtyp <= '903' ) )
          GROUP BY matnr
      ) lqua
ON lqua.matnr = dispinstock.matnr
WHEN MATCHED THEN UPDATE SET
  gesme = l.gesme, verme = l.verme, einme = l.einme, ausme = l.ausme

If the line that says "MODIFY dispinstock" means "update a row in the dispinstock table with the values that were just acquired from the SELECT", then you could probably replace the LOOP and the SELECT with a single MERGE statement.

Something like

MERGE INTO dispinstock
USING (  SELECT matnr, SUM( gesme ) gesme,  SUM( verme ) verme, SUM( einme ) einme,  SUM( ausme ) ausme
         FROM lqua
         WHERE lgnum = 'xxxxx'
         AND ( lgtyp IN zsd_t301_n
          OR ( lgtyp >= '900' AND lgtyp <= '903' ) )
          GROUP BY matnr
      ) lqua
ON lqua.matnr = dispinstock.matnr
WHEN MATCHED THEN UPDATE SET
  gesme = l.gesme, verme = l.verme, einme = l.einme, ausme = l.ausme
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文