我有一个包含 500 万行的消息表,我想检索两个用户对话的最后 10 行
+---------------------------------------------------------------+
| messages |
+---------------------------------------------------------------+
| message_id | id_sender | id_dest | subject | message | time |
+---------------------------------------------------------------+
这是我如何优化它的查询?
SELECT *
FROM
(SELECT message
FROM messages
WHERE ( id_sender = id1 AND id_dest = id2 ) or
( id_dest = id1 AND id_sender = id2 )
ORDER BY message_id DESC
LIMIT 10) AS ttbl
ORDER BY message_id ASC
谢谢!
我在 id_sender id_dest 和 message_1d 上有一个索引
更新