Skip to content

Commit 860d918

Browse files
committed
Forum, Topic: Make slug unique (which creates an index)
Sentry reported a slow DB query for `Topic.slug`. At the moment a full table scan is performed. To fasten things up, the database field is marked as unique. As a side effect, this will create an index to speed up lookups. With this commit, on sqlite the explain changes from ``` SCAN forum_topic ``` to ``` SEARCH forum_topic USING INDEX sqlite_autoindex_forum_topic_1 (slug=?) ``` Even if it was not enforced in the database, the slug is unique across all topics on forum.ubuntuusers.de.
1 parent 498ac20 commit 860d918

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

ChangeLog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Deployment notes
4545
* Fix deprecation warnings related to UTC methods
4646
* Remove private messages after specific duration. This will not affect messages in the 'archive' folder and team members.
4747
* Event: Slug is changed for unpublished events
48+
* Forum, Topic: Make slug unique which creates an index and fasten look ups
4849

4950
🗑 Deprecations
5051
--------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.18 on 2025-02-14 19:31
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("forum", "0017_alter_poll_start_time_alter_post_pub_date_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="topic",
15+
name="slug",
16+
field=models.CharField(blank=True, max_length=50, unique=True),
17+
),
18+
]

inyoka/forum/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ class Topic(models.Model):
569569
objects = TopicManager()
570570

571571
title = models.CharField(max_length=TITLE_MAX_LENGTH, blank=True)
572-
slug = models.CharField(max_length=50, blank=True)
572+
slug = models.CharField(max_length=50, blank=True, unique=True)
573573
view_count = models.IntegerField(default=0)
574574
sticky = models.BooleanField(default=False, db_index=True)
575575
solved = models.BooleanField(default=False)

0 commit comments

Comments
 (0)