Skip to content

IA-4847: Entities list performance#2827

Merged
Phil-V merged 8 commits intodevelopfrom
IA-4847-entities-list-performance
Mar 23, 2026
Merged

IA-4847: Entities list performance#2827
Phil-V merged 8 commits intodevelopfrom
IA-4847-entities-list-performance

Conversation

@Phil-V
Copy link
Copy Markdown
Contributor

@Phil-V Phil-V commented Mar 18, 2026

What problem is this PR solving?

Improve the entity list performance.

Related JIRA tickets

IA-4847

Changes

  • Avoid expensive annotations when not necessary and use prefetches instead
  • Set default ordering to -id to avoid more expensive ordering if not necessary

How to test

Visit the entities list page and:

  • make sure listing, ordering, and csv/xlsx exports are not broken
  • check that entities with pending duplicates correctly have an extra "show duplicates" button in their "actions" column in the entity list
  • you can create a duplicate entry manually in /admin/iaso/entityduplicate/

Print screen / video

/

Notes

  • Moved some tests from test_entities.py to test_entities_ordering.py. They are unchanged and don't need to be reviewed.
  • switched to -id instead of -created_at as the latter is not indexed (to be potentially addressed in future task)

Doc

/

@Phil-V Phil-V force-pushed the IA-4847-entities-list-performance branch from 11d174c to 1d918b7 Compare March 19, 2026 07:25
@Phil-V Phil-V marked this pull request as ready for review March 19, 2026 07:33
@quang-le quang-le added the release Should be released in production at next deploy label Mar 19, 2026
@hugo093 hugo093 self-requested a review March 23, 2026 11:27
Comment thread iaso/models/entity.py
@property
def pending_duplicate_ids(self):
"""Retrieve the id list of related pending duplicate entities."""
results = set()
Copy link
Copy Markdown
Contributor

@hugo093 hugo093 Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return list(
        self.duplicates1.filter(validation_status=ValidationStatus.PENDING)
        .values_list("id", flat=True)
        .union(
            self.duplicates2.filter(validation_status=ValidationStatus.PENDING)
            .values_list("id", flat=True)
        )
    )

instead ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, using values_list doesn't seem to work with Prefetch. But, I've refined the code to filter on validation_status in the prefetch itself. Let me know if you can think of better ways to architecture this.

Comment thread iaso/models/entity.py
for instance in self.instances.all()
if (saved_at := instance.source_created_at or instance.created_at) is not None
)
return max(instance_dates, default=self.created_at)
Copy link
Copy Markdown
Contributor

@hugo093 hugo093 Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd try to turn this into a sql operation (django ORM) as it might be expensive if there are a lot of instances. Moreover if you do that on a list it might trigger N+1 query problem.

Be also aware that, imho, we should avoid putting @property on something that triggers a DB operation. (E.g some debuggers immediately evaluates it and then you don't get why you have more queries than expected)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on the properties, I've moved them to regular methods.

Comment thread iaso/api/entities/views.py Outdated
Comment thread iaso/api/entities/views.py Outdated
# Handle streaming responses

queryset = self.filter_queryset(self.get_queryset())
queryset = self.filter_queryset(self.get_queryset()).order_by("-id")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to be sure, this would erase any other ordering passed, do we want this ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment to clarify that this is for performance reasons (as there are few indexed columns available on the Entity model and the exports iterate over potentially a very high number of records).

Comment thread iaso/tests/api/entities/test_entities_ordering.py
@Phil-V Phil-V requested a review from hugo093 March 23, 2026 14:18
Copy link
Copy Markdown
Contributor

@hugo093 hugo093 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Phil-V Phil-V merged commit bdb7666 into develop Mar 23, 2026
7 checks passed
@Phil-V Phil-V deleted the IA-4847-entities-list-performance branch March 23, 2026 15:16
@beygorghor beygorghor added the user tested Has already been tested on staging label Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release Should be released in production at next deploy Released user tested Has already been tested on staging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants