You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "joshowen (via GitHub)" <gi...@apache.org> on 2023/08/09 01:17:11 UTC

[GitHub] [airflow] joshowen opened a new pull request, #33242: Index optimized fast path to avoid more complicated & slower groupby queryplan

joshowen opened a new pull request, #33242:
URL: https://github.com/apache/airflow/pull/33242

   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   Optimized queryplan to avoid single item groupby which isn't able to efficiently use the `dag_run_dag_id_execution_date_key` index
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on a diff in pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1293050158


##########
airflow/models/dag.py:
##########
@@ -2925,21 +2925,44 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-            most_recent_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                most_recent_subq = (
+                    select(func.max(DagRun.execution_date).label("max_execution_date"))
+                    .where(
+                        DagRun.dag_id == existing_dags[0],
+                        or_(
+                            DagRun.run_type == DagRunType.BACKFILL_JOB,
+                            DagRun.run_type == DagRunType.SCHEDULED,
+                        ),
+                    )
+                    .subquery()
                 )
-                .group_by(DagRun.dag_id)
-                .subquery()
-            )
-            most_recent_runs_iter = session.scalars(
-                select(DagRun).where(
-                    DagRun.dag_id == most_recent_subq.c.dag_id,
-                    DagRun.execution_date == most_recent_subq.c.max_execution_date,
+                most_recent_runs_iter = session.scalars(
+                    select(DagRun).where(
+                        DagRun.dag_id == existing_dags[0],

Review Comment:
   Would the optimization work if we use `DagRun.dag_id == most_recent_subq.c.dag_id` for both cases? If so we can reduce the code a lot.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] eladkal commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "eladkal (via GitHub)" <gi...@apache.org>.
eladkal commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1717373874

   @joshowen can you add the needed tests?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] joshowen commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1723654225

   @uranusjr @amoghrajesh @eladkal I've added tests to cover both paths and rebased.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1456302037


##########
airflow/models/dag.py:
##########
@@ -3068,21 +3068,45 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest automated dag run for each existing dag as a single query (avoid n+1 query)
-            last_automated_runs_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                existing_dag_id = list(existing_dags)[0]

Review Comment:
   `existing_dags` is a dict in this code, so this is grabbing the first key which is a dag_id.  https://github.com/apache/airflow/pull/33242/files#diff-62c8e300ee91e0d59f81e0ea5d30834f04db71ae74f2e155a10b51056b00b59bR3053



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1896456594

   hi @joshowen i moved the query builder to a function and added some testing for the query structure.  please take a look.  i changed the structure of the query slightly so that we don't have to join to the subquery in the 1 dag path (it can be just execution_date = (subquery))


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on a diff in pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1293530440


##########
airflow/models/dag.py:
##########
@@ -2925,21 +2925,44 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-            most_recent_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                most_recent_subq = (
+                    select(func.max(DagRun.execution_date).label("max_execution_date"))
+                    .where(
+                        DagRun.dag_id == existing_dags[0],
+                        or_(
+                            DagRun.run_type == DagRunType.BACKFILL_JOB,
+                            DagRun.run_type == DagRunType.SCHEDULED,
+                        ),
+                    )
+                    .subquery()
                 )
-                .group_by(DagRun.dag_id)
-                .subquery()
-            )
-            most_recent_runs_iter = session.scalars(
-                select(DagRun).where(
-                    DagRun.dag_id == most_recent_subq.c.dag_id,
-                    DagRun.execution_date == most_recent_subq.c.max_execution_date,
+                most_recent_runs_iter = session.scalars(
+                    select(DagRun).where(
+                        DagRun.dag_id == existing_dags[0],

Review Comment:
   So the answer is no?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] joshowen commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1678691128

   > I think we need a test case to cover the one DAG case.
   
   The one dag case is pretty well covered in the integration tests.  I previously had a bug in this code and used those to find it (https://github.com/apache/airflow/actions/runs/5857890497/job/15898563419?pr=33242#step:5:1267).  I'm happy to add a specific test case though if that is preferable though.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] joshowen commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1673590783

   @uranusjr I didn't see a clean way to avid creating 2 branches of the query while maintaining readability.  This change touches both the subquery and the where clause of the parent.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] joshowen commented on a diff in pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1293671824


##########
airflow/models/dag.py:
##########
@@ -2925,21 +2925,44 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-            most_recent_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                most_recent_subq = (
+                    select(func.max(DagRun.execution_date).label("max_execution_date"))
+                    .where(
+                        DagRun.dag_id == existing_dags[0],
+                        or_(
+                            DagRun.run_type == DagRunType.BACKFILL_JOB,
+                            DagRun.run_type == DagRunType.SCHEDULED,
+                        ),
+                    )
+                    .subquery()
                 )
-                .group_by(DagRun.dag_id)
-                .subquery()
-            )
-            most_recent_runs_iter = session.scalars(
-                select(DagRun).where(
-                    DagRun.dag_id == most_recent_subq.c.dag_id,
-                    DagRun.execution_date == most_recent_subq.c.max_execution_date,
+                most_recent_runs_iter = session.scalars(
+                    select(DagRun).where(
+                        DagRun.dag_id == existing_dags[0],

Review Comment:
   I think the answer is no.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1681713627

   Both branches should be covered by tests, whichever covers which.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1894074083

   This is a good optimization.  We ran into this exact issue and came up with the same optimization independently.  But we were thinking to just add an index since there are a couple other instances of if very similar query that I don't think can do the same thing.
   
   But even if we do add the index I don't think it hurts to have both.
   
   My one suggestion would be to move the query generation to a private function somewhere so that things are more readable at the call site.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] joshowen commented on a diff in pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1293467952


##########
airflow/models/dag.py:
##########
@@ -2925,21 +2925,44 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-            most_recent_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                most_recent_subq = (
+                    select(func.max(DagRun.execution_date).label("max_execution_date"))
+                    .where(
+                        DagRun.dag_id == existing_dags[0],
+                        or_(
+                            DagRun.run_type == DagRunType.BACKFILL_JOB,
+                            DagRun.run_type == DagRunType.SCHEDULED,
+                        ),
+                    )
+                    .subquery()
                 )
-                .group_by(DagRun.dag_id)
-                .subquery()
-            )
-            most_recent_runs_iter = session.scalars(
-                select(DagRun).where(
-                    DagRun.dag_id == most_recent_subq.c.dag_id,
-                    DagRun.execution_date == most_recent_subq.c.max_execution_date,
+                most_recent_runs_iter = session.scalars(
+                    select(DagRun).where(
+                        DagRun.dag_id == existing_dags[0],

Review Comment:
   That would still require the Group By in the subquery, which is what the optimized path avoids.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1456302037


##########
airflow/models/dag.py:
##########
@@ -3068,21 +3068,45 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest automated dag run for each existing dag as a single query (avoid n+1 query)
-            last_automated_runs_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                existing_dag_id = list(existing_dags)[0]

Review Comment:
   Existing Dags is a dict in this code, so this is grabbing the first key.  https://github.com/apache/airflow/pull/33242/files#diff-62c8e300ee91e0d59f81e0ea5d30834f04db71ae74f2e155a10b51056b00b59bR3053



##########
airflow/models/dag.py:
##########
@@ -3068,21 +3068,45 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest automated dag run for each existing dag as a single query (avoid n+1 query)
-            last_automated_runs_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                existing_dag_id = list(existing_dags)[0]

Review Comment:
   Existing Dags is a dict in this code, so this is grabbing the first key which is a dag_id.  https://github.com/apache/airflow/pull/33242/files#diff-62c8e300ee91e0d59f81e0ea5d30834f04db71ae74f2e155a10b51056b00b59bR3053



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1782978113

   @uranusjr @amoghrajesh @eladkal Thoughts on this PR?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1670796800

   Also please set up pre-commit according to the contribution guide and fix linting issues.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1678457291

   I think we need a test case to cover the one DAG case.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1456302037


##########
airflow/models/dag.py:
##########
@@ -3068,21 +3068,45 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest automated dag run for each existing dag as a single query (avoid n+1 query)
-            last_automated_runs_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                existing_dag_id = list(existing_dags)[0]

Review Comment:
   `existing_dags` is a dict in this code, so this is grabbing the first and only key which is a dag_id.  https://github.com/apache/airflow/pull/33242/files#diff-62c8e300ee91e0d59f81e0ea5d30834f04db71ae74f2e155a10b51056b00b59bR3053



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33242:
URL: https://github.com/apache/airflow/pull/33242#discussion_r1456148944


##########
airflow/models/dag.py:
##########
@@ -3068,21 +3068,45 @@ def bulk_write_to_db(
         # Skip these queries entirely if no DAGs can be scheduled to save time.
         if any(dag.timetable.can_be_scheduled for dag in dags):
             # Get the latest automated dag run for each existing dag as a single query (avoid n+1 query)
-            last_automated_runs_subq = (
-                select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-                .where(
-                    DagRun.dag_id.in_(existing_dags),
-                    or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+            if len(existing_dags) == 1:
+                # Index optimized fast path to avoid more complicated & slower groupby queryplan
+                existing_dag_id = list(existing_dags)[0]

Review Comment:
   ```suggestion
                   existing_dag_id = list(existing_dags)[0].dag_id
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "joshowen (via GitHub)" <gi...@apache.org>.
joshowen commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1898635315

   > hi @joshowen i moved the query builder to a function and added some testing for the query structure. please take a look. i changed the structure of the query slightly so that we don't have to join to the subquery in the 1 dag path (it can be just execution_date = (subquery))
   
   lgtm.  Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Index optimized fast path to avoid slower groupby queryplan [airflow]

Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish merged PR #33242:
URL: https://github.com/apache/airflow/pull/33242


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on pull request #33242: Index optimized fast path to avoid slower groupby queryplan

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on PR #33242:
URL: https://github.com/apache/airflow/pull/33242#issuecomment-1670795994

   Instead o duplicating much of the query in two branches, I’d extract the actually different part (there `where` argument) instead.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org