Skip to content

Commit 098fc34

Browse files
authored
Deprecation: improve Celery task db query (#10414)
* Deprecation: improve Celery task db query This commit excludes the users that already have an unread notification with this message. This is only to avoid performing db queries for users where our notification backend won't add a new notification anyways. This will improve the performance of this task making it to run faster (hopefully). * Deprecation: improve slow db query Instead of passing 82k slugs on each query to get the user's projects, we perform a set() intersection in Python which is fast. This reduces the time for this query in a pretty noticeable way. * Deprecation: adapt comment * Minor fix * Remove `.exclude` of unread notifications This will interfere with the logic for sending email notifications. We do want to send an email even if the user didn't read the onsite notification.
1 parent ac72647 commit 098fc34

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

readthedocs/projects/tasks/utils.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def deprecated_config_file_used_notification():
271271
queryset = User.objects.filter(username__in=users, profile__banned=False).order_by(
272272
"id"
273273
)
274+
274275
n_users = queryset.count()
275276
for i, user in enumerate(queryset.iterator()):
276277
if i % 500 == 0:
@@ -283,14 +284,15 @@ def deprecated_config_file_used_notification():
283284
)
284285

285286
# All the projects for this user that don't have a configuration file
286-
user_projects = (
287-
AdminPermission.projects(user, admin=True)
288-
.filter(slug__in=projects)
289-
.only("slug")
287+
# Use set() intersection in Python that's pretty quick since we only need the slugs.
288+
# Otherwise we have to pass 82k slugs to the DB query, which makes it pretty slow.
289+
user_projects = AdminPermission.projects(user, admin=True).values_list(
290+
"slug", flat=True
290291
)
292+
user_projects = list(set(user_projects) & projects)
291293

292-
user_project_slugs = ", ".join([p.slug for p in user_projects[:5]])
293-
if user_projects.count() > 5:
294+
user_project_slugs = ", ".join(user_projects[:5])
295+
if len(user_projects) > 5:
294296
user_project_slugs += " and others..."
295297

296298
n_site = DeprecatedConfigFileSiteNotification(

0 commit comments

Comments
 (0)