DependencyTrack / DependencyTrack/dependency-track
High CPU load caused by commonly executed queries
- Dominant language
- Java
- Stars
- 4.2k
- Forks
- 811
- Avg merge
- 8h 39m
- Merged PRs (30d)
- 237
Description
### Current Behavior
All the profiling/data is being done via GCP Query Insights, as I've been trying to track down causes for high CPU utilization in our environment. Apologies for the length. :)
I'm going to truncate some query details (the individual columns) as this is quite long.
High CPU utilization is being seen by this query:
```
SELECT
FROM
"DEPENDENCYMETRICS" "A0"
WHERE
"A0"."COMPONENT_ID" = $1
```
The current indexes do not seem to cover this particular use case, and this query seems to be quite aggressively used. For our environment:

We are running in GCP on CloudSQL, currently on Postgres 17 but we also experienced this on 15 as well.
Adding an index to cover this particular query drastically reduces CPU load, you can see the exact point when the additional index was added (no changes were made to any of the other indexes):

I believe this is being (partially) caused by this particular query often finding no results (as would be often expected), but because it doesn't have a particularly close match on the index, the cost of it is quite high.
Here's the query plan before adding the additional index:
And after the index:
These are also the next two worst offenders (at least in our environment):
First one:
```
SELECT
FROM
"COMPONENT" "A0"
WHERE
"A0"."PROJECT_ID" = $1
```
The second (and a bunch of others) that all fall under this index also have high utilization.
When I zoom out on metrics and look at our total utilization (about 24 hours), these are the biggest causes:
Some are simple:
```
SELECT
FROM
"PROJECTMETRICS" "A0"
WHERE
"A0"."PROJECT_ID" = $1
```
Most are complex queries, but most of them hit the same indexes.
### Steps to Reproduce
1.No steps to reproduce as this is being seen in a production environment.
### Expected Behavior
Adding strategic indexes for queries with high cost reduces time it takes to run the query lowering load on the DB and improving user experience,
### Dependency-Track Version
4.13.0
### Dependency-Track Distribution
Container Image
### Database Server
PostgreSQL
### Database Server Version
17_4.R20250302.00_07
### Browser
N/A
### Checklist
- [x] I have read and understand the [contributing guidelines](https://github.com/DependencyTrack/dependency-track/blob/master/CONTRIBUTING.md#filing-issues)
- [x] I have checked the [existing issues](https://github.com/DependencyTrack/dependency-track/issues) for whether this defect was already reported
Contributor guide
Assessment
This issue has not been assessed yet.