Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `FOR ALL ENTRIES` lower performance of CDS view on DB6?

Tags:

db2

abap

cds

I'm reading data from a SAP Core Data Service (CDS view, SAP R/3, ABAP 7.50) using a WHERE clause on its primary (and only) key column. There is a massive performance decrease when using FOR ALL ENTRIES (about a factor 5):

Reading data using a normal WHERE clause takes about 10 seconds in my case:

SELECT DISTINCT *
FROM ZMY_CDS_VIEW
WHERE prim_key_col eq 'mykey'
INTO TABLE @DATA(lt_table1).

Reading data using FOR ALL ENTRIES with the same WHERE takes about 50 seconds in my case:

"" boilerplate code that creates a table with one entry holding the same key value as above
TYPES: BEGIN OF t_kv,
  key_value like ZMY_CDS_VIEW-prim_key_col,
END OF t_kv.

DATA lt_key_values TYPE TABLE OF t_kv.
DATA ls_key_value TYPE t_kv.
ls_key_value-key_value = 'mykey'.
APPEND ls_key_value TO lt_key_values.
SELECT *
FROM ZMY_CDS_VIEW
FOR ALL ENTRIES IN @lt_key_values
WHERE prim_key_col eq @lt_key_values-key_value
INTO TABLE @DATA(lt_table2). 

I do not understand why the same selection takes five times as long when utilising FOR ALL ENTRIES. Since the table lt_key_values has only 1 entry I'd expect the database (sy-dbsys is 'DB6' in my case) to do exactly the same operations plus maybe some small neglectable overhead ≪ 40s.

Selecting from the underlying SQL view instead of the CDS (with its Access Control and so on) makes no difference at all, neither does adding or removing the DISTINCT key word (because FOR ALL ENTRIES implies DISTINCT).

like image 400
koks der drache Avatar asked Jun 12 '19 07:06

koks der drache


People also ask

What is incorrect about CDS projection list?

The newly defined associations in CDS projection view have limitations though: they can be defined and exposed in the element list, but they cannot be used to include fields from the association target in the current projection view.

Can you do DML in CDS views?

Getting started with CDS Views It is an extension of SQL and at the same time a data definition language (DDL), a data manipulation language (DML) and an authorization control language (DCL). With this, we can create a virtual data model called VDM (Virtual Data Model).

How can I improve my cds performance?

Conclusion : Keep CDS views simple (in particular service-Quality A and B = #BASIC views) Amount of data persisted in S4 CDS views should not exceed 20% of the overall data volume of the system. In transactional processing, only use simple CDS views accessed via CDS key.

How do I use all entries in AMDP?

For all entries: In ABAP, FOR ALL ENTRIES statement is used to fetch corresponding data for all the records in the source or result package. The same functionality can be achieved using “LEFT OUTER JOIN” in the AMDP script.


1 Answers

A colleague guessed, that the FOR ALL ENTRIES is actually selecting the entire content of the CDS and comparing it with the internal table lt_key_values at runtime. This seems about right.

Using the transaction st05 I recorded a SQL trace that looks like the following in the FOR ALL ENTRIES case:

  SELECT
     DISTINCT "ZMY_UNDERLYING_SQL_VIEW".*
   FROM
     "ZMY_UNDERLYING_SQL_VIEW",
     TABLE( SAPTOOLS.MEMORY_TABLE( CAST( ? AS BLOB( 2G )) ) CARDINALITY 1 ) AS "t_00" ( "C_0" VARCHAR(30) )
   WHERE
         "ZMY_UNDERLYING_SQL_VIEW"."MANDT" = ?
     AND "ZMY_UNDERLYING_SQL_VIEW"."PRIM_KEY_COL" = "t_00"."C_0"

   [...]

Variables

   A0(IT,13)       = ITAB[1x1(20)]
   A1(CH,10)       = 'mykey'
   A2(CH,3)        = '100'

So what actually happens is: ABAP selects the entire CDS content and puts the value from the internal table in something like an additional column. Then it only keeps those values where internal table and SQL result entry do match. ==> No optimzation on database level => bad performance.

like image 95
koks der drache Avatar answered Oct 21 '22 05:10

koks der drache