性能优化
This commit is contained in:
@@ -28,24 +28,42 @@ class MessageController extends BaseController
|
||||
->field('id')
|
||||
->buildSql();
|
||||
|
||||
// 使用 UNION 合并群聊和好友消息,然后统一排序和分页
|
||||
// 优化后的查询:使用MySQL兼容的查询方式
|
||||
$unionQuery = "
|
||||
(SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime, 2 as msgType, wc.nickname, wc.chatroomAvatar as avatar,wc.chatroomId
|
||||
(SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime, 2 as msgType, wc.nickname, wc.chatroomAvatar as avatar, wc.chatroomId
|
||||
FROM s2_wechat_chatroom wc
|
||||
LEFT JOIN s2_wechat_message m ON wc.id = m.wechatChatroomId
|
||||
WHERE wc.accountId = {$accountId} AND m.type = 2 AND wc.isDeleted = 0
|
||||
GROUP BY m.wechatChatroomId)
|
||||
INNER JOIN s2_wechat_message m ON wc.id = m.wechatChatroomId AND m.type = 2
|
||||
INNER JOIN (
|
||||
SELECT wechatChatroomId, MAX(wechatTime) as maxTime, MAX(id) as maxId
|
||||
FROM s2_wechat_message
|
||||
WHERE type = 2
|
||||
GROUP BY wechatChatroomId
|
||||
) latest ON m.wechatChatroomId = latest.wechatChatroomId AND m.wechatTime = latest.maxTime AND m.id = latest.maxId
|
||||
WHERE wc.accountId = {$accountId} AND wc.isDeleted = 0
|
||||
)
|
||||
UNION ALL
|
||||
(SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime, 1 as msgType, 1 as nickname, 1 avatar, 1 as chatroomId
|
||||
(SELECT m.id, m.content, m.wechatFriendId, m.wechatChatroomId, m.createTime, m.wechatTime, 1 as msgType, 1 as nickname, 1 as avatar, 1 as chatroomId
|
||||
FROM s2_wechat_message m
|
||||
INNER JOIN (
|
||||
SELECT wechatFriendId, MAX(wechatTime) as maxTime, MAX(id) as maxId
|
||||
FROM s2_wechat_message
|
||||
WHERE type = 1 AND wechatFriendId IN {$friendSubQuery}
|
||||
GROUP BY wechatFriendId
|
||||
) latest ON m.wechatFriendId = latest.wechatFriendId AND m.wechatTime = latest.maxTime AND m.id = latest.maxId
|
||||
WHERE m.type = 1 AND m.wechatFriendId IN {$friendSubQuery}
|
||||
GROUP BY m.wechatFriendId)
|
||||
ORDER BY createTime DESC
|
||||
)
|
||||
ORDER BY wechatTime DESC
|
||||
LIMIT " . (($page - 1) * $limit) . ", {$limit}
|
||||
";
|
||||
|
||||
$list = Db::query($unionQuery);
|
||||
|
||||
// 对分页后的结果进行排序(按wechatTime降序)
|
||||
usort($list, function ($a, $b) {
|
||||
return $b['wechatTime'] <=> $a['wechatTime'];
|
||||
});
|
||||
|
||||
|
||||
// 批量统计未读数量(isRead=0),按好友/群聊分别聚合
|
||||
$friendIds = [];
|
||||
$chatroomIds = [];
|
||||
@@ -62,6 +80,7 @@ class MessageController extends BaseController
|
||||
|
||||
$friendUnreadMap = [];
|
||||
if (!empty($friendIds)) {
|
||||
// 获取未读消息数量
|
||||
$friendUnreadMap = Db::table('s2_wechat_message')
|
||||
->where(['isRead' => 0])
|
||||
->whereIn('wechatFriendId', $friendIds)
|
||||
@@ -71,6 +90,7 @@ class MessageController extends BaseController
|
||||
|
||||
$chatroomUnreadMap = [];
|
||||
if (!empty($chatroomIds)) {
|
||||
// 获取未读消息数量
|
||||
$chatroomUnreadMap = Db::table('s2_wechat_message')
|
||||
->where(['isRead' => 0])
|
||||
->whereIn('wechatChatroomId', $chatroomIds)
|
||||
@@ -78,12 +98,14 @@ class MessageController extends BaseController
|
||||
->column('COUNT(*) AS cnt', 'wechatChatroomId');
|
||||
}
|
||||
|
||||
|
||||
foreach ($list as $k => &$v) {
|
||||
|
||||
$createTime = !empty($v['createTime']) ? date('Y-m-d H:i:s', $v['createTime']) : '';
|
||||
$wechatTime = !empty($v['wechatTime']) ? date('Y-m-d H:i:s', $v['wechatTime']) : '';
|
||||
|
||||
$vunreadCount = 0;
|
||||
|
||||
$unreadCount = 0;
|
||||
if (!empty($v['wechatFriendId'])) {
|
||||
$v['nickname'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['nickname'] : '';
|
||||
$v['avatar'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['avatar'] : '';
|
||||
@@ -91,7 +113,7 @@ class MessageController extends BaseController
|
||||
$v['groupId'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['groupId'] : '';
|
||||
$v['wechatAccountId'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['wechatAccountId'] : '';
|
||||
$v['wechatId'] = !empty($friends[$v['wechatFriendId']]) ? $friends[$v['wechatFriendId']]['wechatId'] : '';
|
||||
$v['labels'] = !empty($friends[$v['wechatFriendId']]) ? json_decode($friends[$v['wechatFriendId']]['labels'],true) : [];
|
||||
$v['labels'] = !empty($friends[$v['wechatFriendId']]) ? json_decode($friends[$v['wechatFriendId']]['labels'], true) : [];
|
||||
$unreadCount = isset($friendUnreadMap[$v['wechatFriendId']]) ? (int)$friendUnreadMap[$v['wechatFriendId']] : 0;
|
||||
unset($v['chatroomId']);
|
||||
}
|
||||
@@ -110,7 +132,14 @@ class MessageController extends BaseController
|
||||
];
|
||||
$v['createTime'] = $createTime;
|
||||
$v['lastUpdateTime'] = $wechatTime;
|
||||
unset($v['wechatFriendId'],$v['wechatChatroomId']);
|
||||
|
||||
// 最新消息内容已经在UNION查询中获取,直接使用
|
||||
$v['latestMessage'] = [
|
||||
'content' => $v['content'],
|
||||
'wechatTime' => $wechatTime
|
||||
];
|
||||
|
||||
unset($v['wechatFriendId'], $v['wechatChatroomId']);
|
||||
|
||||
}
|
||||
unset($v);
|
||||
|
||||
@@ -24,10 +24,10 @@ class WechatFriendController extends BaseController
|
||||
$list = $query->page($page, $limit)->select();
|
||||
|
||||
|
||||
// 提取所有好友ID
|
||||
/* // 提取所有好友ID
|
||||
$friendIds = array_column($list, 'id');
|
||||
|
||||
// 一次性查询所有好友的未读消息数量
|
||||
// 一次性查询所有好友的未读消息数量
|
||||
$unreadCounts = [];
|
||||
if (!empty($friendIds)) {
|
||||
$unreadResults = Db::table('s2_wechat_message')
|
||||
@@ -44,7 +44,7 @@ class WechatFriendController extends BaseController
|
||||
}
|
||||
|
||||
// 一次性查询所有好友的最新消息
|
||||
$latestMessages = [];
|
||||
$latestMessages = [];
|
||||
if (!empty($friendIds)) {
|
||||
// 使用子查询获取每个好友的最新消息ID
|
||||
$subQuery = Db::table('s2_wechat_message')
|
||||
@@ -67,9 +67,7 @@ class WechatFriendController extends BaseController
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
$aiTypeData = [];
|
||||
@@ -87,14 +85,16 @@ class WechatFriendController extends BaseController
|
||||
$v['passTime'] = !empty($v['passTime']) ? date('Y-m-d H:i:s', $v['passTime']) : '';
|
||||
|
||||
|
||||
$config = [
|
||||
/* $config = [
|
||||
'unreadCount' => isset($unreadCounts[$v['id']]) ? $unreadCounts[$v['id']] : 0,
|
||||
'chat' => isset($latestMessages[$v['id']]),
|
||||
'msgTime' => isset($latestMessages[$v['id']]) ? $latestMessages[$v['id']]['wechatTime'] : 0
|
||||
];
|
||||
|
||||
// 将消息配置添加到好友数据中
|
||||
$v['config'] = $config;
|
||||
$v['config'] = $config;*/
|
||||
|
||||
|
||||
$v['aiType'] = isset($aiTypeData[$v['id']]) ? $aiTypeData[$v['id']] : 0;
|
||||
}
|
||||
unset($v);
|
||||
|
||||
Reference in New Issue
Block a user