我刚刚在一个类中发现了一些代码,想要修改数据库中的一些行。但他没有使用更新或修改,而是使用删除和插入。问过他后,他告诉我,在 SYBASE 上这实际上更快,但他不确定在 HANA 上。我自己尝试了一下。
假设您有这个示例数据库:
@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;
}
这个要测试的示例类:
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.
运行 4 次后,控制台输出如下:
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
我的测试有缺陷吗?鉴于它非常简单。或者如果表中有很多行,它会改变吗?还是像在 SYBASE 和 HANA 上一样,简单的更新/修改是最快的?有时更新速度更快,有时修改速度更快。我认为这两者之间没有性能差异。
编辑: 刚刚再次测试了 1.000.000 行:
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
在我的示例中,更新似乎总是最快的
因为它似乎找到了我的答案(至少在我的开发系统上)并且其他人可能会感兴趣,我在此处复制了测试结果以提供答案: 4 行:
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
有 1.000.000 行:
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
在我的示例中,更新似乎总是最快的