fix(messages): 聊天记录大对话超时——DESC首屏+keyset游标+群聊直达索引
用户报:聊天记录多时「聊天记录加载失败:请求失败 (ECONNABORTED)」(2026-09-07)。 服务器 EXPLAIN ANALYZE 实测两级根因: - 单聊 1 万条:ASC 末页 OFFSET 10486 逐条回表 2515ms(DESC LIMIT 15ms,167 倍); - 群聊 16,290 条:查询刻意不带 owner(chat_group 去重后 owner 错位),现有 (channel,owner,room,timestamp) 索引被 owner 挡住,退化到 room 单列索引+ filter+filesort——count 4.7s/首页 5.1s/末页 10.7s;真凶群 53132846289@chatroom 40,863 条,三段串行 ~40s 必撞前端 axios 15s 超时。 修法(不动共享 MySQL 配置,沿用既往修复模式): 1. 迁移 e6f7a8b9c0d1:message 补 (channel, room_id, timestamp) 索引,群查询 seek+排序直达+覆盖 count;模型侧双声明防 autogenerate 反 DROP。 2. /api/messages 加 order=asc|desc(默认 asc 不变)与 before_ts/before_id keyset 游标(成对,单传 422)——排序列 (timestamp,msg_id) 全序,平手按 msg_id 决胜,游标页恒 O(page_size)。 3. 前端 ChatMain/ChatTimeline 首屏改「order=desc 取最新 50 反转」单请求 (原 page1 拿 total+深 OFFSET 末页两请求),「加载更多」改游标上翻; hasMoreOlder=已加载<总数。SessionPane 搜索等 order=asc 默认路径不受影响。 本地实测(37,532 条群):desc 首页 0.39s、游标上翻 0.24s,浏览器真机 渲染+上翻 150 条无错。测试 8 增(desc/游标/平手边界/422/窗口叠加/ 前端流程不变式),全量 601 绿。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -94,6 +94,14 @@ export interface MessageQuery {
|
||||
end?: number
|
||||
page?: number
|
||||
page_size?: number
|
||||
/** 排序方向:asc(默认,最旧在前)/ desc(最新在前)。聊天窗首屏用 desc
|
||||
* 取最新一页(O(page_size)),避开 ASC 深 OFFSET 逐行回表(大对话 2.5s+,
|
||||
* 2026-09-07 超时修复)。 */
|
||||
order?: 'asc' | 'desc'
|
||||
/** keyset 游标(与 before_id 成对):只取严格早于 (before_ts, before_id) 的
|
||||
* 消息。「加载更多」上翻用,每页恒 O(page_size),与 order 独立组合。 */
|
||||
before_ts?: number
|
||||
before_id?: string
|
||||
}
|
||||
|
||||
export function getMessages(query: MessageQuery): Promise<Page<Message>> {
|
||||
@@ -110,6 +118,9 @@ export function getMessages(query: MessageQuery): Promise<Page<Message>> {
|
||||
end: query.end,
|
||||
page: query.page ?? 1,
|
||||
page_size: query.page_size ?? 50,
|
||||
order: query.order,
|
||||
before_ts: query.before_ts,
|
||||
before_id: query.before_id,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -32,17 +32,17 @@ const loading = ref(false)
|
||||
const loadingOlder = ref(false)
|
||||
const errorMsg = ref<string | null>(null)
|
||||
|
||||
// Asc-ordered pagination: page 1 = oldest 50, last page = most recent 50.
|
||||
// We land on the last page (most recent activity) and walk backwards to load
|
||||
// older history. `currentPage` is the smallest page number currently loaded.
|
||||
const currentPage = ref(1)
|
||||
const totalPages = ref(1)
|
||||
// Desc-keyset 流式加载(2026-09-07 深分页超时修复):首屏 order=desc 取最新
|
||||
// PAGE_SIZE 条反转成 ASC 展示,「加载更多」带 (before_ts, before_id) 游标向更旧
|
||||
// 翻——每页恒 O(PAGE_SIZE)。旧方案「先拉 page1 拿 total 再拉 ASC 末页」在大对话
|
||||
// 上 OFFSET 数万行逐行回表(1 万条 2.5s),群聊再叠加 filesort(4 万条 count 4.7s
|
||||
// /首页 5.1s/末页 10.7s),串行撞 axios 15s 超时 → ECONNABORTED。
|
||||
const total = ref(0)
|
||||
/** 首次加载是否完成(msgid 定位只在已加载数据上尝试,避免误报「不在当前页」)。 */
|
||||
const initialLoaded = ref(false)
|
||||
|
||||
// 请求序号防竞态:快速切换会话时,旧会话的请求仍在途、响应后到,若不拦截会把
|
||||
// 新会话已渲染的 messages/total/currentPage/标题 覆盖回旧会话数据(自愈但错乱)。
|
||||
// 新会话已渲染的 messages/total/标题 覆盖回旧会话数据(自愈但错乱)。
|
||||
// 每次 loadInitial/loadTalkerName 自增取号,响应回来时号不对即整体丢弃。
|
||||
let loadSeq = 0
|
||||
let nameSeq = 0
|
||||
@@ -51,7 +51,8 @@ const isGroup = computed(() => props.kind === 'group')
|
||||
/** 是否已选定具体会话(决定是否拉消息 / 显示空态)。账号级入口(talkerId='')→ 空态。 */
|
||||
const hasActive = computed(() => !!props.talkerId)
|
||||
|
||||
const hasMoreOlder = computed(() => currentPage.value > 1)
|
||||
/** 还有更旧历史未加载:已加载数 < 会话总数(总数以首屏响应为准,游标响应的 total 是剩余数)。 */
|
||||
const hasMoreOlder = computed(() => messages.value.length < total.value)
|
||||
|
||||
/**
|
||||
* 关键词过滤:仅作用于"当前已加载"的消息(纯前端过滤,不触发重载——输入过程中
|
||||
@@ -164,8 +165,8 @@ function scrollToBottom(): void {
|
||||
})
|
||||
}
|
||||
|
||||
/** Shared query params for both DM and group timelines. */
|
||||
function queryFor(pageNum: number) {
|
||||
/** Shared query params for both DM and group timelines (without pagination bits). */
|
||||
function queryFor() {
|
||||
return {
|
||||
channel: CHANNEL,
|
||||
// 群聊不带 owner:chat_group 表按 room_id 去重、owner 被最后写入的员工
|
||||
@@ -176,15 +177,14 @@ function queryFor(pageNum: number) {
|
||||
: { owner_account_id: props.ownerId, talker_id: props.talkerId as string }),
|
||||
...(winStart.value != null && winEnd.value != null ? { start: winStart.value, end: winEnd.value } : {}),
|
||||
...(msgType.value ? { msg_type: msgType.value } : {}),
|
||||
page: pageNum,
|
||||
page_size: PAGE_SIZE,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the last page (most recent messages) on entry. Page 1 is fetched first
|
||||
* purely to learn `total` so we can compute `lastPage` — the messages API
|
||||
* returns asc, so the last page holds the newest slice.
|
||||
* Fetch the newest slice on entry: `order=desc` page 1 returns the most recent
|
||||
* PAGE_SIZE messages; we reverse it to ASC for display. One request replaces
|
||||
* the old two-step dance (page 1 for `total` + deep-offset last page).
|
||||
*/
|
||||
async function loadInitial(): Promise<void> {
|
||||
const seq = ++loadSeq
|
||||
@@ -193,8 +193,6 @@ async function loadInitial(): Promise<void> {
|
||||
if (!hasActive.value) {
|
||||
messages.value = []
|
||||
total.value = 0
|
||||
totalPages.value = 1
|
||||
currentPage.value = 1
|
||||
pendingMsgid.value = null
|
||||
initialLoaded.value = true
|
||||
// 本分支已取最新序号,在途旧请求的 finally 会被拦下,须自行复位加载态
|
||||
@@ -204,19 +202,11 @@ async function loadInitial(): Promise<void> {
|
||||
loading.value = true
|
||||
errorMsg.value = null
|
||||
try {
|
||||
const first = await getMessages(queryFor(1))
|
||||
const res = await getMessages({ ...queryFor(), order: 'desc', page: 1 })
|
||||
if (seq !== loadSeq) return // 竞态:已有更新的加载,丢弃旧会话响应
|
||||
total.value = first.total
|
||||
totalPages.value = Math.max(1, Math.ceil(first.total / PAGE_SIZE))
|
||||
|
||||
if (totalPages.value <= 1) {
|
||||
messages.value = first.items
|
||||
} else {
|
||||
const last = await getMessages(queryFor(totalPages.value))
|
||||
if (seq !== loadSeq) return // 竞态:已有更新的加载,丢弃旧会话响应
|
||||
messages.value = last.items
|
||||
}
|
||||
currentPage.value = totalPages.value
|
||||
total.value = res.total
|
||||
// 后端 desc(新→旧);反转成时间线正序(旧→新)。
|
||||
messages.value = [...res.items].reverse()
|
||||
scrollToBottom()
|
||||
} catch (e) {
|
||||
if (seq !== loadSeq) return // 竞态:旧请求的报错不展示(会话已切换)
|
||||
@@ -233,18 +223,25 @@ async function loadInitial(): Promise<void> {
|
||||
nextTick(() => tryLocate())
|
||||
}
|
||||
|
||||
/** Prepend the next older page (currentPage - 1). No-op on page 1. */
|
||||
/** Prepend the next older slice via keyset cursor. No-op when nothing older. */
|
||||
async function loadOlder(): Promise<void> {
|
||||
if (loadingOlder.value || !hasMoreOlder.value) return
|
||||
const oldest = messages.value[0]
|
||||
// 游标取最早一条已加载消息的 (timestamp, msg_id);timestamp 缺失无法定位
|
||||
// (生产数据 0 行 NULL,防御性早退)。
|
||||
if (!oldest || oldest.timestamp == null) return
|
||||
loadingOlder.value = true
|
||||
const prevHeight = streamRef.value?.scrollHeight ?? 0
|
||||
try {
|
||||
const prevPage = currentPage.value - 1
|
||||
const res = await getMessages(queryFor(prevPage))
|
||||
// Backend returns asc, so res.items are older than everything currently
|
||||
// shown — prepend them.
|
||||
messages.value = [...res.items, ...messages.value]
|
||||
currentPage.value = prevPage
|
||||
const res = await getMessages({
|
||||
...queryFor(),
|
||||
order: 'desc',
|
||||
page: 1,
|
||||
before_ts: oldest.timestamp,
|
||||
before_id: oldest.msg_id,
|
||||
})
|
||||
// 后端 desc(新→旧);反转后正好衔接在现有列表之前。
|
||||
messages.value = [...[...res.items].reverse(), ...messages.value]
|
||||
// Keep the user's scroll anchored near where they were.
|
||||
nextTick(() => {
|
||||
const el = streamRef.value
|
||||
|
||||
@@ -35,15 +35,15 @@ const loading = ref(false)
|
||||
const loadingOlder = ref(false)
|
||||
const errorMsg = ref<string | null>(null)
|
||||
|
||||
// Asc-ordered pagination: page 1 = oldest 50, last page = most recent 50.
|
||||
// We land on the last page (most recent activity) and walk backwards to load
|
||||
// older history. `currentPage` is the smallest page number currently loaded.
|
||||
const currentPage = ref(1)
|
||||
const totalPages = ref(1)
|
||||
// Desc-keyset 流式加载(2026-09-07 深分页超时修复,与 staffchat/ChatMain 同款):
|
||||
// 首屏 order=desc 取最新 PAGE_SIZE 条反转成 ASC 展示,「加载更多」带
|
||||
// (before_ts, before_id) 游标向更旧翻——每页恒 O(PAGE_SIZE)。旧方案
|
||||
// 「先拉 page1 拿 total 再拉 ASC 末页」在大对话上 OFFSET 数万行逐行回表,
|
||||
// 群聊再叠加 filesort,串行撞 axios 15s 超时 → ECONNABORTED。
|
||||
const total = ref(0)
|
||||
|
||||
// 请求序号防竞态:快速切换会话时,旧会话的请求仍在途、响应后到,若不拦截会把
|
||||
// 新会话已渲染的 messages/total/currentPage/标题 覆盖回旧会话数据(自愈但错乱)。
|
||||
// 新会话已渲染的 messages/total/标题 覆盖回旧会话数据(自愈但错乱)。
|
||||
// 每次 loadInitial/loadTalkerName 自增取号,响应回来时号不对即整体丢弃。
|
||||
let loadSeq = 0
|
||||
let nameSeq = 0
|
||||
@@ -62,7 +62,8 @@ const activeId = computed<string | null>(() => props.talkerId ?? props.roomId ??
|
||||
/** 是否已选定具体会话(决定是否拉消息 / 显示空态)。 */
|
||||
const hasActive = computed(() => !!activeId.value)
|
||||
|
||||
const hasMoreOlder = computed(() => currentPage.value > 1)
|
||||
/** 还有更旧历史未加载:已加载数 < 会话总数(总数以首屏响应为准,游标响应的 total 是剩余数)。 */
|
||||
const hasMoreOlder = computed(() => messages.value.length < total.value)
|
||||
|
||||
/**
|
||||
* 关键词过滤:仅作用于"当前已加载"的消息(/api/messages 无 q 参数,非全量历史)。
|
||||
@@ -175,8 +176,8 @@ function scrollToBottom(): void {
|
||||
})
|
||||
}
|
||||
|
||||
/** Shared query params for both DM and group timelines. */
|
||||
function queryFor(pageNum: number) {
|
||||
/** Shared query params for both DM and group timelines (without pagination bits). */
|
||||
function queryFor() {
|
||||
return {
|
||||
channel: channelTyped.value,
|
||||
// 群聊不带 owner:chat_group 表按 room_id 去重、owner 被最后写入的员工
|
||||
@@ -186,15 +187,14 @@ function queryFor(pageNum: number) {
|
||||
? { room_id: props.roomId as string }
|
||||
: { owner_account_id: props.ownerId, talker_id: props.talkerId as string }),
|
||||
...(winStart.value != null && winEnd.value != null ? { start: winStart.value, end: winEnd.value } : {}),
|
||||
page: pageNum,
|
||||
page_size: PAGE_SIZE,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the last page (most recent messages) on entry. Page 1 is fetched first
|
||||
* purely to learn `total` so we can compute `lastPage` — the messages API
|
||||
* returns asc, so the last page holds the newest slice.
|
||||
* Fetch the newest slice on entry: `order=desc` page 1 returns the most recent
|
||||
* PAGE_SIZE messages; we reverse it to ASC for display. One request replaces
|
||||
* the old two-step dance (page 1 for `total` + deep-offset last page).
|
||||
*/
|
||||
async function loadInitial(): Promise<void> {
|
||||
const seq = ++loadSeq
|
||||
@@ -202,8 +202,6 @@ async function loadInitial(): Promise<void> {
|
||||
if (!hasActive.value) {
|
||||
messages.value = []
|
||||
total.value = 0
|
||||
totalPages.value = 1
|
||||
currentPage.value = 1
|
||||
// 本分支已取最新序号,在途旧请求的 finally 会被拦下,须自行复位加载态
|
||||
loading.value = false
|
||||
return
|
||||
@@ -211,19 +209,11 @@ async function loadInitial(): Promise<void> {
|
||||
loading.value = true
|
||||
errorMsg.value = null
|
||||
try {
|
||||
const first = await getMessages(queryFor(1))
|
||||
const res = await getMessages({ ...queryFor(), order: 'desc', page: 1 })
|
||||
if (seq !== loadSeq) return // 竞态:已有更新的加载,丢弃旧会话响应
|
||||
total.value = first.total
|
||||
totalPages.value = Math.max(1, Math.ceil(first.total / PAGE_SIZE))
|
||||
|
||||
if (totalPages.value <= 1) {
|
||||
messages.value = first.items
|
||||
} else {
|
||||
const last = await getMessages(queryFor(totalPages.value))
|
||||
if (seq !== loadSeq) return // 竞态:已有更新的加载,丢弃旧会话响应
|
||||
messages.value = last.items
|
||||
}
|
||||
currentPage.value = totalPages.value
|
||||
total.value = res.total
|
||||
// 后端 desc(新→旧);反转成时间线正序(旧→新)。
|
||||
messages.value = [...res.items].reverse()
|
||||
scrollToBottom()
|
||||
} catch (e) {
|
||||
if (seq !== loadSeq) return // 竞态:旧请求的报错不展示(会话已切换)
|
||||
@@ -235,18 +225,25 @@ async function loadInitial(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Prepend the next older page (currentPage - 1). No-op on page 1. */
|
||||
/** Prepend the next older slice via keyset cursor. No-op when nothing older. */
|
||||
async function loadOlder(): Promise<void> {
|
||||
if (loadingOlder.value || !hasMoreOlder.value) return
|
||||
const oldest = messages.value[0]
|
||||
// 游标取最早一条已加载消息的 (timestamp, msg_id);timestamp 缺失无法定位
|
||||
// (生产数据 0 行 NULL,防御性早退)。
|
||||
if (!oldest || oldest.timestamp == null) return
|
||||
loadingOlder.value = true
|
||||
const prevHeight = streamRef.value?.scrollHeight ?? 0
|
||||
try {
|
||||
const prevPage = currentPage.value - 1
|
||||
const res = await getMessages(queryFor(prevPage))
|
||||
// Backend returns asc, so res.items are older than everything currently
|
||||
// shown — prepend them.
|
||||
messages.value = [...res.items, ...messages.value]
|
||||
currentPage.value = prevPage
|
||||
const res = await getMessages({
|
||||
...queryFor(),
|
||||
order: 'desc',
|
||||
page: 1,
|
||||
before_ts: oldest.timestamp,
|
||||
before_id: oldest.msg_id,
|
||||
})
|
||||
// 后端 desc(新→旧);反转后正好衔接在现有列表之前。
|
||||
messages.value = [...[...res.items].reverse(), ...messages.value]
|
||||
// Keep the user's scroll anchored near where they were.
|
||||
nextTick(() => {
|
||||
const el = streamRef.value
|
||||
|
||||
Reference in New Issue
Block a user