Skip to content

Commit

Permalink
Merge pull request #5008 from kobotoolbox/fix-timeout-in-project-view…
Browse files Browse the repository at this point in the history
…-new-class

Fix project view table timeout
  • Loading branch information
noliveleger authored Jul 17, 2024
2 parents 4c4856f + aee9d7d commit dd8dec6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kobo/apps/project_views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from kpi.mixins.object_permission import ObjectPermissionViewSetMixin
from kpi.models import Asset, ProjectViewExportTask
from kpi.paginators import FastAssetPagination
from kpi.permissions import IsAuthenticated
from kpi.serializers.v2.asset import AssetMetadataListSerializer
from kpi.serializers.v2.user import UserListSerializer
Expand Down Expand Up @@ -52,6 +53,7 @@ def get_queryset(self, *args, **kwargs):
detail=True,
methods=['GET'],
filter_backends=[SearchFilter, AssetOrderingFilter],
pagination_class=FastAssetPagination,
)
def assets(self, request, uid):
if not user_has_view_perms(request.user, uid):
Expand Down
15 changes: 15 additions & 0 deletions kpi/paginators.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ class DataPagination(LimitOffsetPagination):
offset_query_param = 'start'
max_limit = settings.SUBMISSION_LIST_LIMIT


class FastAssetPagination(Paginated):
"""
Pagination class optimized for faster counting for DISTINCT queries on large tables.
This class overrides the get_count() method to only look at the primary key field, avoiding expensive DISTINCTs
comparing several fields. This may not work for queries with lots of joins, especially with one-to-many or
many-to-many type relationships.
"""

def get_count(self, queryset):
if queryset.query.distinct:
return queryset.only('pk').count()
return super().get_count(queryset)


class TinyPaginated(PageNumberPagination):
"""
Expand Down

0 comments on commit dd8dec6

Please sign in to comment.