api: Fix index usage for usage queries #2002
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this pull request do? Explain your changes. (required)
This is to fix the usage queries that have been causing a 20% CPU sustained spike on our DB
for 10 minutes every hour.
This is caused by the way we are filtering by the
createdAt
timestamp, which is not able to usethe existing index on that field since the index is created on the string value, not a number. The fix
was to also query by the string value, which should be fine since timestamps have the same number
of digits until 2286.
We should eventually fix our indexes to optimize for number queries as well, since this is not the
only place where we filter by numbers, but that would be a more complex change that I don't think
is worth to do as an emergency measure while we have a lot of traffic from fishtank right now.
Specific updates (required)
createdAt
field as a string instead of bigintHow did you test each of these updates (required)
Ran the query manually on pgAdmin and ensured the index would be used.
Does this pull request close any open issues?
CPU spikes on DB.
Checklist