Create a new ABAP class called ZCL_FILL_ZS4D401_FLIGHTS.
Enter the following code in the implementation of the main method:
Code snippet
DATA flights TYPE TABLE OF /dmo/flight.
DATA insert_tab TYPE TABLE OF /dmo/flight.
DELETE FROM ZS4D401_flights.
SELECT FROM /dmo/flight FIELDS * ORDER BY carrier_Id, connection_id INTO TABLE @flights .
LOOP AT flights INTO DATA(first_date).
* Original table flights has 2 flights per connection. Only process the first
IF sy-tabix MOD 2 = 0. CONTINUE. ENDIF.
*Extend flight dates by 2000 days
DO 2000 TIMES.
APPEND first_date TO insert_tab.
first_date-flight_date += 1.
ENDDO.
* ENDIF.
ENDLOOP.
* Read highest connection number for each flight
SELECT FROM /dmo/flight AS main FIELDS carrier_Id, connection_id, flight_date, price, currency_code, plane_type_id
WHERE connection_id = ( SELECT MAX( connection_id ) FROM /dmo/flight WHERE carrier_id = main~carrier_id )
AND flight_Date = ( SELECT MIN( flight_date ) FROM /dmo/flight WHERE carrier_id = main~carrier_id AND connection_id = main~connection_id )
GROUP BY carrier_id, connection_Id, flight_date, price, currency_code, plane_type_id
ORDER BY carrier_id, connection_id
INTO TABLE @DATA(max).
*Add 50 new connection numbers and 2000 days of flights for each
LOOP AT max INTO DATA(line).
DO 50 TIMES.
line-connection_id += 1.
line-plane_type_id = SWITCH #( CONV i( line-connection_id ) MOD 2 WHEN 0 THEN 'A330' WHEN 1 THEN 'A350' ).
first_date = CORRESPONDING #( line ).
DATA(repetitions) = COND i( WHEN line-carrier_id = 'LH' AND line-connection_id = '0405' THEN 4000 ELSE 2000 ).
DO repetitions TIMES.
first_date-seats_max = 220.
APPEND first_date TO insert_tab.
first_date-flight_date += 1.
ENDDO.
ENDDO.
ENDLOOP.
SORT insert_tab BY carrier_Id connection_id flight_date.
DELETE ADJACENT DUPLICATES FROM insert_tab COMPARING carrier_id connection_id flight_date.
INSERT ZS4D401_flights FROM TABLE @insert_tab.
out->write( |Generated { sy-dbcnt } rows in table ZS4D401_flights| ).
Select F9 to run the class. The main method fills an extra database table that is required for the code snippets in the next two sections.
Sorted and Hashed Tables
Up until now, we have worked with standard internal tables. The data in a standard table is not held in a particular sort order. Consequently, when you read data from the table, the system must search the table sequentially until it finds the row or rows that you need. Depending on the size of the table, this can take some time.
You can optimize the performance of read accesses to internal tables using sorted or hashed tables.
In a sorted table, the contents of the table are always sorted according to the key fields in ascending order. When you insert a new record into the table, the system ensures that it is placed at the correct position. Since the data is always sorted, the system can retrieve records more efficiently than from a standard table (as long as you follow particular rules).
Hashed tables are managed using a special hash algorithm. This ensures that the system can retrieve records very quickly even if the table is extremely large. However, this performance gain only works in very particular cases.
Access to a Sorted Table
In this video, you will see how to access a sorted table.
When you declare a hashed table, you must define it with a unique key – duplicates are never allowed.
Watch the video to know how to access a hashed table.
When to use Standard, Sorted, and Hashed Tables
Select each option to learn more.
Comparative Runtimes of Standard, Sorted, and Hashed Tables
This figure compares the runtimes of standard, sorted, and hashed tables.
Filling Sorted and Hashed Tables
When you use a standard table, you can use the APPEND statement to add the contents of a work area to the end of the table. This is not possible with hashed tables, and leads to a syntax error.
Technically, you can use the APPEND statement to fill a sorted table. However, you risk causing a runtime error if the line that you try to append does not belong at the end of the table according to the sort area. This would happen in our example.
The way to fill a hashed table and to fill a sorted table safely is to use the INSERT statement. INSERT ensures that the new row is inserted at the correct position in the table.
Try It Out: Sorted And Hashed Tables
Create a new class that implements the interface IF_OO_ADT_CLASSRUN.
Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
Code snippet
* Run this class using the ABAP Profiler to measure relative access times for standard, sorted, and hashed tables
data(flights) = new lcl_flights( ).
flights->access_standard( ).
flights->access_sorted( ).
flights->access_hashed( ).
out->write( |Done| ).