Skip to content

Commit

Permalink
fix: slow SQL query in post views
Browse files Browse the repository at this point in the history
  • Loading branch information
vas3k committed Jan 17, 2025
1 parent c2636da commit 9e15544
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion etc/crontab
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
30 */4 * * 1-6 root cd /app && python3 manage.py send_best_comments >/proc/1/fd/1 2>/proc/1/fd/2
0 10 * * 1 root cd /app && python3 manage.py send_weekly_digest --production true >/proc/1/fd/1 2>/proc/1/fd/2
0 12 * * * root cd /app && python3 manage.py send_subscription_expired --production true >/proc/1/fd/1 2>/proc/1/fd/2
0 2 * * 6 root cd /app && python3 manage.py cleanup_post_views >/proc/1/fd/1 2>/proc/1/fd/2
0 2 * * 7 root cd /app && python3 manage.py rebuild_search_index >/proc/1/fd/1 2>/proc/1/fd/2
0 8 * * * root cd /app && python3 manage.py replay_stuck_reviews >/proc/1/fd/1 2>/proc/1/fd/2
0 8 * * * root find /app/gdpr/downloads/ -mindepth 1 -mtime +3 -type f -delete >/proc/1/fd/1 2>/proc/1/fd/2
0 4 * * * root find /app/gdpr/downloads/ -mindepth 1 -mtime +3 -type f -delete >/proc/1/fd/1 2>/proc/1/fd/2
13 * * * * root cd /app && python3 manage.py update_hotness >/proc/1/fd/1 2>/proc/1/fd/2
0 7 * * 3,6 root cd /app && python3 manage.py promote_one_old_post_on_main >/proc/1/fd/1 2>/proc/1/fd/2
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{% load posts %}🤘 Клубный журнал. Итоги недели. Выпуск #{{ issue_number }}
{% load posts %}🤘 <b><a href="{{ settings.APP_HOST }}{% url "show_post" post.type post.slug %}">Клубный журнал. Итоги недели. Выпуск #{{ issue_number }}{% if digest_title %}: {{ digest_title }}{% endif %}</a></b>

{% if digest_title %}<b><a href="{{ settings.APP_HOST }}{% url "show_post" post.type post.slug %}">{{ digest_title }}</a></b>{% endif %}

{% if digest_intro %}{% render_tg digest_intro 3500 %}{% endif %}

👉 {{ settings.APP_HOST }}{% url "show_post" post.type post.slug %}
{% if digest_intro %}{% render_tg digest_intro 3500 %}
{% endif %}
👉 <b>Читать полностью: {{ settings.APP_HOST }}{% url "show_post" post.type post.slug %}</b>
17 changes: 17 additions & 0 deletions posts/migrations/0028_postview_post_views_post_id_49dd7e_idx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.13 on 2025-01-17 10:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('posts', '0027_auto_20230731_1014'),
]

operations = [
migrations.AddIndex(
model_name='postview',
index=models.Index(fields=['post', 'ipaddress'], name='post_views_post_id_49dd7e_idx'),
),
]
4 changes: 4 additions & 0 deletions posts/models/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class PostView(models.Model):
class Meta:
db_table = "post_views"
unique_together = [["user", "post"]]
indexes = [
models.Index(fields=["post", "ipaddress"])
]

@classmethod
def register_view(cls, request, user, post):
Expand Down Expand Up @@ -60,6 +63,7 @@ def register_anonymous_view(cls, request, post):
ipaddress=parse_ip_address(request),
).first()

# use this instead get_or_create because multiple objects can be returned
if not post_view:
post_view = PostView.objects.create(
post=post,
Expand Down

0 comments on commit 9e15544

Please sign in to comment.