mirror of
https://github.com/ansible/awx.git
synced 2026-07-07 06:18:07 -02:30
Optimize HostList API: conditional DISTINCT + composite index on JobHostSummary (#16530)
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
17
awx/main/migrations/0206_jobhostsummary_host_id_idx.py
Normal file
17
awx/main/migrations/0206_jobhostsummary_host_id_idx.py
Normal file
@@ -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',
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user