From 41545cfcf074416354aa5845254177dc2a4dac4b Mon Sep 17 00:00:00 2001 From: Dirk Julich Date: Mon, 6 Jul 2026 17:01:20 +0200 Subject: [PATCH] Optimize HostList API: conditional DISTINCT + composite index on JobHostSummary (#16530) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AAP-81517 — Optimize HostList API: conditional DISTINCT + composite index Make .distinct() conditional on host_filter being set — without it the RBAC IN subquery on a direct FK cannot produce duplicates, so DISTINCT is pure overhead. Add composite index (host_id, id DESC) on main_jobhostsummary so the with_latest_summary_id() correlated subquery can use an index-only top-1 scan instead of scanning and sorting. Co-authored-by: Claude Opus 4.6 --- awx/api/views/__init__.py | 3 ++- .../0206_jobhostsummary_host_id_idx.py | 17 +++++++++++++++++ awx/main/models/jobs.py | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 awx/main/migrations/0206_jobhostsummary_host_id_idx.py diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index 522bf2836f..1ec0f83134 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -1926,7 +1926,8 @@ class HostList(HostRelatedSearchMixin, ListCreateAPIView): if filter_string: filter_qs = SmartFilter.query_from_string(filter_string) qs &= filter_qs - return qs.distinct().with_latest_summary_id() + qs = qs.distinct() + return qs.with_latest_summary_id() def list(self, *args, **kwargs): try: diff --git a/awx/main/migrations/0206_jobhostsummary_host_id_idx.py b/awx/main/migrations/0206_jobhostsummary_host_id_idx.py new file mode 100644 index 0000000000..13617aaefc --- /dev/null +++ b/awx/main/migrations/0206_jobhostsummary_host_id_idx.py @@ -0,0 +1,17 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('main', '0205_add_ordering_to_instancegroup_and_workflow_nodes'), + ] + + operations = [ + migrations.AddIndex( + model_name='jobhostsummary', + index=models.Index( + fields=['host', '-id'], + name='main_jobhostsumm_host_id_desc', + ), + ), + ] diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 6a3992686d..9b84488836 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -1092,6 +1092,9 @@ class JobHostSummary(CreatedModifiedModel): unique_together = [('job', 'host_name')] verbose_name_plural = _('job host summaries') ordering = ('-pk',) + indexes = [ + models.Index(fields=['host', '-id'], name='main_jobhostsumm_host_id_desc'), + ] job = models.ForeignKey( 'Job',