Post

Select-then-Update vs Single Update: What Matters More Than Performance

Select-then-Update vs Single Update: What Matters More Than Performance

Problem

While building the Gift API clone at Kakao Tech Campus, I needed to implement a product update API. In a JDBC environment, there were two main approaches:

Method 1: Single Update Query

  • One UPDATE product SET ... WHERE id = ? and done
  • Just 1 query

Method 2: Select then Update

  • Fetch first, modify the object, then save
  • 2 queries

From a performance standpoint, Method 1 wins. But I went with Method 2.


Limitation of Method 1: Ambiguity of affected rows

A single Update query returns affected rows—the number of rows changed. If it’s 0, you might assume the update failed.

The problem? You can’t tell why it’s 0.

  • Does the ID not exist?
  • Does the user lack permission?
  • Did some business condition (status, deleted_at, etc.) fail?

From the client’s perspective, “Product does not exist” and “You don’t have permission” mean completely different things—but affected rows alone can’t tell them apart.


Advantage of Method 2: Clear Exception Separation

By fetching the domain object first, you can separate validation logic at the application level.

  1. Check existence → EntityNotFoundException if missing
  2. Check permission → AccessDeniedException if denied
  3. Check state → IllegalStateException if already deleted

This way, each exception is clearly separated, making debugging easier and API responses more meaningful.


Is the Performance Overhead Acceptable?

You might worry about the extra query, but here’s my reasoning:

  1. PK lookups are cheap — B-Tree index lookups are O(log n), usually just 3–4 disk I/Os.
  2. Most update requests succeed — Editing a nonexistent product is the exception, not the rule.
  3. Optimize at the bottleneck — There’s no rush to optimize before there’s an actual performance issue.

Summary

CriteriaSingle UpdateSelect-then-Update
PerformanceFaster1 extra query
Exception handlingImpossibleClear
MaintainabilityHarderEasier
DebuggingInconvenientConvenient

Takeaway: Designing predictable and safe systems matters more than raw performance.


From Kakao Tech Campus 3rd cohort Gift API clone coding.

This post is licensed under CC BY 4.0 by the author.