ABAP for SAP HANA and Code Pushdown

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Every ABAP program that reads business data has to decide where the work happens. It can fetch every row from the database and add things up in the program, or it can ask the database to add them up and send back only the answer. This page explains why the second way is usually much faster on SAP HANA, which is the database that SAP S/4HANA and the ABAP environment run on. It names the idea, code pushdown, and gives you the rule of thumb for how far to push: first ABAP SQL, then a CDS view, and only then a database procedure written in SQLScript. It also explains two older tools that you will meet in existing systems, database procedure proxies and external views, and walks through the rewrite of a small report. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). Code pushdown applies in ABAP Cloud and in classic Standard ABAP on premise alike; the legacy tools in the second half belong to classic Standard ABAP.

Before you read

Key terms

  • Code pushdown: moving calculation from the ABAP program to the database, also called code-to-data.

  • Application server: the computer that runs your ABAP program; it is separate from the database computer.

  • SAP HANA database: the in-memory database that is the standard database of an ABAP system.

  • ABAP SQL: the SQL dialect of ABAP, written inside ABAP programs and checked by the ABAP compiler.

  • Aggregate expression: a function such as SUM or COUNT that turns many rows into one value.

  • CDS view entity: a database view defined in ABAP CDS, which ABAP SQL can read like a table.

  • AMDP: ABAP Managed Database Procedures, database code written in SQLScript but kept in an ABAP class.

The problem: data moves, then work happens

An ABAP system has two main parts that run on different computers. The application server runs your ABAP program. The database stores the tables. Every row that your program reads has to travel from the database to the application server, and moving a million rows costs far more time than adding them up. On the SAP HANA database the work itself is fast: the database keeps the data in main memory, and a column-oriented layout lets it add up one column without reading the others. So the efficient program asks the database for the answer, not for the rows. The ABAP Keyword Documentation states the same effect for grouping: using GROUP BY and aggregate functions makes the database system build aggregates and groups, not the application server, which can considerably reduce the volume of data transported from the database to the application server. The older way of thinking, "read it all, then process it in ABAP", is sometimes called data-to-code; the newer one is code-to-data or code pushdown.

flowchart LR subgraph Before[Before: loop and add up in ABAP] direction LR DB1[(Database: 1,000,000 claim rows)] -->|every row travels| AS1[Application server: LOOP and add up] AS1 --> R1[Result: 2 totals] end subgraph After[After: ABAP SQL aggregate or CDS view] direction LR DB2[(Database: adds up per status)] -->|only 2 rows travel| AS2[Application server: write result] AS2 --> R2[Result: 2 totals] end

The numbers in the figure are an illustration, not a measurement. The point is the arrow labels: the rows that cross from the database to the application server fall from the size of the table to the size of the answer.

The database interface

Your program does not talk to the database directly. The statement you write is passed to the database interface of the application server, which hands it to the database. ABAP SQL is the interface for ordinary programs: you write the same statement whatever the database, and the interface takes care of client handling (the separation of data of different customers inside one system) and table buffering. Native SQL is the other way in: statements in the database’s own dialect, which are passed on without translation. Native SQL is needed only for features ABAP SQL does not have, and it gives up the safeguards of ABAP SQL. The SAP HANA database is the only database supported as the standard database of an application server ABAP, which is why the examples on this page speak about SAP HANA.

The pushdown hierarchy

ABAP offers three levels, and the rule is to use the lowest one that solves the task.

Level What it is Use it when

ABAP SQL

A SELECT statement in the program, with joins, WHERE, aggregate expressions, GROUP BY and SQL expressions.

Almost always first. It runs as one statement on the database and needs no extra object.

ABAP CDS

A CDS view entity that defines a reusable query on the database and is read with ABAP SQL.

Several programs, or a RAP business object or Fiori app, need the same calculation. The view keeps it in one place.

AMDP

A method of an AMDP class whose body is SQLScript, the programming language of the SAP HANA database.

You need a database feature that ABAP SQL and CDS do not offer, or a long process flow that would otherwise move large amounts of data back and forth.

The Keyword Documentation says this directly for AMDP: using AMDP is not recommended if the same task can be achieved using ABAP SQL or ABAP CDS, and AMDP should be used only for access to database-specific functions that ABAP SQL lacks, or when large process flows or analyses that would repeatedly transport large amounts of data between the database and the application server can be moved to the database. AMDP classes can be edited only in ABAP development tools for Eclipse (ADT). The AMDP page of this series (Native SQL, ADBC and AMDP) shows its syntax.

Example: from a loop to one statement

The running example is the expense-claim table ZEXP_CLAIM with the columns CLAIM_ID, STATUS and AMOUNT. The task is to total the amounts per status. The first version is the pattern found in many older reports: read the whole table into an internal table, then loop over it and keep one total per status. This listing is run in the local ABAP lab, which supports SELECT ... INTO TABLE, loops and the three insert rows below.

REPORT zpush_before.

TYPES: BEGIN OF ty_total,
         status TYPE c LENGTH 1,
         amount TYPE i,
       END OF ty_total.
TYPES ty_totals TYPE SORTED TABLE OF ty_total WITH UNIQUE KEY status.

DATA claims TYPE STANDARD TABLE OF zexp_claim WITH EMPTY KEY.
DATA totals TYPE ty_totals.

TYPES ty_claims TYPE STANDARD TABLE OF zexp_claim WITH EMPTY KEY.

INSERT zexp_claim FROM TABLE @( VALUE ty_claims( ( claim_id = 'C-1001' status = 'A' amount = 120 )
                                              ( claim_id = 'C-1002' status = 'A' amount = 80 )
                                              ( claim_id = 'C-1003' status = 'P' amount = 45 ) ) ).

" before: ship every row to the application server, then add up in a loop
SELECT * FROM zexp_claim INTO TABLE @claims.

LOOP AT claims INTO DATA(claim).
  READ TABLE totals ASSIGNING FIELD-SYMBOL(<total>) WITH TABLE KEY status = claim-status.
  IF sy-subrc <> 0.
    INSERT VALUE #( status = claim-status ) INTO TABLE totals ASSIGNING <total>.
  ENDIF.
  <total>-amount = <total>-amount + claim-amount.
ENDLOOP.

LOOP AT totals INTO DATA(total).
  WRITE / |status { total-status }: { total-amount }|.
ENDLOOP.

Output of the run in the local ABAP lab (open-abap, not an SAP system):

status A: 200
status P: 45

Two things are wrong with this version once the table grows. SELECT * moves every column of every row, and the loop adds up on the application server what the database could have added up where the data lives. The second version removes both: the database sums per status and returns one row per status. The local lab cannot run aggregate expressions or GROUP BY, so this listing requires an SAP system; not run here.

" after (step 1): one ABAP SQL statement with an aggregate expression and GROUP BY
SELECT FROM zexp_claim
       FIELDS status, SUM( amount ) AS total_amount
       GROUP BY status
       ORDER BY status
       INTO TABLE @DATA(totals).

LOOP AT totals INTO DATA(total).
  WRITE / |status { total-status }: { total-amount }|.
ENDLOOP.

Requires an SAP system; not run here. Against the same three sample rows it would print the same two lines as the loop version, because the total per status does not depend on where the adding happens.

The rule behind the statement: when GROUP BY is used, every column in the SELECT list that is not an argument of an aggregate function must be listed after GROUP BY, and the aggregate expressions say how the other columns are combined. The Keyword Documentation also notes that GROUP BY cannot be processed by the ABAP SQL in-memory engine, that is, it is always done by the database.

Step 2: the same total as a CDS view

If more than one program needs the totals, define the grouping once as a CDS view entity and read that. A view entity is created in ADT as a data definition; its source states the table, the grouping and the aggregate.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Claim totals per status'
define view entity ZEXP_I_ClaimTotal
  as select from zexp_claim
{
  key status,
      sum( amount ) as total_amount
}
group by status

Requires an SAP system; not run here. The key marks the grouping column as the key of the view. The @AccessControl line says that no access control is defined for this teaching example; a real view would carry one.

The program then reads the view like a table, and the totals are still computed by the database when the statement runs:

SELECT FROM zexp_i_claimtotal
       FIELDS status, total_amount
       ORDER BY status
       INTO TABLE @DATA(totals).

Requires an SAP system; not run here.

Source: CDS DDL: DEFINE VIEW ENTITY (the aggregate-expression syntax follows the SAP-samples file zdemo_abap_cds_ve_agg_exp, which states that aggregate expressions need an alias and a GROUP BY that lists all non-aggregated fields)

The choice between step 1 and step 2 is not about speed, because both run one statement on the database. It is about reuse. A statement in a program stays in that program. A view is a named object that other reports, other views and RAP business objects can build on, and it keeps one definition of "total per status".

An optimisation walkthrough

The same thinking works on an existing report. Take a report that is slow and look for these patterns, in this order.

  1. Find the database reads. Read the report top to bottom and note every SELECT.

  2. Look for a SELECT inside a LOOP or inside another SELECT loop. Each pass triggers another database access. The SAP-samples performance notes say to avoid repeated database access in a loop, and to avoid nested SELECT loops because the number of accesses of the inner loop multiplies by those of the outer one.

  3. Replace row-at-a-time access by one statement: a join, a subquery, or a block-wise INSERT, UPDATE or DELETE with an internal table.

  4. Restrict the columns: name the columns you need instead of SELECT *.

  5. Replace "read then add up" by an aggregate expression and GROUP BY, as in this page.

  6. Only if ABAP SQL cannot express the calculation, define a CDS view or, as the last step, an AMDP method.

A before-and-after of the typical shape, a SELECT inside a loop replaced by one join:

" before: one database access per claim
LOOP AT claims INTO DATA(claim).
  SELECT SINGLE name FROM zexp_employee
         WHERE employee_id = @claim-employee_id
         INTO @DATA(employee_name).
  " ... use employee_name
ENDLOOP.

" after: one statement reads claims and employee names together
SELECT c~claim_id, c~amount, e~name
       FROM zexp_claim AS c
       INNER JOIN zexp_employee AS e ON e~employee_id = c~employee_id
       INTO TABLE @DATA(claims_with_names).

Requires an SAP system; not run here. The table ZEXP_EMPLOYEE and the column EMPLOYEE_ID of ZEXP_CLAIM are an illustration and do not belong to the lab.

Measure before and after, because the gain depends on the data volume and on how well the statement is written. The same performance notes caution that the positive effect of aggregate functions depends on factors such as how efficiently they are used and how many rows are processed.

Legacy tools: database procedure proxies and external views

Before AMDP existed, two Dictionary objects connected ABAP programs to code that lives only in the SAP HANA database. You will find them in older systems.

  • A database procedure proxy is a Dictionary object that represents an SQLScript procedure of the SAP HANA database and is called with the statement CALL DATABASE PROCEDURE. The Keyword Documentation says that AMDP is the replacement technology when the procedure is part of the same database. Proxies are still recommended when a secondary database connection is used to reach SQLScript procedures in a different SAP HANA database.

  • A DDIC external view is a Dictionary view that makes an SAP HANA view of the SAP HANA repository usable in ABAP programs. It works like a structured type and can be the source of a reading ABAP SQL statement.

For new development use ABAP SQL, CDS view entities and AMDP, in that order of preference. Both older objects are described in the Keyword Documentation as supported on SAP HANA XSA but not on SAP HANA XSC, two generations of the SAP HANA application runtime, so check which one a system uses before reusing them.

What older books show, and what has changed

The book ABAP Development for SAP HANA (1st edition, 2014) introduced code pushdown when the SAP HANA database was new to ABAP. Its ideas, push calculation down and keep data movement small, are still the foundation of this page. Several details have changed: ABAP SQL has since gained aggregate and SQL expressions that cover many cases the book solved with database procedures, CDS view entities have replaced the older DDIC-based CDS views in new code, and AMDP is the recommended way to call SQLScript from ABAP. Read the book for the reasoning and this page for the current tools.

Source: SAP PRESS: ABAP Development for SAP HANA (the publisher page lists the successor edition; the 1st edition is out of print)

Summary

  • Moving every row to the application server and adding up in a loop wastes time; asking the database for the answer moves only the result. This is code pushdown.

  • The order is ABAP SQL first, a CDS view entity when a calculation is shared, and AMDP with SQLScript only for what the first two cannot do.

  • A loop that totals rows per group becomes one SELECT with an aggregate expression such as SUM and a GROUP BY.

  • Look for a SELECT in a loop, for SELECT *, and for sums done in ABAP when you optimise an existing report.

  • Database procedure proxies and DDIC external views are legacy tools; AMDP replaces proxies for procedures in the same database.