我使用了这个架构:
-- store messages between users
CREATE TABLE chat_messages (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
sender_id BIGINT UNSIGNED NOT NULL,
recipient_id BIGINT UNSIGNED NOT NULL,
message_body TEXT,
read_at DATETIME DEFAULT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY(id),
CONSTRAINT FK_messages_1 FOREIGN KEY (sender_id) REFERENCES profiles (id),
CONSTRAINT FK_messages_2 FOREIGN KEY (recipient_id) REFERENCES profiles (id)
) ENGINE = INNODB;
我需要针对它运行查询以获取用户的唯一活动聊天列表,我有这样的查询:
SELECT * FROM chat_messages WHERE recipient_id = :user_id GROUP BY recipient_id ORDER BY created_at DESC
然而,返回分组行:
返回的行包含message_body,但它不是最新的消息(根据以前订购的数据集对其进行GROUP it的思考)
此外,如果用户向其他用户发送了消息但尚未收到回复,则不会将其包含在列表中(在这种情况下,也应考虑sender_id)
有任何建议要有一个以性能为中心的查询来实现这一切吗?谢谢:))
答案 0 :(得分:1)
您可以使用not exists
查询执行所需操作:
SELECT cm.*
FROM chat_messages cm
WHERE recipient_id = :user_id AND
NOT EXISTS (SELECT 1
FROM chat_messages cm2
WHERE cm2.recipient_id = cm.recipient_id AND
cm2.created_at > cm.created_at
)
ORDER BY created_at DESC;
为了提高性能,您应该在chat_messages(recipient_id, created_at)
上有一个索引。
答案 1 :(得分:0)
这是一个查询,可以满足您的需求:
SELECT a.sender_id, a.recipient_id, a.message_body
FROM chat_messages a
INNER JOIN (
SELECT IF(recipient_id=1, sender_id, recipient_id) user_id,
MAX(created_at) created_at
FROM chat_messages
WHERE 1 IN (recipient_id, sender_id)
GROUP BY IF(recipient_id=1, sender_id, recipient_id)
) recent
ON
(
(a.sender_id = recent.user_id AND a.recipient_id = 1)
OR
(a.recipient_id = recent.user_id AND a.sender_id = 1)
)
AND a.created_at = recent.created_at
内部联接计算对话伙伴用户ID和最近消息的日期。这与整个表连接,以搜索相应的消息并检索其内容。
如果在(sender_id, recipient_id, created_at)
上创建索引,则可以使用它来支持执行。然而,这个问题并不是那么轻松。
我也准备了SQLFiddle。