Post

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

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

The Question

At Kakao Tech Campus, I was implementing member profile update with JDBC Template. Mentor gave me this feedback:

“Even if a field value hasn’t changed, you’re updating it to the same value. Wouldn’t performance improve if you checked whether each field changed before updating?”


Option 1: Select-then-Update

1
2
3
4
5
6
7
8
9
10
11
public void update(Member member) {
    Member existing = findById(member.getId());
    
    if (!existing.getName().equals(member.getName())) {
        updateName(member.getId(), member.getName());
    }
    if (!existing.getEmail().equals(member.getEmail())) {
        updateEmail(member.getId(), member.getEmail());
    }
    // ... check each field
}

Pros: Only updates changed fields Cons: Always 1 SELECT query, code complexity increases


Option 2: Single Update

1
2
3
4
public void update(Member member) {
    String sql = "UPDATE member SET name = ?, email = ? WHERE id = ?";
    jdbcTemplate.update(sql, member.getName(), member.getEmail(), member.getId());
}

Pros: Simple code, single query Cons: Updates all fields even when unchanged


Mentor’s Feedback

“From what I know, most teams just update all fields without separate checks. The SELECT query cost and code complexity aren’t worth the performance gain in most situations.”

“Performance optimization is worth it when there’s actually a performance problem. Premature optimization can hurt code maintainability instead.”


Lessons Learned

  • Code readability and maintainability is also an important value
  • Don’t think only about performance
  • Optimize when there’s an actual problem, not preemptively
  • Trade-offs always exist - pick what matters more

From Kakao Tech Campus 3rd cohort Gift API clone coding, summarizing mentor feedback.

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