Commit 8553bf0
fix: multi-table DELETE+LIMIT syntax error and inaccurate index comment in cleanup_orphan_stats (#115)
Two issues in the batched `cleanup_orphan_stats` rewrite and one
misleading comment introduced by the previous perf commit.
## Bug fix: multi-table DELETE with LIMIT is invalid MySQL/MatrixOne
syntax
`DELETE alias FROM t1 LEFT JOIN t2 … LIMIT n` is rejected at runtime —
MySQL forbids `ORDER BY`/`LIMIT` on multi-table deletes. Every other
batched `DELETE` in the file is single-table for exactly this reason.
Replaced with the standard two-step pattern used elsewhere:
```sql
-- Step 1: SELECT up to 1000 orphan IDs (LIMIT valid on SELECT)
SELECT s.memory_id
FROM mem_memories_stats s
LEFT JOIN mem_memories m ON s.memory_id = m.memory_id
WHERE m.memory_id IS NULL
LIMIT 1000;
-- Step 2: single-table DELETE by PK — no LIMIT restriction
DELETE FROM mem_memories_stats WHERE memory_id IN (?, …);
```
`mem_memories_stats` has `memory_id` as its `PRIMARY KEY` (one row per
memory), so the SELECT never returns duplicates and the loop terminates
correctly.
## Comment fix: `idx_memories_user_observed` scope
The original comment claimed the index also accelerates
`archive_stale_working`. It doesn't — that function uses
`TIMESTAMPDIFF(HOUR, observed_at, NOW()) > ?`, which wraps `observed_at`
in a function and prevents a B-tree range scan. The index only helps
`health_capacity()`'s direct range predicate `observed_at >= NOW() -
INTERVAL 30 DAY`. Comment corrected accordingly.
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/matrixorigin/Memoria/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: XuPeng-SH <39627130+XuPeng-SH@users.noreply.github.com>1 parent fd92c54 commit 8553bf0
1 file changed
Lines changed: 92 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
784 | 784 | | |
785 | 785 | | |
786 | 786 | | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
787 | 832 | | |
788 | 833 | | |
789 | 834 | | |
| |||
1376 | 1421 | | |
1377 | 1422 | | |
1378 | 1423 | | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
1379 | 1427 | | |
1380 | 1428 | | |
1381 | 1429 | | |
1382 | 1430 | | |
1383 | 1431 | | |
1384 | | - | |
| 1432 | + | |
| 1433 | + | |
1385 | 1434 | | |
1386 | 1435 | | |
1387 | 1436 | | |
| |||
1681 | 1730 | | |
1682 | 1731 | | |
1683 | 1732 | | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
| 1736 | + | |
1684 | 1737 | | |
1685 | | - | |
1686 | | - | |
1687 | | - | |
1688 | | - | |
1689 | | - | |
1690 | | - | |
1691 | | - | |
1692 | | - | |
1693 | | - | |
| 1738 | + | |
| 1739 | + | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
| 1771 | + | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
1694 | 1776 | | |
1695 | 1777 | | |
1696 | 1778 | | |
| |||
0 commit comments