UPDATE:补充说,运行查询第二最:从这个查询的MySQL索引中受益?
(考虑采取索引的时候,也许需要???)
SELECT m.time, m.message, m.receiver_uid AS receiver, m.sender_uid AS sender
FROM messages AS m, users AS u
WHERE u.uid = '$coID'
AND ((m.receiver_uid = '$meID' AND m.sender_uid = '$coID') OR
(m.receiver_uid = '$coID' AND m.sender_uid = '$meID'))
ORDER BY m.time DESC
- $ MEID是用户的ID,谁运行wuery,
- $ coID是联系人的ID。
我有一个大一些查询,并将其每次运行一个用户访问我的网页。
SELECT m2.message, m2.time, m2.sender_uid AS sender, m2.receiver_uid AS receiver,
m.contact, u.ufirstname
FROM ( SELECT CASE
WHEN sender_uid = '$me' THEN receiver_uid
ELSE sender_uid
END AS contact,
MAX(time) AS maxtime
FROM messages
WHERE sender_uid = '$me' OR receiver_uid = '$me'
GROUP BY CASE
WHEN sender_uid = '$me' THEN receiver_uid
ELSE sender_uid
END ) AS m
INNER JOIN messages m2 ON m.maxtime = m2.time
AND ((m2.sender_uid = '$me' AND m2.receiver_uid = m.Contact)
OR (m2.receiver_uid = '$me' AND m2.sender_uid = m.Contact))
INNER JOIN users AS u ON m.contact = u.uid
ORDER BY time DESC
- $我是谁运行查询的用户的ID
这个查询(成功)检索:
LAST MESSAGE from EVERY 'CONVERSATION' ordered by TIME.
所以它会得到的最后消息(在每个PM会话中是否发送或接收消息) 然后按时间排序并检索联系人信息。
请告诉我,如果我没有正确解释它。
我的MySQL表看起来是这样的:从什么指数(ES)将这个查询利益
receiver_id | sender_id | message | time
?
(用户表已有的ID的主键,以便在加入部分检索联系人的名称应该是有效的)
EXPLAIN输出:
大查询:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
1 PRIMARY m2 ALL NULL NULL NULL NULL 42 Using where
1 PRIMARY u eq_ref PRIMARY PRIMARY 4 m.contact 1 Using where
2 DERIVED messages ALL NULL NULL NULL NULL 42 Using where; Using temporary; Using filesort
在更新部分查询:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE u const PRIMARY PRIMARY 4 const 1 Using index; Using filesort
1 SIMPLE m ALL NULL NULL NULL NULL 42 Using where
如何将旧的(14天)消息移动到较旧的表中? 所以,只有当用户真的想阅读旧信息时,他必须在长表中搜索。 – SuperSpy 2011-03-08 11:25:44