remove N+1 db queries for team names#61471
Open
steveahnahn wants to merge 2 commits intoapache:mainfrom
Open
Conversation
uranusjr
reviewed
Feb 5, 2026
Comment on lines
+2934
to
+2938
| if ( | ||
| executor := self._try_to_load_executor( | ||
| ti, session, team_name=dag_id_to_team_name.get(ti.dag_id, NOTSET) | ||
| ) | ||
| ) is None: |
Member
There was a problem hiding this comment.
Let’s split this to a separate = statement and then if executor is None, this is too long IMO.
uranusjr
reviewed
Feb 5, 2026
Comment on lines
+2885
to
+2888
| dag_id_to_team_name: dict[str, str | None] = {} | ||
| if conf.getboolean("core", "multi_team"): | ||
| unique_dag_ids = {ti.dag_id for ti in task_instances_without_heartbeats} | ||
| dag_id_to_team_name = self._get_team_names_for_dag_ids(unique_dag_ids, session) |
Member
There was a problem hiding this comment.
Suggested change
| dag_id_to_team_name: dict[str, str | None] = {} | |
| if conf.getboolean("core", "multi_team"): | |
| unique_dag_ids = {ti.dag_id for ti in task_instances_without_heartbeats} | |
| dag_id_to_team_name = self._get_team_names_for_dag_ids(unique_dag_ids, session) | |
| if conf.getboolean("core", "multi_team"): | |
| unique_dag_ids = {ti.dag_id for ti in task_instances_without_heartbeats} | |
| dag_id_to_team_name = self._get_team_names_for_dag_ids(unique_dag_ids, session) | |
| else: | |
| dag_id_to_team_name = {} |
uranusjr
approved these changes
Feb 5, 2026
Member
uranusjr
left a comment
There was a problem hiding this comment.
A couple of minor suggestions, overrall logic is good to me.
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.
Summary
Optimize multi-team executor resolution in the scheduler by batching dag_id -> team_name lookups and passing the resolved team_name into executor selection, eliminating per-task DB queries when core.multi_team=true.
With multi-team enabled, the scheduler can process many task instances per loop. Previously, team resolution could trigger repeated database lookups (effectively N queries for N task instances) when selecting executors. This PR reduces that overhead by resolving team names once per unique DAG ID and reusing the results.