Files
hfb_sys/backend/migrations/000107_unread_query_indexes.sql
T

26 lines
984 B
SQL

-- +goose Up
-- 未读统计按会话和已读时间取消息,覆盖索引可避免逐会话回表。
ALTER TABLE chat_messages
ADD KEY idx_chat_messages_conversation_created_sender (conversation_id, created_at, sender_type, sender_id);
ALTER TABLE chat_participants
ADD KEY idx_chat_participants_unread (participant_type, participant_id, conversation_id, last_read_at);
-- 通知未读需要同时过滤站点并计数,将站点字段纳入覆盖索引。
ALTER TABLE notifications
DROP KEY idx_notifications_tenant_user_read,
ADD KEY idx_notifications_tenant_user_unread_site (tenant_id, user_id, read_at, distribution_site_id, id);
-- +goose Down
ALTER TABLE notifications
DROP KEY idx_notifications_tenant_user_unread_site,
ADD KEY idx_notifications_tenant_user_read (tenant_id, user_id, read_at, id);
ALTER TABLE chat_participants
DROP KEY idx_chat_participants_unread;
ALTER TABLE chat_messages
DROP KEY idx_chat_messages_conversation_created_sender;