By profiling your ABAP code you discovered that access to the database is a major contributor to the overall runtime. You now use the SQL Trace tool to analyze the database access on your program in more details and to further improve the performance of your code.
Task 1: Copy Template (Optional)
Copy the template class. If you finished the previous exercise, you can skip this task and continue editing your class ZCL_##_SOLUTION.
Steps
Copy class /LRN/CL_S4D401_ATS_PROFILING to a class in your own package (suggested name: ZCL_##_SOLUTION, where ## stands for your group number).
In the Project Explorer view, right-click class /LRN/CL_S4D401_ATS_PROFILING to open the context menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_SOLUTION, where ## stands for your group number.
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
Activate the copy.
Press Ctrl + F3 to activate the class.
Task 2: Perform an SQL Trace
To further analyze the performance issues, you activate the SQL Trace and execute your ABAP class ZCL_##_SOLUTION as a console app.
Within the same ABAP instance, only one user can switch on the SQL Trace at a time. To also give other users a chance, make sure you switch of the SQL Trace as soon as you finish executing the console app.
Steps
If it is not open, open your ABAP class ZCL_##_SOLUTION in the editor.
Press Ctrl + Shift + A and enter the name of your ABAP class.
Choose OK.
Open the SQL Trace State window for your ABAP cloud project.
In the Project Explorer view on the left, right-click your cloud project to open the context menu.
Choose SQL Trace ....
Check the current SQL Trace State. If the trace is switched on, close the dialog window and come back in a few minutes.
Choose OK to close the dialog window.
If the trace is currently switched off, switch it on. Then close the dialog window.
Choose Activate to switch on the SQL trace.
Choose OK to close the dialog window.
Execute your global class ZCL_##_SOLUTION as console app.
Press F9 to execute the ABAP class as console app.
As soon as the application has finished, switch off the SQL trace and open the trace directory.
As soon as the green progress indicator in the bottom right corner disappears, right-click your cloud project in the Project Explorer and choose SQL Trace ....
Choose Deactivate to swithc off the trace.
Choose View Trace Directory to display a list of your SQL traces.
Task 3: Analyze the Trace Result
Analyze the SQL Trace result. Filter for database accesses that originate from your ABAP class and find out which database tables have been accessed for how often.
Steps
On the Technical Monitor Cockpit, display the trace records of the newest trace.
Hint
By default, the list of traces is sorted descending by column Time Stamp.Choose the topmost list entry.
Choose Trace Records.
Analyze the list of trace records.
You find the displayed number of rows above the header of column Time Stamp.
You find the maximum number of rows that are displayed next to the Go button.
Filter the list of trace records. Only display records that originate from your ABAP class ZCL_##_SOLUTION.
Hint
We recommend that you use the selection filter left from the Go button and not the display filter that you can set by choosing the header of column Program.
Choose Filter (...) from the toolbar on the left of the Go button.
Choose Program from the drop down list at the top of the dialog window.
Make sure that the Like option is selected.
Replace the input prompt Enter value with the name of your ABAP class (Z00_SOLUTION).
Choose OK.
Choose Go to refresh the selection.
Sort to the list of trace records. Find the SQL statements that accessed the most records.
Note
By default, the list of trace records is sorted by the Time Stamp column. If you want to sort it using the Duration column, you have to remove the existing sorting first.
Choose the three horizontal dots on the right of the Go button.
Choose Reset Sort.
Choose Sort(0) on the left of the Go button.
Find the Accessed Records [#] column. Tick the box on the left and choose the button with the downward arrow.
Choose OK to close the dialog window.
Choose Go.
Remove the sorting by the Accessed Records column and sort the list descending by the Duration column.
Choose the three horizontal dots on the right of the Go button.
Choose Reset Sort.
Choose Sort(0) on the left of the Go button.
Find the Duration [µs] column. Tick the box on the left and choose the button with the downward arrow.
Choose OK to close the dialog window.
Choose Go.
For the most expensive single record statement, navigate to the SQL statement display.
Place the cursor on the row with value 1 in the Accessed Records column and the largest value in the Duration column.
Choose SQL Statement from the navigation pane on the top.
You find the table name in the FROM clause.
Open the SELECT statement in the ABAP development tools.
Choose Show ABAP Source in ADT.
Task 4: Improve the Code
To further improve the performance of your code, eliminate the repeated SELECT SINGLE access to database table /LRN/CONNECTION. You know that table /LRN/CONNECTION contains a relatively small number of records. Therefore, you decide to buffer the entire content of the database table in a static attribute (suggested name: connections_buffer). You fill the buffer in the static constructor (method class_constructor) and replace the SELECT SINGLE statement in the instance constructor with a read access to the buffer.
Hint
The SELECT SINGLE access to database table /LRN/CONNECTION is located in the constructor method of local class lcl_passenger_flight. Steps
Navigate to the definition of attribute connection_details and from there to the definition of structure type st_connection_details.
In the FROM clause of the SINGLE SELECT statement, place the cursor on connection_details and press F3. Alternatively, you can hold down the Ctrl key and choose connection_details.
In the DATA statement, place the cursor on st_connection_details and press F3. Alternatively, you can hold down the Ctrl key and choose st_connection_details.
Copy public structure type st_connection_details to a new private structure type (suggested name: st_connections_buffer).
Select the entire TYPES: statement (including row END OF st_conection_details.) and press Ctrl + C.
Scroll down to the private section, place the cursor above the ENDCLASS statement and press Ctrl + V to insert the copied code.
In the inserted code, replace st_connection_details with st_connections_buffer.
At the beginning of the component list of st_connections_buffer, add the key fields of database table /LRN/CONNECTION.
Adjust the code as follows:
1234567891011
TYPES:
BEGIN OF st_connections_buffer,
carrier_id TYPE /dmo/carrier_id,
connection_id TYPE /dmo/connection_id,
airport_from_id TYPE /dmo/airport_from_id,
airport_to_id TYPE /dmo/airport_to_id,
departure_time TYPE /dmo/flight_departure_time,
arrival_time TYPE /dmo/flight_departure_time,
END OF st_connections_buffer.
Declare a private static attribute connections_buffer as an internal table with st_connections_buffer as row type.
At the end of the class definition, add the following code:
123
CLASS-DATA connections_buffer TYPE TABLE OF st_connections_buffer.
Use a quick fix to generate a static constructor (class constructor) for class lcl_passenger_flight.
Scroll up to statement CLASS lcl_passenger_flight DEFINITION..
Place the cursor on lcl_passenger_flight and press Ctrl + 1 to display a list of available quick fixes.
From the list, choose Generate class constructor.
Edit the implementation of method class_constructor. Implement a SELECT statement that reads all records from database table /LRN/CONNECTION into the connections_buffer attribute.
Adjust the code as follows:
1234567891011
METHOD class_constructor.
SELECT
FROM /lrn/connection
FIELDS carrier_id, connection_id,
airport_from_id, airport_to_id, departure_time, arrival_time
INTO TABLE @connections_buffer.
ENDMETHOD.
In the implementation of method constructor, replace the SELECT SINGLE statement with a read access to the connections_buffer.
Note
Keep in mind that attribute connection_details has fewer components than the row type of the connections_buffer attribute.Select the entire SELECT SINGLE statement and choose Ctrl + < to add a comment sign in front of each row.
After the commented SELECT SINGLE statement, add the following code:
1234567891011121314
* SELECT SINGLE
* FROM /lrn/connection
* FIELDS airport_from_id, airport_to_id, departure_time, arrival_time
* WHERE carrier_id = @carrier_id
* AND connection_id = @connection_id
* INTO @connection_details .
connection_details = CORRESPONDING #( connections_buffer[
carrier_id = i_carrier_id
connection_id = i_connection_id ]
).
Task 5: Re-check the Code
Activate and test your global class. Execute the class as console app. Repeat the profiling to measure the performance improvement.
Note
If you want, you can re-run the unit test and the ABAP Test Cockpit analysis to make sure you did not introduce any new issues.Steps
Activate your global class and execute it as console app.
Press Ctrl + F3 to activate the class.
Press F9 to execute the class as console app.
Start the runtime measurement with the ABAP Profiler.
Right-click in the editor and choose Profile As → ABAP Application (Console).
In the Trace Parameters dialog box, choose Finish without changing any of the settings.
Switch to the Profiler perspective and analyze the trace overview.
Choose Window → Perspective → Open Perspective → ABAP Profiling.
In the ABAP traces view, choose Refresh. Alternatively, you can place the cursor anywhere in the ABAP Traces view and press F5 to re-load the list of trace results.
Double-click the most recent trace (this is the topmost entry under your project).
The overall runtime and the distribution between ABAP and Database are displayed in the lower section of the Overview.