Skip to content

Investigate areas where queries with null parameters are bypassing the DataNucleus query compilation cache #2540

Open

Description

@nscuro

Current Behavior

DataNucleus will compile every single JDOQL query into a generic expression tree, and from there into SQL.

It has caches in place for each of these steps, refer to https://www.datanucleus.org/products/accessplatform_6_0/jdo/query.html#cache

While investigating a different issue, I came across this check in DataNucleus:

https://github.com/datanucleus/datanucleus-rdbms/blob/datanucleus-rdbms-6.0.3/src/main/java/org/datanucleus/store/rdbms/query/JDOQLQuery.java#L313-L328

Essentially, if any of the provided parameters to a query is null, the query compilation cache will be bypassed. This can have a big impact on resource utilization in cases where a certain query is both:

  • Used very frequently
  • Expecting null parameter values

This is the case for our getVulnerableSoftwareByCpe23 method for example, which is invoked for every single CPE during NVD mirroring:

public VulnerableSoftware getVulnerableSoftwareByCpe23(String cpe23,
String versionEndExcluding, String versionEndIncluding,
String versionStartExcluding, String versionStartIncluding) {
final Query<VulnerableSoftware> query = pm.newQuery(VulnerableSoftware.class);
query.setFilter("cpe23 == :cpe23 && versionEndExcluding == :versionEndExcluding && versionEndIncluding == :versionEndIncluding && versionStartExcluding == :versionStartExcluding && versionStartIncluding == :versionStartIncluding");
query.setRange(0, 1);
return singleResult(query.executeWithArray(cpe23, versionEndExcluding, versionEndIncluding, versionStartExcluding, versionStartIncluding));
}

Depending on the reported version range, the *Excluding and *Including parameters may be null.

The solution is include null values as part of the JDOQL query, instead of passing it as parameter:

final String myParam = null;
final var query = pm.newQuery(...);
if (myParam == null) {
    query.setFilter("field == null");
} else {
    query.setFilter("field == :myParam");
    query.setParameters(myParam);
}

This way, the query cache can be utilized.

Proposed Behavior

Identify other areas in the code where queries are called frequently, and parameters may be null in certain cases.

Apply the optimization outlined above to ensure that the query compilation cache can be utilized.

Checklist

Activity

  1. added
    enhancement
    p3
    on Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementp3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions