//! Trigram search and the indexes hot paths were missing (each checked
//! with EXPLAIN ANALYZE on generated data):
//!
//! * `pg_trgm` GIN indexes on `topics.title` and `posts.raw` (live rows only:
//! search never matches deleted ones) and `tags.name`, so `col ILIKE
//! '%abc%'` (3+ characters) is an index lookup instead of a scan of every
//! post. The search query is written as a UNION of one `ILIKE` per table
//! on the bare column, the shape these can serve.
//! * `topic_reads(topic_id)`: its primary key leads with `user_id`, but the
//! auto-hide's reader count and unread counts go by topic.
//! * `posts(user_id, created_at)`: the lameness duplicate check, flags on a
//! user's posts.
//! * `post_flags(user_id, created_at)`: the queue by flagger, a flagger's
//! record, flags they raised.
//! * `moderation_log(actor_id, created_at)` and `(target_id, created_at)`:
//! `?user=` is `actor_id = $1 OR target_id = $1`, newest first (a
//! BitmapOr of the two).
//! * `topics(category_id, bumped_at)`: a category's topics in bump order.
//! It replaces `idx_topics_category_id`, which is its prefix. Not partial
//! on `deleted_at`, so it still serves the FK cascade when a category is
//! deleted.
//!
//! Plain `CREATE INDEX`, not `CONCURRENTLY`: the migrator runs each
//! migration in a transaction, where `CONCURRENTLY` isn't allowed. At a
//! forum's size the brief write lock at deploy is the lesser cost.
//!
//! Down drops the indexes and restores `idx_topics_category_id`. It leaves
//! the `pg_trgm` extension in place: it may predate this migration or be
//! used by something else, and it's harmless.
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
const UP: &str = r#"
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX idx_topics_title_trgm ON topics USING GIN (title gin_trgm_ops) WHERE deleted_at IS NULL;
CREATE INDEX idx_posts_raw_trgm ON posts USING GIN (raw gin_trgm_ops) WHERE deleted_at IS NULL;
CREATE INDEX idx_tags_name_trgm ON tags USING GIN (name gin_trgm_ops);
CREATE INDEX idx_topic_reads_topic ON topic_reads (topic_id);
CREATE INDEX idx_posts_user_created ON posts (user_id, created_at);
CREATE INDEX idx_post_flags_user_created ON post_flags (user_id, created_at);
CREATE INDEX idx_moderation_log_actor_created ON moderation_log (actor_id, created_at);
CREATE INDEX idx_moderation_log_target_created ON moderation_log (target_id, created_at);
CREATE INDEX idx_topics_category_bumped ON topics (category_id, bumped_at);
DROP INDEX IF EXISTS idx_topics_category_id;
"#;
const DOWN: &str = r#"
CREATE INDEX IF NOT EXISTS idx_topics_category_id ON topics (category_id);
DROP INDEX IF EXISTS idx_topics_category_bumped;
DROP INDEX IF EXISTS idx_moderation_log_target_created;
DROP INDEX IF EXISTS idx_moderation_log_actor_created;
DROP INDEX IF EXISTS idx_post_flags_user_created;
DROP INDEX IF EXISTS idx_posts_user_created;
DROP INDEX IF EXISTS idx_topic_reads_topic;
DROP INDEX IF EXISTS idx_tags_name_trgm;
DROP INDEX IF EXISTS idx_posts_raw_trgm;
DROP INDEX IF EXISTS idx_topics_title_trgm;
"#;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.get_connection().execute_unprepared(UP).await.map(|_| ())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.get_connection().execute_unprepared(DOWN).await.map(|_| ())
}
}
neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
3.4 KiB · text
History
263b40b