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.
- Check existence →
EntityNotFoundExceptionif missing - Check permission →
AccessDeniedExceptionif denied - Check state →
IllegalStateExceptionif 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:
- PK lookups are cheap — B-Tree index lookups are O(log n), usually just 3–4 disk I/Os.
- Most update requests succeed — Editing a nonexistent product is the exception, not the rule.
- Optimize at the bottleneck — There’s no rush to optimize before there’s an actual performance issue.
Summary
| Criteria | Single Update | Select-then-Update |
|---|---|---|
| Performance | Faster | 1 extra query |
| Exception handling | Impossible | Clear |
| Maintainability | Harder | Easier |
| Debugging | Inconvenient | Convenient |
Takeaway: Designing predictable and safe systems matters more than raw performance.
From Kakao Tech Campus 3rd cohort Gift API clone coding.