Skip to content

Commit e4438d6

Browse files
authored
Merge pull request #21134 from mvdbeek/perf_fix_update_cte
[25.1] Improve _touch_collection_update_time_cte performance
2 parents fee2ed0 + 89f4dcb commit e4438d6

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lib/galaxy/model/__init__.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4743,26 +4743,32 @@ def _touch_collection_update_time_cte(self, session, supports_skip_locked):
47434743
literal(0).label("depth_level"),
47444744
]
47454745

4746-
# Create a single base query that covers both HDA and LDDA cases using OR conditions
4747-
base_query = (
4746+
# Create separate base queries for HDA and LDDA cases and union them
4747+
# We need to wrap the union in a subquery to use it as the anchor for the recursive CTE
4748+
union_query = (
47484749
select(*base_columns)
47494750
.select_from(
4750-
DatasetCollectionElement.__table__.outerjoin(
4751+
DatasetCollectionElement.__table__.join(
47514752
HistoryDatasetAssociation.__table__, DatasetCollectionElement.hda_id == HistoryDatasetAssociation.id
4752-
).outerjoin(
4753-
LibraryDatasetDatasetAssociation.__table__,
4754-
DatasetCollectionElement.ldda_id == LibraryDatasetDatasetAssociation.id,
47554753
)
47564754
)
4757-
.where(
4758-
or_(
4759-
HistoryDatasetAssociation.dataset_id == self.id,
4760-
LibraryDatasetDatasetAssociation.dataset_id == self.id,
4755+
.where(HistoryDatasetAssociation.dataset_id == self.id)
4756+
.union(
4757+
select(*base_columns)
4758+
.select_from(
4759+
DatasetCollectionElement.__table__.join(
4760+
LibraryDatasetDatasetAssociation.__table__,
4761+
DatasetCollectionElement.ldda_id == LibraryDatasetDatasetAssociation.id,
4762+
)
47614763
)
4764+
.where(LibraryDatasetDatasetAssociation.dataset_id == self.id)
47624765
)
4763-
)
4766+
).subquery()
4767+
4768+
# Select from the union subquery to create a proper base query for the CTE
4769+
base_query = select(union_query.c.collection_id, union_query.c.depth_level)
47644770

4765-
# Create the recursive CTE from the single base query
4771+
# Create the recursive CTE from the base query
47664772
collection_hierarchy_cte = base_query.cte(name="collection_hierarchy", recursive=True)
47674773

47684774
# Create aliases for the recursive part

0 commit comments

Comments
 (0)