14.5Performance Recommendations for Native Implementations in SAP HANA

Now that we’ve discussed performance recommendations for working with Open SQL, we’ll provide some recommendations for working with Native SQL, modeled and implemented SAP HANA views, and SQLScript.

14.5.1Recommendations for Native SQL

In connection with the use of Native SQL via ADBC, we’ll refer—in addition to the recommendations for Open SQL, which apply in the same way as Native SQL—to two topics separately. This involves the use of prepared statements and mass operations. We’ve presented both in Chapter 3, Section 3.2.4, so we only want to discuss the performance aspects here. For the topics presented here (and for others), there is an example in the INSERT_ROWS and INSERT_ITAB subroutines in ABAP test Program ADBC_DEMO, which is provided with the standard SAP.

Prepared Statements

Unlike Open SQL, which is optimized for performance by the SAP Kernel, the programmer must ensure optimal use when using Native SQL via ADBC. If the CL_SQL_STATEMENT class is used, this involves a dynamic statement that is transferred to the database for each execution with the EXECUTE_QUERY method, which analyzes the SQL statement in turn as a character string. The parameters are included in the analysis. The following two SQL statements are thus different for the database because two different character strings are involved:

SELECT * FROM scarr WHERE carrid = 'AA';
SELECT * FROM scarr WHERE carrid = 'UA';

For each of these two statements, the database must perform, among other things, the following steps:

These steps are known as the prepare phase because the statement for execution is prepared here. If a very large number of SQL statements is sent to the database, which differ only in the parameters that are used, the database has to make a relatively large effort in preparing each statement. The time required can lie in the mid-three-digit microsecond range and thus may be as high as the time required for actually executing the statement. Frequent executions therefore quickly involve additional effort, which can constitute a significant part of the runtime.

If only the parameters of an SQL statement change, the statement can be transferred to the database using the CL_PREPARED_STATEMENT class with a parameter marker. The transferred statement looks, for example, as follows:

SELECT * FROM scarr WHERE carrid = ?;

This statement is prepared once only and is stored in the SQL cache. Immediately before execution, the parameters that were set with the SET_PARAM method are used instead of the parameter marker when you call the EXECUTE_QUERY method of the CL_PREPARED_STATEMENT class. That means you can reduce the effort in preparing the SQL statements to the bare minimum. After you no longer require the prepared SQL statement, you should use the CLOSE method to close the CL_PREPARED_STATEMENT class, so you can release the resources required by the SQL statement as soon as possible.

You should use the CL_SQL_STATEMENT class to execute statements that are executed only once. For SQL statements that you want to execute several times, you should use the CL_PREPARED_STATEMENT class and pass the different parameters separately. That helps keep the effort in preparing SQL statements as low as possible and contributes to relieving the database.

Mass Operations

As of SAP NetWeaver AS ABAP 7.4, an array interface is available for modifying SQL statements via ADBC. You can add, for example, multiple rows at once, and you don’t have to proceed row by row. Because a reduced number of statements has a positive effect on the performance of an application, we recommend that you use this option not only for read accesses but also for write accesses. As discussed in Section 14.1.6, however, you should modify data via the ADBC interface only in exceptional situations.

14.5.2Recommendations for SAP HANA Views

Chapter 4 introduced you to various view types. When modeling and implementing SAP HANA views, you can make certain errors that have a particularly adverse effect on performance. We’ll provide a few basic recommendations for modeling SAP HANA views.

Selecting the Correct View Type

First, it’s incredibly important to select the correct view type when modeling in SAP HANA Studio. Your options are shown in Figure 14.3, which is derived from the SAP HANA SQLScript Reference that supports you in decision making.

Selecting the View Type in SAP HANA

Figure 14.3Selecting the View Type in SAP HANA

When selecting the view type, you should first check whether you need a star schema to map a given requirement and/or want to aggregate a large number of data records. If this is the case, we recommend that you use an analytic view. Otherwise, you can first use an attribute view. An attribute view allows you to relate multiple tables to each other using joins. If necessary, you can also define calculated fields.

If you can’t map a given requirement through an analytic view nor an attribute view, use a calculation view. You can use a modeled calculation view if you want to use only the operations JOIN, PROJECTION, AGGREGATION, and UNION. Otherwise, you must implement the calculation view and either use only calculation engine (CE) functions or rely on the additional options of SQLScript.

Modeling/Implementation

In addition to selecting the correct view type, you should consider some other recommendations for modeling and implementing SAP HANA views to achieve optimum performance.

You very often need several SAP HANA views (see Figure 14.4) to solve a given requirement, as in the following situation:

In such a case, several engines (see Chapter 1, Section 1.3) are involved in calculating the final result. This is illustrated schematically in Figure 14.4.

You can support the engine involved in solving a given task by following some rules:

14.5.3Recommendations for SQLScript

If you have to use SQLScript to implement a requirement (because the requirement can’t be mapped by modeling an SAP HANA view), note the following rules (already discussed to some extent in Chapter 4, Section 4.2):

For more information, refer to SAP HANA SQLScript Reference in the SAP online help.