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

kon*_*tin 6 db2 sap 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).
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)
"" 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.
Run Code Online (Sandbox Code Playgroud)

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).

kon*_*tin 3

一位同事猜测,FOR ALL ENTRIES实际上是选择CDS的全部内容并lt_key_values在运行时与内表进行比较。这似乎是对的。

使用该事务,st05我记录了一个 SQL 跟踪,在本例中如下所示FOR ALL ENTRIES

  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'
Run Code Online (Sandbox Code Playgroud)

所以实际发生的情况是:ABAP 选择整个 CDS 内容并将内部表中的值放入类似附加列的内容中。然后它只保留内表和 SQL 结果条目匹配的那些值。==> 数据库级别没有优化 => 性能不佳。