Сделать (chat_id, id) ключом сообщений и добавить сценарий EXPLAIN истории

This commit is contained in:
2026-10-06 15:25:26 +03:00
parent 7f599cb25e
commit 8f9252a5c1
2 changed files with 82 additions and 5 deletions
+7 -5
View File
@@ -22,17 +22,19 @@ CREATE TABLE chat_members (
-- Lists the chats of one user in (created_at, id) order: chat ids are UUIDv7.
CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id);
-- The key is (chat_id, id) and serves the history query directly:
-- WHERE chat_id = $1 AND id > $2 ORDER BY id LIMIT $3. Nothing looks a
-- message up by id alone, and a separate index on id would only cost writes
-- and tempt the planner into scanning every chat's messages in id order.
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats (id),
id uuid NOT NULL,
sender_id uuid NOT NULL REFERENCES users (id),
text text NOT NULL,
created_at timestamptz NOT NULL
created_at timestamptz NOT NULL,
PRIMARY KEY (chat_id, id)
);
-- Keyset pagination of one chat's history: WHERE chat_id = $1 AND id > $2 ORDER BY id.
CREATE INDEX messages_chat_id_idx ON messages (chat_id, id);
-- +goose Down
DROP TABLE messages;
DROP TABLE chat_members;