Skip to Content

Use of (primary) key in internal tables

Hi,

this question is for the real experts (like Horst Keller) so please don't answer if not really know...

I delare an internal table with various fields as SORTED TABLE WITH UNIQUE KEY table_line - I won't have any duplicates as I fill from a database (customizing) table.

In a LOOP AT this table WHERE <field> = <value>, will the loop use the key if it's the first key?

What are the rules of index use in LOOP?

I mean NOT secondary index as they can be defined now.

Thanks a lot and best regards,

Clemens

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    Posted on Nov 26, 2014 at 10:42 PM
    16

    Hi Clemens,

    I really can't believe that a respected topaz level member like yourself would discriminate other users by asking them not to reply to your question. Surely you are being VERY disrespectful to other members.

    If you have a direct question to Dr Horst Keller (or someone "like" him), I'm sure you can figure out his email address. (tip: it's first_name dot last_name at company dot com)

    Regards,

    Custodio

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Matt,

      I wanted to let this part of the discussion die after Clemens recognized that he didn't do well (kudos to him), but I think you deserve a quick reply, at least in one point:

      Matthew Billingham wrote:

      I disagree. I spend a lot of time rejecting posts that show that certain responders (who are just hunting for points rather than wanting to help) have not understood the first thing about the question. Am I discriminating against them?

      No, you are not. You are simply doing your job as moderator (very well, btw). It's completely different from asking only "real experts" (whatever the heck it means) to answer your question. Not even close.

      I could go much further, but as I said, I want to let it die, so I hope Clemens get his answer, be it from a "real expert", a fake expert, a newbie, from doco, whatever.

      Cheers,

      Custodio

  • Posted on Nov 28, 2014 at 12:19 PM

    Clemens... that's is the doc, look at Optimization of the WHERE Condition (so here answer is YES)

    Regards,

    Raymond

    Add a comment
    10|10000 characters needed characters exceeded

    • Clemens,

      Yes, it is more fun 😊, but with that approach I think you may be filtering out some fine signal!

      An interesting question can provoke some innovative, huh-never-thought-of-that answers from people who know their stuff - or are willing to investigate on your behalf - but aren't German PhDs...

      "Actually it would cost me some time to set up a measure routine to check."

      Many hungry newcomers would be happy to do this for you, but now you've put them off. 😉

      cheers

      Paul

  • Posted on Dec 01, 2014 at 06:31 AM

    Hi,

    No, I believe (why should the table_line, whatever the resulting key, cause something to differ from the normal rules..?), based on this:

    There is no implicit selection of a suitable key or index. The used table key or table index is always specified uniquely. The syntax check issues a warning if there is a suitable secondary table key but this table key is not used. This warning should be removed through using the key. However, in exceptional cases, it can be bypassed using a pragma.

    The values for using primary key would need to be provided in full, if I remember correctly.

    No,

    LOOP AT this_sorted_table USING KEY primary_key

    WHERE

    ...

    compiles with partial key, as long as the "beginning of key" is somewhere in WHERE condition (and rest of WHERE optimization conditions are fulfilled). My understanding of the red statement above still is - without the addition USING KEY, no key will be used by LOOP...

    cheers

    Jānis

    Message was edited by: Jānis B

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 05, 2014 at 07:42 AM

    The code:

    REPORT zjbtst20.

    TYPES: BEGIN OF ty_item,

    mandt TYPE symandt,

    group(3) TYPE c,

    counter(8) TYPE n,

    a TYPE sysuuid_c32,

    b TYPE sysuuid_c32,

    c TYPE sysuuid_c32,

    d TYPE sysuuid_c32,

    e TYPE sysuuid_c32,

    END OF ty_item,

    tyt_sorted_items TYPE SORTED TABLE OF ty_item

    WITH UNIQUE KEY table_line .


    DATA: gt_sorted_table TYPE tyt_sorted_items .

    DATA: gs_sorted_table_item TYPE ty_item .


    DATA: gx_ex TYPE REF TO cx_uuid_error .


    DATA: go_timer TYPE REF TO if_abap_runtime .

    DATA: g_runtime TYPE int4 .


    DATA: g_groupk(3) TYPE c .

    DATA: g_counterk(8) TYPE n .


    LOAD-OF-PROGRAM .

    gs_sorted_table_item-mandt = sy-mandt .

    DO 3 TIMES .

    WRITE sy-index TO gs_sorted_table_item-group RIGHT-JUSTIFIED .

    DO 10000 TIMES .

    TRY .

    gs_sorted_table_item-counter = sy-index .

    gs_sorted_table_item-a = cl_system_uuid=>create_uuid_c32_static( ).

    gs_sorted_table_item-b = cl_system_uuid=>create_uuid_c32_static( ).

    gs_sorted_table_item-c = cl_system_uuid=>create_uuid_c32_static( ).

    gs_sorted_table_item-d = cl_system_uuid=>create_uuid_c32_static( ).

    gs_sorted_table_item-e = cl_system_uuid=>create_uuid_c32_static( ).

    CATCH cx_uuid_error INTO gx_ex.

    MESSAGE gx_ex TYPE 'E' .

    ENDTRY .

    INSERT gs_sorted_table_item INTO TABLE gt_sorted_table .

    ENDDO .

    ENDDO .


    START-OF-SELECTION .

    g_groupk = 2 .


    go_timer = cl_abap_runtime=>create_hr_timer( ) .

    go_timer->get_runtime( ). "init

    DO 10000 TIMES .

    g_counterk = sy-index.

    LOOP AT gt_sorted_table INTO gs_sorted_table_item

    WHERE

    mandt = sy-mandt AND

    group = g_groupk AND

    counter = g_counterk .

    EXIT .

    ENDLOOP .

    ENDDO .

    g_runtime = go_timer->get_runtime( ) .

    WRITE: / g_runtime.


    go_timer = cl_abap_runtime=>create_hr_timer( ) .

    go_timer->get_runtime( ). "init

    DO 10000 TIMES .

    g_counterk = sy-index.

    LOOP AT gt_sorted_table INTO gs_sorted_table_item

    USING KEY primary_key

    WHERE

    mandt = sy-mandt AND

    group = g_groupk AND

    counter = g_counterk .

    EXIT .

    ENDLOOP .

    ENDDO .

    g_runtime = go_timer->get_runtime( ) .

    WRITE: / g_runtime.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.