Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

A couple of questions about sorted tables and field-symbols...

aris_hidalgo
Contributor
0 Kudos

Hello experts,

I am currently trying to optimize a report since it is really slow. Here are my questions:

1. in the original program, there is an itab named it_alv and after appending records to it, the original programmer made this:

sort it_alv by saknr lifnr budat.

Now, the itab it_alv is a standard table so I tried to use sorted table to skip the sort statement he used. This is what I did:

types: BEGIN OF s_alv,

saknr LIKE bsik-saknr,

lifnr LIKE bsak-lifnr,

name1 LIKE lfa1-name1,

sgtxt LIKE bsik-sgtxt,

budat LIKE bsik-budat,

belnr LIKE bsik-belnr,

dmbtr LIKE bsik-dmbtr,

rtntn LIKE bsik-dmbtr,

balnc LIKE bsik-dmbtr,

curr LIKE bsik-dmbtr,

mon02 LIKE bsik-dmbtr,

mon36 LIKE bsik-dmbtr,

mon79 LIKE bsik-dmbtr,

mo112 LIKE bsik-dmbtr,

mon12 LIKE bsik-dmbtr,

remrk(40),

ppnam LIKE bkpf-ppnam,

usnam LIKE bkpf-usnam,

END OF s_alv.

data: it_alv type sorted table of s_alv with non-unique key saknr lifnr budat with header line.

So, is my statement above correct? is it the same as sorting the itab by saknr lifnr budat?And also, I debugged the program and it seems that I cannot use the command append it_alv.

2. This is my 2nd question:

In the original report, I noticed there wasn't any field-symbols so I tried to play around with it. I used field-symbols as header of the itabs declared in the report compared in using a header line. I assigned the field-symbols during loops. So, is this faster?

3. this is my 3rd question:

I am doing a validation and I am confused on what is the fastest.Here are my choices(please add as necessary)

check not itab[] is initial

if not itab[] is initial

describe table itab lines n

4. this is my 4th question:

I tried to play around with indexes(for BSIK and BSAK) and I went to SE11, clicked the indexes button, created my own index and declared certain fields. Now, how can I use my index?

Thanks guys and take care!

4 REPLIES 4

Former Member
0 Kudos

Hi Viray,

This is an excerpt from SAP Help:

Appending lines to standard tables and sorted tables with a non-unique key works regardless of whether lines with the same key already exist in the table. Duplicate entries may occur. A runtime error occurs if you attempt to add a duplicate entry to a sorted table with a unique key. <b>Equally, a runtime error occurs if you violate the sort order of a sorted table by appending to it.</b>

See http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb36c8358411d1829f0000e829fbfe/content.htm

Indexes are used to optimise the performance of database operations. You can create them to optimize that. Not to play around with it in your program.

Usage of field symbols improves the performance.

CHECK itab IS NOT INITIAL.

--- Stops processing furtther if condition is not satisfied.

Regards,

Wenceslaus.

Former Member
0 Kudos

A lot of questions for one thread....

1. Append cannot be used with sorted tables, you should use INSERT (records will be inserted into the internal table at the correct position to keep the sorting ok).

2. Especially with large internal tables the ASSIGNING FIELD-SYMBOLS technique is much faster as data is not copied unnecessarily.

3. Check ... is initial or if.... is inital is the same for performance reasons.

4. The index will be used automatically by the DB-optimizer.

Regards,

John.

Former Member
0 Kudos

Hi

I think you should understand which side of program is slow:

- Uploading the data to be printed

- Printing the data by ALV.

If it's the second step you can do anything, because std tools (ALV) manage it.

1- I believe you can't use a SORTED TABLE as internal table to transfer to ALV fm.

Here if the user'll try to sort the list using different fields a dump could occur.

If there isn't APPEND statamnet, perhaps you can find INTO TABLE or INSERT.

2- I think if you know the structure of the table it should be better to use a workarea structurated as it instead of the field-symbol.

3- I don't know, I don't believe it can be big differences, anyway you can also try to use:

READ TABLE ITAB INDEX 1.

4- You'll use your index if the fields of WHERE condition are placed with the same order of the INDEX:

For example is the fields of an index are:

BUKRS, LIFNR, BUDAT

You'll use this index in select like:

SELECT * FROM BSIK/BSAK WHERE BUKRS

AND LIFNR

AND BUDAT

You won't use that index in select like

SELECT * FROM BSIK/BSAK WHERE BUKRS

AND BUDAT

AND LIFNR

Max

Former Member
0 Kudos

Hi Viraylab,

1.1 When you use an internal table of sorted type, you can only insert the data . You can not use the append statement.

1.2 When use " with non-unique key " in your syntax, the system follows the sequence of the key fields. So, the way you have defined the table is same as sorting the itab by saknr lifnr budat.

<b> To say, the syntax is correct. But only thing is you can not use the append statement for such a table.</b>

2. <b>Access Using Field Symbols :</b>

When you read table entries using READ or in a LOOP, you can assign them to a field symbol using the addition

... ASSIGNING <FS>

The field symbol <FS> points directly to the assigned line in memory. Unlike work areas, in which the contents of the line are only available indirectly, field symbols allow you to read and change table entries directly.

Remember when you access internal tables using field symbols that you must not change the contents of the key fields of sorted or hashed tables. If you try to assign a new value to a key field using a field symbol, a runtime error occurs. Note that you cannot use the SUM statement with field symbols, since the statement is always applied to work areas .

<b>Advantage of Field Symbol :</b>

When you read from an internal table, there are no overheads for copying the table line to the work area. When you change an internal table with the MODIFY statement, you must first fill a work area with values, and then assign them to the internal table. If you work with field symbols instead, you do not have this overhead. This can improve performance if you have large or complex internal tables. It also makes it easier to process nested internal tables.

3. you can use this :

<b>if not itab[] is initial</b>

4. See the indexes you created is a Secondory index.

The Primary indexes are automatically created whihc consist of the key values.

Now when you use the fields , say BSIK and BSAK in your where statement, the secondary indexes which you created will make the search faster. you dont have to do anythihng additional. just you have to use them in your where statement.

Hope your queries are solved.

Thanks and Regards,

Kunal.

note : Mark the helpful answer.