Is a DELETE followed by an Insert faster than a Single MODIFY?

113 views Asked by At

I just found in a Class some Code, that wants to modify some rows in a Database. But instead of an Update or Modify, he used a Delete and an Insert. After asking him he told me, on SYBASE this was actually faster, but he isn't sure on HANA. I tried to test it myself.

Imagine you have this Sample Database:

@EndUserText.label : 'Datenbanktest'
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zdb_test {
  key mandt : mandt not null;
  key col1  : char20 not null;
  key col2  : char20 not null;
  key col3  : char20 not null;
  col4      : char20 not null;

}

And this Sample Class to Test:

CLASS zcl_db_test DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS zcl_db_test IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA tab TYPE STANDARD TABLE OF zdb_test.

    tab = VALUE #(
                ( mandt = sy-mandt col1 = '1' col2 = '2'  col3 = '3'  col4 = '4' )
                ( mandt = sy-mandt col1 = '5' col2 = '6'  col3 = '7'  col4 = '8' )
                ( mandt = sy-mandt col1 = '9' col2 = '10' col3 = '11' col4 = '12' )
    ).

    "Variant 1
    GET RUN TIME FIELD FINAL(t1).
    DELETE zdb_test FROM TABLE tab.
    INSERT zdb_test FROM TABLE tab.
    GET RUN TIME FIELD FINAL(t2).

    "Variant 2
    GET RUN TIME FIELD FINAL(t3).
    MODIFY zdb_test FROM TABLE tab.
    GET RUN TIME FIELD FINAL(t4).

    "Variant 3
    GET RUN TIME FIELD FINAL(t5).
    UPDATE zdb_test FROM TABLE tab.
    GET RUN TIME FIELD FINAL(t6).

    out->write( |Delete + Insert: { t2 - t1 NUMBER = ENVIRONMENT }µs| ).
    out->write( |Modify: { t4 - t3 NUMBER = ENVIRONMENT }µs| ).
    out->write( |Update: { t6 - t5 NUMBER = ENVIRONMENT }µs| ).
  ENDMETHOD.

ENDCLASS.

After running that 4 Times the Console output is like that:

Delete + Insert: 1.693µs
Modify: 753µs
Update: 1.122µs

Delete + Insert: 1.630µs
Modify: 878µs
Update: 739µs

Delete + Insert: 1.979µs
Modify: 813µs
Update: 770µs

Delete + Insert: 1.867µs
Modify: 781µs
Update: 1.097µs

Is my test flawed? Given its pretty simple. Or does it change if the Table has many rows in it? Or was it like that on SYBASE and on HANA a simple Update/Modify is the fastest? Sometimes Update is faster, sometimes Modify. I don't think there's a Performance Difference between those 2.

Edit: Just tested it again with 1.000.000 Rows:

Delete + Insert: 5.785.950µs
Modify: 5.335.416µs
Update: 4.350.517µs

Delete + Insert: 10.169.629µs
Modify: 5.158.889µs
Update: 4.414.202µs

Delete + Insert: 10.066.633µs
Modify: 5.243.362µs
Update: 4.583.713µs

Delete + Insert: 10.232.670µs
Modify: 5.079.865µs
Update: 4.689.990µs

Seems like Update is always the fastest in my Example

1

There are 1 answers

3
Schesam On BEST ANSWER

Cause it seems like it found my answer (at least on my Development System) and it might be interesting for others, i copied the Test results here to provide an Answer: 4 Rows:

Delete + Insert: 1.693µs
Modify: 753µs
Update: 1.122µs

Delete + Insert: 1.630µs
Modify: 878µs
Update: 739µs

Delete + Insert: 1.979µs
Modify: 813µs
Update: 770µs

Delete + Insert: 1.867µs
Modify: 781µs
Update: 1.097µs

With 1.000.000 Rows:

Delete + Insert: 5.785.950µs
Modify: 5.335.416µs
Update: 4.350.517µs

Delete + Insert: 10.169.629µs
Modify: 5.158.889µs
Update: 4.414.202µs

Delete + Insert: 10.066.633µs
Modify: 5.243.362µs
Update: 4.583.713µs

Delete + Insert: 10.232.670µs
Modify: 5.079.865µs
Update: 4.689.990µs

Seems like Update is always the fastest in my Example