-- Add denormalized query keys. Ignored when 0011 already created the columns. ALTER TABLE private_message ADD COLUMN receiver_id BIGINT NOT NULL DEFAULT 0 COMMENT '接收者 user_id;系统消息为 0'; ALTER TABLE private_message ADD KEY idx_receiver_id (receiver_id, id); UPDATE private_message pm INNER JOIN p2p_conversations p ON p.conversation_id = pm.conversation_id SET pm.receiver_id = IF(pm.sender_id = p.user_small, p.user_big, IF(pm.sender_id = p.user_big, p.user_small, 0)) WHERE pm.receiver_id = 0 AND pm.sender_id <> 0; ALTER TABLE group_message ADD COLUMN group_id BIGINT NOT NULL DEFAULT 0 COMMENT '群号 groups.id,便于按群查询'; ALTER TABLE group_message ADD KEY idx_group_id (group_id, id); UPDATE group_message gm INNER JOIN `groups` g ON g.conversation_id = gm.conversation_id SET gm.group_id = g.id WHERE gm.group_id = 0;