Open ercpe opened 2 years ago
Hi,
the __len__ implementation of TableQuerysetData class only checks for the presence of the paginator to determine to use .count() over len(): https://github.com/jieter/django-tables2/blob/master/django_tables2/data.py#L150 Unfortunately, __len__ of a QuerySet is implemented to fetch all records and using len() on the result list: https://github.com/django/django/blob/main/django/db/models/query.py#L302
__len__
TableQuerysetData
paginator
.count()
len()
This can be a really expensive operation. I suggest to change the condition to
if hasattr(self.table, "paginator") or isinstance(self.data, QuerySet):
to utilize the much faster .count() query in case of a QuerySet.
what would be the benefit? without pagination, the whole queryset will be listed anyway, right? so the current code version is already optimized and saves the count() call.
count()
Hi,
the
__len__
implementation ofTableQuerysetData
class only checks for the presence of thepaginator
to determine to use.count()
overlen()
: https://github.com/jieter/django-tables2/blob/master/django_tables2/data.py#L150 Unfortunately,__len__
of a QuerySet is implemented to fetch all records and usinglen()
on the result list: https://github.com/django/django/blob/main/django/db/models/query.py#L302This can be a really expensive operation. I suggest to change the condition to
to utilize the much faster
.count()
query in case of a QuerySet.