简体   繁体   中英

Which scenario is more efficient?

ALL, I have a DB with some data. I also have a C++ application that communicates with the DB.

Which scenario is more efficient?

Scenario 1:

  1. User hit "Edit Data" button.
  2. Program reads data from DB and presents it for editing
  3. User edit the data and hit "Save" button
  4. Program creates new object with the newly saved data then makes the comparison with the old object.
  5. Program issues some UPDATE statements.

Scenario 2:

  1. Steps 1-3 are the same.
  2. Program overwrites an old object data with the new data.
  3. Program issue DELETE/INSERT statements in one transaction.

I'm inclined to say scenario 2.

Its a question of which is efficient and transactionally sound. Deleting an existing object to perform an update is not transactionally sound. Updates exist to update existing entities in the database. If you are using synthetic keys you cant use this approach at all.

In most system, a refetch (to at least check if the object your about to commit has been modified since you requested it from the database) is mandatory.

Step 4 should read, refetch the object from the database to ensure it hasnt changed, then commit update or rollback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM