默认情况下如何对内部表进行排序?

发布于 2025-02-01 17:59:59 字数 199 浏览 2 评论 0 原文

因此,我想知道当我声明时是否

lt_table TYPE STANDARD TABLE OF mara.

lt_table TYPE STANDARD TABLE OF mara WITH DEFAULT KEY.

与不声明默认键相同的标准表键是否相同

So i was wondering if when i declare

lt_table TYPE STANDARD TABLE OF mara.

Is it the same as

lt_table TYPE STANDARD TABLE OF mara WITH DEFAULT KEY.

Or are the standard table keys selected differently when not declaring DEFAULT KEY?

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

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

发布评论

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

评论(1

巷雨优美回忆 2025-02-08 17:59:59

这是一样的,如

如果未针对标准表定义明确的主键,则它会自动具有标准键。

a 默认键(下面的第一个项目符号)或什么都没有(第二个):

标准键可以声明如下:

  • 明确使用添加的语句类型,数据等唯一|非唯一键,其中指定了添加默认键,而不是组件列表。
  • 隐含地,如果在使用语句数据的标准表声明中没有明确的主要密钥规范。
  • 隐含地,如果在语句数据中指定了带有通用主表键的标准表类型。
  • 中的类型。


编辑2022年5月31日:关于“标准表键”的含义可能会有一些混乱。这可能会使人们认为桌子已经排序,然后访问更快。

那是错误的。

仅当您按Comp1 comp2 对ITAB进行明确对ITAB进行明确排序(在耗时之时),然后使用使用键Comp1 = ... comp2 =。 ..二进制搜索

声明标准表的主键(默认键或明确组件)是一种不提及 sort> sort 读取表等之后的组件的方法,而是ABAP文档建议在 sort 读取表等之后明确声明它们。

因此,我对声明标准表的主要键没有任何兴趣。

nb:收集仅基于标准表的主要键,因此别无选择,除非您将收集用这样的代码替换为:

ASSIGN itab[ c1 = line-c1 c2 = line-c2 ] TO FIELD-SYMBOL(<exist_line>).
IF sy-subrc = 0.
  <exist_line>-counter = <exist_line>-counter + line-counter.
ELSE.
  INSERT line INTO TABLE itab.
ENDIF.

如果要:如果要使用排序的表进行更快的访问,更喜欢使用键入表格表键入hashed表(或任何替代语法都具有辅助键),它将真正对表和访问更快,编译器将使用 sort (错误,因为已经排序),读取表等来发送更好的警告或错误消息,而不是标准表(仅在使用ATC时才警告)。

有关更多信息,请参见

That's the same, as explained in the ABAP documentation:

If no explicit primary key is defined for a standard table, it automatically has a standard key.

A standard key is when you indicate DEFAULT KEY (first bullet point below) or nothing (second one):

The standard key can be declared as follows:

  • Explicitly, using the additions UNIQUE|NON-UNIQUE KEY of the statements TYPES, DATA and so on, where the addition DEFAULT KEY is specified instead of the list of components.
  • Implicitly, if no explicit primary key specification is made in the declaration of a standard table with the statement DATA.
  • Implicitly, if a standard table type with a generic primary table key is specified behind TYPE in the statement DATA.

EDIT May 31st, 2022: there can be some confusion about the meaning of the "keys of a standard table". That could make people think that the table is sorted and the access is then faster.

That's wrong.

It will be faster only if you sort explicitly your internal table SORT itab BY comp1 comp2 (once as it's time consuming), and use READ TABLE itab WITH KEY comp1 = ... comp2 = ... BINARY SEARCH.

Declaring the primary key (default key or explicit components) of a standard table is a way to not mention the components after SORT, READ TABLE, etc., but the ABAP documentation recommends to explicitly declare them after SORT, READ TABLE, etc.

Consequently, I don't see any interest in declaring the primary key of a standard table.

NB: COLLECT works only based on the primary key of the standard table, so here there's no choice except if you replace COLLECT with code like this for instance:

ASSIGN itab[ c1 = line-c1 c2 = line-c2 ] TO FIELD-SYMBOL(<exist_line>).
IF sy-subrc = 0.
  <exist_line>-counter = <exist_line>-counter + line-counter.
ELSE.
  INSERT line INTO TABLE itab.
ENDIF.

If you want to use a sorted table for faster access, prefer declaring the table with TYPE SORTED TABLE or TYPE HASHED TABLE (or any alternate syntax to have Secondary Keys), it will really sort the table and accesses are faster, the compiler will send better warning or error messages with SORT (error because already sorted), READ TABLE, etc., than for a standard table (only some warnings if you use ATC).

For more information, see ABAP documentation - itab - Selection of the Table Category

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文