Aias00 opened a new issue, #4974:
URL: https://github.com/apache/rocketmq-dashboard/issues/4974

   ## Problem
   
   `MybatisPlusAiEventRepository.findByConversationIdAfterSeq` filters events 
by `(conversation_id, seq)`, applies `LIMIT`, and only sorts the returned 
subset in Java. SQL without `ORDER BY` is not required to return the lowest 
sequence values, even when a suitable index exists.
   
   For a timeline with more eligible rows than the requested slice, MySQL may 
choose any qualifying subset. Sorting that subset afterwards makes it look 
ordered but cannot recover earlier rows that were excluded by `LIMIT`. 
Advancing `nextAfter` from that subset can then skip timeline events 
permanently.
   
   ## Root cause
   
   The code intentionally avoids SQL ordering because `payload` is 
`MEDIUMTEXT`, assuming the database may materialize it into a sort buffer. 
However the unique index `uk_ai_event_conversation_seq (conversation_id, seq)` 
directly satisfies this query:
   
   - `conversation_id = ?` fixes the leading index column;
   - `seq > ?` is an ascending range on the second column;
   - `ORDER BY seq ASC LIMIT ?` can read the required prefix in index order.
   
   No payload sort is required.
   
   ## Proposed design
   
   1. Add `ORDER BY seq ASC` before `LIMIT` in the repository query.
   2. Remove the in-memory sort, which currently masks the nondeterministic 
subset rather than fixing it.
   3. Update repository/service documentation to state that pagination 
correctness depends on the composite index order.
   4. Change SQL-shape tests to require ordering before the limit and keep 
cursor/probe-row assertions intact.
   
   ## Test plan
   
   - First update the SQL contract test to require `ORDER BY seq ASC LIMIT 
...`; confirm it fails on the current implementation.
   - Assert the repository preserves the mapper's already ordered slice rather 
than applying a second sort.
   - Run `AiTimelineRepositoryTest`, persistence/timeline service tests, the AI 
backend test set, Checkstyle, and package build.
   
   ## Acceptance criteria
   
   - Every timeline page contains the lowest available `seq` values after its 
cursor.
   - `LIMIT` is applied after deterministic index-backed ordering.
   - `nextAfter` cannot advance past an event omitted by an unordered subset.
   - The query remains bounded and uses the existing `(conversation_id, seq)` 
index contract.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to