Skip to content

Commit

Permalink
job_seekers_views: improve performance of the query
Browse files Browse the repository at this point in the history
This is a big queryset to build, it is better to split the requests.
  • Loading branch information
EwenKorr committed Feb 7, 2025
1 parent 887f37b commit 91ee249
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 79 deletions.
41 changes: 26 additions & 15 deletions itou/www/job_seekers_views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.auth.mixins import UserPassesTestMixin
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.db.models import Count, DateTimeField, Exists, IntegerField, Max, OuterRef, Q, Subquery
from django.db.models import Count, DateTimeField, IntegerField, Max, OuterRef, Q, Subquery
from django.db.models.functions import Coalesce
from django.forms import ValidationError
from django.http import Http404, HttpResponseRedirect
Expand Down Expand Up @@ -126,10 +126,31 @@ def __init__(self):
def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
if self.test_func():
job_seekers_created_by_user = User.objects.filter(kind=UserKind.JOB_SEEKER, created_by=request.user)

if request.current_organization:
job_seekers_created_by_orga = User.objects.filter(
kind=UserKind.JOB_SEEKER,
jobseeker_profile__created_by_prescriber_organization=request.current_organization,
)
job_seekers_applications = User.objects.filter(
(
Q(job_applications__sender=request.user)
& Q(job_applications__sender_prescriber_organization__isnull=True)
)
| Q(job_applications__sender_prescriber_organization=request.current_organization),
kind=UserKind.JOB_SEEKER,
)
else:
job_seekers_created_by_orga = User.objects.none()
job_seekers_applications = User.objects.filter(
job_applications__sender=request.user, kind=UserKind.JOB_SEEKER
)
self.job_seekers_ids = job_seekers_created_by_user.union(
job_seekers_created_by_orga, job_seekers_applications
).values_list("id", flat=True)
self.form = FilterForm(
User.objects.filter(kind=UserKind.JOB_SEEKER).filter(
Exists(self._get_user_job_applications()) | self._get_user_jobseekers_created()
),
User.objects.filter(kind=UserKind.JOB_SEEKER).filter(pk__in=self.job_seekers_ids),
self.request.GET or None,
request_user=request.user,
)
Expand Down Expand Up @@ -157,19 +178,9 @@ def _get_user_job_applications(self):
job_seeker=OuterRef("pk")
)

def _get_user_jobseekers_created(self):
if self.request.user.is_prescriber:
filter = Q(created_by=self.request.user)
if self.request.current_organization:
filter |= Q(jobseeker_profile__created_by_prescriber_organization=self.request.current_organization)
return filter
else:
return Q()

def get_queryset(self):
queryset = super().get_queryset()
user_applications = self._get_user_job_applications()
user_job_seekers_created_by_organization = self._get_user_jobseekers_created()
subquery_count = Subquery(
user_applications.values("job_seeker").annotate(count=Count("pk")).values("count"),
output_field=IntegerField(),
Expand All @@ -186,7 +197,7 @@ def get_queryset(self):
),
output_field=IntegerField(),
)
query = queryset.filter(Exists(user_applications) | user_job_seekers_created_by_organization).annotate(
query = queryset.filter(pk__in=self.job_seekers_ids).annotate(
job_applications_nb=Coalesce(subquery_count, 0),
last_updated_at=subquery_last_update,
valid_eligibility_diagnosis=subquery_diagnosis,
Expand Down
196 changes: 132 additions & 64 deletions tests/www/job_seekers_views/__snapshots__/test_list.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,29 @@
"users_user"."address_filled_at",
"users_user"."first_login"
FROM "users_user"
LEFT OUTER JOIN "users_jobseekerprofile" ON ("users_user"."id" = "users_jobseekerprofile"."user_id")
WHERE ("users_user"."kind" = %s
AND (EXISTS
(SELECT %s AS "a"
FROM "job_applications_jobapplication" U0
WHERE (((U0."sender_id" = %s
AND U0."sender_prescriber_organization_id" IS NULL)
OR U0."sender_prescriber_organization_id" = %s)
AND U0."job_seeker_id" = ("users_user"."id"))
LIMIT 1)
OR "users_user"."created_by_id" = %s
OR "users_jobseekerprofile"."created_by_prescriber_organization_id" = %s))
AND "users_user"."id" IN (
(SELECT U0."id" AS "col1"
FROM "users_user" U0
WHERE (U0."created_by_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "users_jobseekerprofile" U1 ON (U0."id" = U1."user_id")
WHERE (U1."created_by_prescriber_organization_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "job_applications_jobapplication" U1 ON (U0."id" = U1."job_seeker_id")
WHERE (((U1."sender_id" = %s
AND U1."sender_prescriber_organization_id" IS NULL)
OR U1."sender_prescriber_organization_id" = %s)
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)))
ORDER BY "users_user"."first_name" ASC,
"users_user"."last_name" ASC
''',
Expand Down Expand Up @@ -293,18 +304,29 @@
'sql': '''
SELECT COUNT(*) AS "__count"
FROM "users_user"
LEFT OUTER JOIN "users_jobseekerprofile" ON ("users_user"."id" = "users_jobseekerprofile"."user_id")
WHERE ("users_user"."kind" = %s
AND (EXISTS
(SELECT %s AS "a"
FROM "job_applications_jobapplication" U0
WHERE (((U0."sender_id" = %s
AND U0."sender_prescriber_organization_id" IS NULL)
OR U0."sender_prescriber_organization_id" = %s)
AND U0."job_seeker_id" = ("users_user"."id"))
LIMIT 1)
OR "users_user"."created_by_id" = %s
OR "users_jobseekerprofile"."created_by_prescriber_organization_id" = %s))
AND "users_user"."id" IN (
(SELECT U0."id" AS "col1"
FROM "users_user" U0
WHERE (U0."created_by_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "users_jobseekerprofile" U1 ON (U0."id" = U1."user_id")
WHERE (U1."created_by_prescriber_organization_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "job_applications_jobapplication" U1 ON (U0."id" = U1."job_seeker_id")
WHERE (((U1."sender_id" = %s
AND U1."sender_prescriber_organization_id" IS NULL)
OR U1."sender_prescriber_organization_id" = %s)
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)))
''',
}),
dict({
Expand Down Expand Up @@ -409,16 +431,28 @@
FROM "users_user"
LEFT OUTER JOIN "users_jobseekerprofile" ON ("users_user"."id" = "users_jobseekerprofile"."user_id")
WHERE ("users_user"."kind" = %s
AND (EXISTS
(SELECT %s AS "a"
FROM "job_applications_jobapplication" U0
WHERE (((U0."sender_id" = %s
AND U0."sender_prescriber_organization_id" IS NULL)
OR U0."sender_prescriber_organization_id" = %s)
AND U0."job_seeker_id" = ("users_user"."id"))
LIMIT 1)
OR "users_user"."created_by_id" = %s
OR "users_jobseekerprofile"."created_by_prescriber_organization_id" = %s))
AND "users_user"."id" IN (
(SELECT U0."id" AS "col1"
FROM "users_user" U0
WHERE (U0."created_by_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "users_jobseekerprofile" U1 ON (U0."id" = U1."user_id")
WHERE (U1."created_by_prescriber_organization_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "job_applications_jobapplication" U1 ON (U0."id" = U1."job_seeker_id")
WHERE (((U1."sender_id" = %s
AND U1."sender_prescriber_organization_id" IS NULL)
OR U1."sender_prescriber_organization_id" = %s)
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)))
ORDER BY "users_user"."first_name" ASC,
"users_user"."last_name" ASC
LIMIT 4
Expand Down Expand Up @@ -773,18 +807,29 @@
"users_user"."address_filled_at",
"users_user"."first_login"
FROM "users_user"
LEFT OUTER JOIN "users_jobseekerprofile" ON ("users_user"."id" = "users_jobseekerprofile"."user_id")
WHERE ("users_user"."kind" = %s
AND (EXISTS
(SELECT %s AS "a"
FROM "job_applications_jobapplication" U0
WHERE (((U0."sender_id" = %s
AND U0."sender_prescriber_organization_id" IS NULL)
OR U0."sender_prescriber_organization_id" = %s)
AND U0."job_seeker_id" = ("users_user"."id"))
LIMIT 1)
OR "users_user"."created_by_id" = %s
OR "users_jobseekerprofile"."created_by_prescriber_organization_id" = %s))
AND "users_user"."id" IN (
(SELECT U0."id" AS "col1"
FROM "users_user" U0
WHERE (U0."created_by_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "users_jobseekerprofile" U1 ON (U0."id" = U1."user_id")
WHERE (U1."created_by_prescriber_organization_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "job_applications_jobapplication" U1 ON (U0."id" = U1."job_seeker_id")
WHERE (((U1."sender_id" = %s
AND U1."sender_prescriber_organization_id" IS NULL)
OR U1."sender_prescriber_organization_id" = %s)
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)))
ORDER BY "users_user"."first_name" ASC,
"users_user"."last_name" ASC
''',
Expand Down Expand Up @@ -817,18 +862,29 @@
'sql': '''
SELECT COUNT(*) AS "__count"
FROM "users_user"
LEFT OUTER JOIN "users_jobseekerprofile" ON ("users_user"."id" = "users_jobseekerprofile"."user_id")
WHERE ("users_user"."kind" = %s
AND (EXISTS
(SELECT %s AS "a"
FROM "job_applications_jobapplication" U0
WHERE (((U0."sender_id" = %s
AND U0."sender_prescriber_organization_id" IS NULL)
OR U0."sender_prescriber_organization_id" = %s)
AND U0."job_seeker_id" = ("users_user"."id"))
LIMIT 1)
OR "users_user"."created_by_id" = %s
OR "users_jobseekerprofile"."created_by_prescriber_organization_id" = %s))
AND "users_user"."id" IN (
(SELECT U0."id" AS "col1"
FROM "users_user" U0
WHERE (U0."created_by_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "users_jobseekerprofile" U1 ON (U0."id" = U1."user_id")
WHERE (U1."created_by_prescriber_organization_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "job_applications_jobapplication" U1 ON (U0."id" = U1."job_seeker_id")
WHERE (((U1."sender_id" = %s
AND U1."sender_prescriber_organization_id" IS NULL)
OR U1."sender_prescriber_organization_id" = %s)
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)))
''',
}),
dict({
Expand Down Expand Up @@ -933,16 +989,28 @@
FROM "users_user"
LEFT OUTER JOIN "users_jobseekerprofile" ON ("users_user"."id" = "users_jobseekerprofile"."user_id")
WHERE ("users_user"."kind" = %s
AND (EXISTS
(SELECT %s AS "a"
FROM "job_applications_jobapplication" U0
WHERE (((U0."sender_id" = %s
AND U0."sender_prescriber_organization_id" IS NULL)
OR U0."sender_prescriber_organization_id" = %s)
AND U0."job_seeker_id" = ("users_user"."id"))
LIMIT 1)
OR "users_user"."created_by_id" = %s
OR "users_jobseekerprofile"."created_by_prescriber_organization_id" = %s))
AND "users_user"."id" IN (
(SELECT U0."id" AS "col1"
FROM "users_user" U0
WHERE (U0."created_by_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "users_jobseekerprofile" U1 ON (U0."id" = U1."user_id")
WHERE (U1."created_by_prescriber_organization_id" = %s
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)
UNION
(SELECT U0."id" AS "col1"
FROM "users_user" U0
INNER JOIN "job_applications_jobapplication" U1 ON (U0."id" = U1."job_seeker_id")
WHERE (((U1."sender_id" = %s
AND U1."sender_prescriber_organization_id" IS NULL)
OR U1."sender_prescriber_organization_id" = %s)
AND U0."kind" = %s)
ORDER BY RANDOM() ASC)))
ORDER BY "users_user"."first_name" ASC,
"users_user"."last_name" ASC
LIMIT 4
Expand Down

0 comments on commit 91ee249

Please sign in to comment.