mirror of https://gitlab.com/tildes/tildes.git
Browse Source
Extend topic indexes for keyset pagination
Extend topic indexes for keyset pagination
The "keyset"-style pagination that Tildes uses for topic listings uses WHERE and ORDER BY clauses that involve multiple columns to keep a deterministic ordering even when the values in the main sort column are equal. For example, when sorting by number of votes, you're actually ordering by num_votes DESC, topic_id DESC. The previous single-column indexes were a little inefficient for this and couldn't always be used well. This commit extends all of the relevant indexes to composite ones that contain topic_id as well, and drops all of the original ones. This should be more efficient, and should probably be done to indexes on the comments table as well.merge-requests/88/head
Deimos
5 years ago
2 changed files with 96 additions and 15 deletions
-
76tildes/alembic/versions/f4e1ef359307_extend_topic_indexes_for_keyset_.py
-
35tildes/tildes/models/topic/topic.py
@ -0,0 +1,76 @@ |
|||
"""Extend topic indexes for keyset pagination |
|||
|
|||
Revision ID: f4e1ef359307 |
|||
Revises: 4e101aae77cd |
|||
Create Date: 2020-01-15 20:52:23.380355 |
|||
|
|||
""" |
|||
from alembic import op |
|||
import sqlalchemy as sa |
|||
|
|||
|
|||
# revision identifiers, used by Alembic. |
|||
revision = "f4e1ef359307" |
|||
down_revision = "4e101aae77cd" |
|||
branch_labels = None |
|||
depends_on = None |
|||
|
|||
|
|||
def upgrade(): |
|||
op.create_index( |
|||
"ix_topics_created_time_keyset", |
|||
"topics", |
|||
[sa.text("created_time DESC"), sa.text("topic_id DESC")], |
|||
unique=False, |
|||
) |
|||
op.create_index( |
|||
"ix_topics_last_activity_time_keyset", |
|||
"topics", |
|||
[sa.text("last_activity_time DESC"), sa.text("topic_id DESC")], |
|||
unique=False, |
|||
) |
|||
op.create_index( |
|||
"ix_topics_last_interesting_activity_time_keyset", |
|||
"topics", |
|||
[sa.text("last_interesting_activity_time DESC"), sa.text("topic_id DESC")], |
|||
unique=False, |
|||
) |
|||
op.create_index( |
|||
"ix_topics_num_comments_keyset", |
|||
"topics", |
|||
[sa.text("num_comments DESC"), sa.text("topic_id DESC")], |
|||
unique=False, |
|||
) |
|||
op.create_index( |
|||
"ix_topics_num_votes_keyset", |
|||
"topics", |
|||
[sa.text("num_votes DESC"), sa.text("topic_id DESC")], |
|||
unique=False, |
|||
) |
|||
op.drop_index("ix_topics_created_time", table_name="topics") |
|||
op.drop_index("ix_topics_last_activity_time", table_name="topics") |
|||
op.drop_index("ix_topics_last_interesting_activity_time", table_name="topics") |
|||
op.drop_index("ix_topics_num_comments", table_name="topics") |
|||
op.drop_index("ix_topics_num_votes", table_name="topics") |
|||
|
|||
|
|||
def downgrade(): |
|||
op.create_index("ix_topics_num_votes", "topics", ["num_votes"], unique=False) |
|||
op.create_index("ix_topics_num_comments", "topics", ["num_comments"], unique=False) |
|||
op.create_index( |
|||
"ix_topics_last_interesting_activity_time", |
|||
"topics", |
|||
["last_interesting_activity_time"], |
|||
unique=False, |
|||
) |
|||
op.create_index( |
|||
"ix_topics_last_activity_time", "topics", ["last_activity_time"], unique=False |
|||
) |
|||
op.create_index("ix_topics_created_time", "topics", ["created_time"], unique=False) |
|||
op.drop_index("ix_topics_num_votes_keyset", table_name="topics") |
|||
op.drop_index("ix_topics_num_comments_keyset", table_name="topics") |
|||
op.drop_index( |
|||
"ix_topics_last_interesting_activity_time_keyset", table_name="topics" |
|||
) |
|||
op.drop_index("ix_topics_last_activity_time_keyset", table_name="topics") |
|||
op.drop_index("ix_topics_created_time_keyset", table_name="topics") |
Write
Preview
Loading…
Cancel
Save
Reference in new issue