jnosql icon indicating copy to clipboard operation
jnosql copied to clipboard

Optimize DatabaseManager.count(SelectQuery) to Avoid Full Entity Load

Open otaviojava opened this issue 4 months ago • 1 comments

Currently, the default implementation of count(SelectQuery query) in DatabaseManager executes a full select() operation and then counts the results in memory.

⚠️ Current Behavior (Default Fallback)

default long count(SelectQuery query) {
    Objects.requireNonNull(query, "query is required");
    return this.select(DefaultSelectQuery.countBy(query)).count();
}
flowchart TD
  subgraph Current Default Flow
    A1[SelectQuery] --> B1[Run full select]
    B1 --> C1[Return result stream]
    C1 --> D1[Count items in stream]
  end

  subgraph Optimized Driver Flow
    A2[SelectQuery] --> B2[Translate to native count query]
    B2 --> C2[Execute at DB layer and return count]
  end

✅ Tasks

  • [ ] Override count(SelectQuery) in the following database drivers:

    • [ ] MongoDB (jnosql-communication-document-mongodb)
    • [ ] Cassandra (jnosql-communication-column-cassandra)
    • [ ] Couchbase (jnosql-communication-document-couchbase)
    • [ ] HBase (jnosql-communication-column-hbase)
    • [ ] ArangoDB (jnosql-communication-document-arangodb)
    • [ ] Infinispan (jnosql-communication-document-infinispan)
  • [ ] Add documentation to clarify performance and behavior expectations

otaviojava avatar Sep 14 '25 11:09 otaviojava

@dearrudam I will move this one as well to you.

otaviojava avatar Sep 14 '25 11:09 otaviojava

@otaviojava , JNoSQL Infinispan database driver provides a BucketManager only, which doesn't offer count capability, so, I think that we could disconsider it from this optimization request.

dearrudam avatar Dec 15 '25 01:12 dearrudam

@otaviojava , I think that it's better to explore the JNoSQL HBase Database Driver implementation in another task because:

  • It provides a ColumnManager only and such one implementation doesn't support the count by columnFamily method by now.

  • Also, the count by SelectQuery is supporting a limited group of operators (AND, OR, EQUALS and IN). It's using the HBase Get classes only by now.

  • It looks like there are some HBase capabilities (combining Filters (e.g SingleColumnValueFilter) or using Scan classes in some points) that we could use to improve the JNoSQL Database Driver implementation.

What do you think?!

dearrudam avatar Dec 15 '25 02:12 dearrudam

@dearrudam you are right, key-value does not support it as the same as Hbase.

otaviojava avatar Dec 15 '25 07:12 otaviojava

Check the others, such as Tinkerpop and Neo4J.

otaviojava avatar Dec 15 '25 07:12 otaviojava