discourse/db
Alan Guo Xiang Tan fe5cd479eb
PERF: Add index on topic_id and created_at to posts table (#22818)
Why this change?

In `PostDestroyer#make_previous_post_the_last_one` and
`Topic.reset_highest`, we have a query that looks something like this:

```
SELECT user_id FROM posts
WHERE topic_id = :topic_id AND
      deleted_at IS NULL AND
      post_type <> 4
      #{post_type}
ORDER BY created_at desc
LIMIT 1
```

However, we currently don't have an index that caters directly to this
query. As a result, we have seen this query performing poorly on large
sites if the PG planner ends up using an index that is suboptimal for
the query.

This commit adds an index to the `posts` table on `topic_id` and then
`created_at`. For the query above, PG will be able to do a backwards
index scan efficiently.
2023-07-27 10:55:10 +08:00
..
fixtures UX: Update badge icons (#20462) 2023-02-28 10:42:05 +08:00
migrate PERF: Add index on topic_id and created_at to posts table (#22818) 2023-07-27 10:55:10 +08:00
post_migrate DEV: Promote historic post_deploy migrations 2023-07-26 10:36:37 +02:00
api_test_seeds.rb DEV: Apply syntax_tree formatting to `db/*` 2023-01-09 11:59:41 +00:00