Bug 1900606 - Gather replicates during alert generation if they are available #9107
+33
−4
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.
Hi there :)
There was a DB overload issue in #9085, so I opened a new one with an improved query.
Normally, when the
PerformanceDatumReplicatetable is searched, the optimizer chooses a full(sequential) table scan, which causes heavy load on the database.I checked the query using the GCP Insights menu, and the previous version took about 2–3 minutes per call, leading to overload.
Now it runs in 0–2 seconds, which solves the overload.
Documenting this here for future reference:
INlookupThe optimizer chose the full(sequential) scan, even though the
performance_datum_idis an indexed column. It overestimates the number of matching rows, assumes a large result set, and decides to do the full scan, regardless of whether data actually exists or not.When the
performance_datumresult is small,performance_datum_replicateis scanned using the index.But as soon as the
performance_datumresult grows even a little, the optimizer falls back to a full table scan.The join itself is fine, but the driving table size affects optimizer decisions and can trigger full scan fallback.
I implemented the ORM code using the query above. The
EXISTSreduces the candidates efficiently andSubqueryguides PostgreSQL to fetch replicates using index lookups instead of full scans.When I tested this in redash, queries usually completed in about 0–2 seconds. (But if the query is not cached or depending on the DB state, it may take a bit longer, though the ORM is designed to encourage index scans.)
Performance Test in Redash
Frequently Used (Large) Signatures
performance_datum_replicateblocks into shared buffers and page cache.Infrequently Used (Small) Signatures
Note
Cache effectiveness scales with signature size and task frequency.
Large signatures → frequently queried → highly likely to be warm-cached.
Small signatures → lightly queried → inherently fast due to low data volume.