Skip to content

Commit

Permalink
Merge pull request #3348 from Minnozz/more-indexes
Browse files Browse the repository at this point in the history
Define more indexes for slow queries
  • Loading branch information
mouse-reeve committed Apr 4, 2024
2 parents 5082806 + e1c54b2 commit ca6dbcb
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 9 deletions.
14 changes: 5 additions & 9 deletions bookwyrm/activitystreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ def _get_audience(self, status): # pylint: disable=no-self-use
| (
Q(following=status.user) & Q(following=status.reply_parent.user)
) # if the user is following both authors
).distinct()
)

# only visible to the poster's followers and tagged users
elif status.privacy == "followers":
audience = audience.filter(
Q(following=status.user) # if the user is following the author
)
return audience.distinct()
return audience.distinct("id")

@tracer.start_as_current_span("ActivityStream.get_audience")
def get_audience(self, status):
Expand All @@ -156,7 +156,7 @@ def get_audience(self, status):
status_author = models.User.objects.filter(
is_active=True, local=True, id=status.user.id
).values_list("id", flat=True)
return list(set(list(audience) + list(status_author)))
return list(set(audience) | set(status_author))

def get_stores_for_users(self, user_ids):
"""convert a list of user ids into redis store ids"""
Expand All @@ -183,15 +183,13 @@ class HomeStream(ActivityStream):
def get_audience(self, status):
trace.get_current_span().set_attribute("stream_id", self.key)
audience = super()._get_audience(status)
if not audience:
return []
# if the user is following the author
audience = audience.filter(following=status.user).values_list("id", flat=True)
# if the user is the post's author
status_author = models.User.objects.filter(
is_active=True, local=True, id=status.user.id
).values_list("id", flat=True)
return list(set(list(audience) + list(status_author)))
return list(set(audience) | set(status_author))

def get_statuses_for_user(self, user):
return models.Status.privacy_filter(
Expand Down Expand Up @@ -239,9 +237,7 @@ def _get_audience(self, status):
)

audience = super()._get_audience(status)
if not audience:
return models.User.objects.none()
return audience.filter(shelfbook__book__parent_work=work).distinct()
return audience.filter(shelfbook__book__parent_work=work)

def get_audience(self, status):
# only show public statuses on the books feed,
Expand Down
19 changes: 19 additions & 0 deletions bookwyrm/migrations/0200_status_bookwyrm_st_thread__cf064f_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.25 on 2024-04-03 19:05

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0199_status_bookwyrm_st_remote__06aeba_idx"),
]

operations = [
migrations.AddIndex(
model_name="status",
index=models.Index(
fields=["thread_id"], name="bookwyrm_st_thread__cf064f_idx"
),
),
]
19 changes: 19 additions & 0 deletions bookwyrm/migrations/0201_keypair_bookwyrm_ke_remote__472927_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.25 on 2024-04-03 19:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0200_status_bookwyrm_st_thread__cf064f_idx"),
]

operations = [
migrations.AddIndex(
model_name="keypair",
index=models.Index(
fields=["remote_id"], name="bookwyrm_ke_remote__472927_idx"
),
),
]
19 changes: 19 additions & 0 deletions bookwyrm/migrations/0202_user_bookwyrm_us_usernam_b2546d_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.25 on 2024-04-03 19:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0201_keypair_bookwyrm_ke_remote__472927_idx"),
]

operations = [
migrations.AddIndex(
model_name="user",
index=models.Index(
fields=["username"], name="bookwyrm_us_usernam_b2546d_idx"
),
),
]
19 changes: 19 additions & 0 deletions bookwyrm/migrations/0203_user_bookwyrm_us_is_acti_972dc4_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.25 on 2024-04-03 19:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("bookwyrm", "0202_user_bookwyrm_us_usernam_b2546d_idx"),
]

operations = [
migrations.AddIndex(
model_name="user",
index=models.Index(
fields=["is_active", "local"], name="bookwyrm_us_is_acti_972dc4_idx"
),
),
]
1 change: 1 addition & 0 deletions bookwyrm/models/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Meta:
ordering = ("-published_date",)
indexes = [
models.Index(fields=["remote_id"]),
models.Index(fields=["thread_id"]),
]

def save(self, *args, **kwargs):
Expand Down
15 changes: 15 additions & 0 deletions bookwyrm/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ class User(OrderedCollectionPageMixin, AbstractUser):
hotp_secret = models.CharField(max_length=32, default=None, blank=True, null=True)
hotp_count = models.IntegerField(default=0, blank=True, null=True)

class Meta(AbstractUser.Meta):
"""indexes"""

indexes = [
models.Index(fields=["username"]),
models.Index(fields=["is_active", "local"]),
]

@property
def active_follower_requests(self):
"""Follow requests from active users"""
Expand Down Expand Up @@ -509,6 +517,13 @@ class KeyPair(ActivitypubMixin, BookWyrmModel):
activity_serializer = activitypub.PublicKey
serialize_reverse_fields = [("owner", "owner", "id")]

class Meta:
"""indexes"""

indexes = [
models.Index(fields=["remote_id"]),
]

def get_remote_id(self):
# self.owner is set by the OneToOneField on User
return f"{self.owner.remote_id}/#main-key"
Expand Down

0 comments on commit ca6dbcb

Please sign in to comment.