fix: Optimize TaskQueueDB matching with composite indices and EXISTS#8462
Merged
fstagni merged 2 commits intoDIRACGrid:integrationfrom Feb 24, 2026
Merged
Conversation
The TaskIndex on multi-value tables (tq_TQTo*) previously only covered TQId, requiring a separate lookup for the Value column. Adding Value to the composite index makes it a covering index for all subqueries that filter on both columns, improving query performance significantly.
Replace COUNT-based subqueries with EXISTS/NOT EXISTS patterns in __generateTQMatchSQL, __generateTagSQLSubCond, and __generateTQFindSQL. EXISTS short-circuits on the first matching row instead of scanning all rows, which combined with the composite (TQId, Value) indices reduces matching query time from ~30ms to ~3ms on production.
495abee to
fe366e0
Compare
Member
Author
|
@fstagni At the dops it's probably worth recommending people do this: Even on lbwms this ran in a few hundred milliseconds so it's pretty safe to do even in a production system. |
aldbr
approved these changes
Feb 19, 2026
Contributor
aldbr
left a comment
There was a problem hiding this comment.
Not an expert of that part of the code but, given the description of the PR, the changes look good to me
fstagni
reviewed
Feb 19, 2026
Contributor
I will do, but just for the sake of organization, I would:
Like this:
|
atsareg
reviewed
Feb 23, 2026
fstagni
requested changes
Feb 24, 2026
fstagni
approved these changes
Feb 24, 2026
Contributor
fstagni
left a comment
There was a problem hiding this comment.
I will merge this one now to not stop the release. The indices can be dropped at any time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
__generateTQMatchSQLmatching query usesCOUNTsubqueries to check whether multi-value tables (tq_TQTo*) have rows for a given TQId. This is inefficient becauseCOUNTscans all matching rows even when we only need to know if any row exists, and the single-columnTaskIndexonTQIdrequires a separate lookup to fetch theValuecolumn.This PR makes two changes:
Composite
(TQId, Value)indices on the multi-value tables, replacing the single-columnTaskIndexonTQId. This makes the index a covering index for all subqueries that filter on both columns. The existing per-field{Field}IndexonValuealone is kept for reverse lookups (e.g. BannedSites antijoin).EXISTS/NOT EXISTSinstead ofCOUNTin__generateTQMatchSQL,__generateTagSQLSubCond, and__generateTQFindSQL.EXISTSshort-circuits on the first matching row rather than counting all of them.Together these drop
__generateTQMatchSQLquery time from ~30 ms to ~3 ms on a production-sized DB (confirmed viaEXPLAIN ANALYZE).Note: For existing deployments the schema change only takes effect on newly created tables (
__initializeDBskips tables that already exist). Production DBs need a one-offALTER TABLEto add the composite index.BEGINRELEASENOTES
*WorkloadManagement
FIX: Optimize TaskQueueDB matching queries by adding composite (TQId, Value) indices and replacing COUNT subqueries with EXISTS/NOT EXISTS
*Deployment
CHANGE: ALTER TABLE tq_TQToBannedSites ADD PRIMARY KEY (TQId, Value), DROP INDEX TaskIndex;
CHANGE: ALTER TABLE tq_TQToGridCEs ADD PRIMARY KEY (TQId, Value), DROP INDEX TaskIndex;
CHANGE: ALTER TABLE tq_TQToGridMiddlewares ADD PRIMARY KEY (TQId, Value), DROP INDEX TaskIndex;
CHANGE: ALTER TABLE tq_TQToJobTypes ADD PRIMARY KEY (TQId, Value), DROP INDEX TaskIndex;
CHANGE: ALTER TABLE tq_TQToPlatforms ADD PRIMARY KEY (TQId, Value), DROP INDEX TaskIndex;
CHANGE: ALTER TABLE tq_TQToSites ADD PRIMARY KEY (TQId, Value), DROP INDEX TaskIndex;
CHANGE: ALTER TABLE tq_TQToTags ADD PRIMARY KEY (TQId, Value), DROP INDEX TaskIndex;
ENDRELEASENOTES