Repository navigation
Description
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:
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
nullparameter values
This is the case for our getVulnerableSoftwareByCpe23 method for example, which is invoked for every single CPE during NVD mirroring:
dependency-track/src/main/java/org/dependencytrack/persistence/VulnerableSoftwareQueryManager.java
Lines 126 to 133 in 93321f8
| 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
- I have read and understand the contributing guidelines
- I have checked the existing issues for whether this enhancement was already requested
Activity
Fix `getVulnerableSoftwareByCpe23` bypassing the query compilation cache
Fix `getVulnerableSoftwareByCpe23` bypassing the query compilation cache
Fix `getVulnerableSoftwareByCpe23` bypassing the query compilation cache
Fix `getVulnerableSoftwareByCpe23` bypassing the query compilation cache
Fix `getVulnerableSoftwareByCpe23` bypassing the query compilation cache
Prevent query compilation cache being bypassed for `matchSingleIdenti…
Prevent query compilation cache being bypassed for `matchSingleIdenti…
Prevent query compilation cache being bypassed for `matchSingleIdenti…
Prevent query compilation cache being bypassed for `matchSingleIdenti…
Improve BOM processing performance and make it transactional (#218)
Improve BOM processing performance and make it transactional (#218)
Fix impossible SQL query conditions causing DB indexes to be bypassed
Fix impossible SQL query conditions causing DB indexes to be bypassed
Fix `getVulnerableSoftwareByCpe23` bypassing the query compilation cache
Fix impossible SQL query conditions causing DB indexes to be bypassed