jnosql icon indicating copy to clipboard operation
jnosql copied to clipboard

Optimize DatabaseManager.update(UpdateQuery) to Avoid Full Entity Rewrite

Open otaviojava opened this issue 3 months ago • 1 comments

The current update is implemented as a default method, primarily to ensure compatibility across all NoSQL types. However, this comes with significant performance drawbacks:

  • It fetches the existing entity from the database
  • Deserializes it to Java
  • Updates the field(s) in memory
  • Then saves the entire object back to the database

This process introduces unnecessary round-trips and serialization overhead, especially for document and column-family databases that support native update operations (e.g., $set, SET, etc.).

The goal is provide a custom update() implementation per database driver to take advantage of database-native operations whenever possible — avoiding read/deserialize steps when only partial updates are needed.

📊 Current vs. Target Flow

❌ Current Implementation (Default in Java)

flowchart TD
  subgraph Current Default Flow
    A1[UpdateQuery] --> B1[Select matching entities]
    B1 --> C1[Apply in-memory changes]
    C1 --> D1[Re-insert entire entity]
  end

  subgraph Optimized Driver Flow
    A2[UpdateQuery] --> B2[Translate to native DB update command]
    B2 --> C2[Execute at DB layer]
  end
  • Reduces latency
  • Fewer serialization/deserialization steps
  • Better alignment with database-native update capabilities
  • Enables atomic updates in databases that support them

🛠️ Implementation per database

The code to be overwritten on those drivers

  • [ ] Override update in MongoDB driver to use $set
  • [ ] Override update in Cassandra driver to use UPDATE ... SET
  • [ ] Override update in DynamoDB driver using UpdateItem
  • [ ] Override update in Couchbase using mutateIn
  • [ ] Override update in Neo4j using SET
  • [ ] Override update in ArangoDB using update()
  • [ ] Override update in CouchDB (if native support allows it)

As the next step, break compatibility and move it to a non-default method.

otaviojava avatar Sep 14 '25 11:09 otaviojava

@dearrudam I will move this one to you as well, so you can enhance the database on your time.

otaviojava avatar Sep 14 '25 11:09 otaviojava