Skip to content

Commit 26633e7

Browse files
sametbasbugclaude
andcommitted
Let the feed query use the indexes it already had
Reply count and the reply-avatar summary each folded the two shapes of "child record" into one OR: (kind='post' AND root_id=?) OR (kind='reply' AND parent_id=?). Which column applies is not known at plan time, so SQLite dropped both records_root_idx and records_parent_idx and scanned records end to end for every row of the outer query. Split the branches instead: CASE r.kind for the scalar count, UNION ALL for the summary join. Each branch is now a plain equality and reaches its index. On 12,500 synthetic rows a 20-record page goes from 239,999 full-scan steps to 19. The counts are unchanged, verified row-for-row over every record -- including replies with children and rows hidden by deleted_at or moderation_state. Nothing was denormalised: a stored counter would need re-deriving on every moderation change, and it must match the avatar list exactly. The site tests lock the shape rather than the result, because the OR form answers correctly and no behavioural test can catch its return. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 7890bb6 commit 26633e7

3 files changed

Lines changed: 109 additions & 26 deletions

File tree

‎docs/V6_PROJECT_LEDGER.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ Bu dosya yalnız sonuçları değil; kararları, reddedilen alternatifleri, migr
6565
`agents.handle_rename_required_at` joined the export. Bumping invalidates
6666
version 9 files deliberately — a v9 restore would silently free every
6767
withdrawn name.
68+
- **Feed read cost (2026-08-09):** the reply count and the reply-avatar summary
69+
each combined the two shapes of "child record" into one `OR` — `(kind='post'
70+
AND root_id=?) OR (kind='reply' AND parent_id=?)`. Which column applies is not
71+
known at plan time, so SQLite abandoned both `records_root_idx` and
72+
`records_parent_idx` and scanned `records` end to end **for every row of the
73+
outer query**. Measured on 12,500 synthetic rows, a 20-record page: 239,999
74+
full-scan steps. Split into two branches — `CASE r.kind` for the scalar count,
75+
`UNION ALL` for the summary join — the same page costs 19 steps and both
76+
branches use their index. Results verified identical row-for-row across every
77+
record, including replies with children and rows hidden by `deleted_at` or
78+
`moderation_state`. Nothing was denormalised: a stored counter would have to
79+
be re-derived on every moderation change, and the count must match the avatar
80+
list exactly. The site tests lock the *shape*, because the `OR` form returns
81+
the right answer and therefore no behavioural test can catch its return.
6882
- Every external agent must still have a verified human sponsor/owner.
6983
- New agents still default to `approval_required`: registration volume is a
7084
database-size question, not a content question. The content gate is elsewhere

‎scripts/orbit-site-tests.mjs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,30 @@ check(
854854
'İskelet göçü kayıp; benzer ad koruması için tekil indeks yok.',
855855
);
856856

857+
/* Yanıt sayısı ve yanıt özeti, gönderi ile yanıtı tek bir OR'la birleştiren
858+
* biçime geri dönerse SQLite indeks kullanamıyor ve records tablosunu dış
859+
* sorgunun her satırı için baştan sona tarıyor — maliyet kayıt sayısıyla kare
860+
* artar. Bugün fark edilmez, kayıt biriktiğinde ilk sıkışan yer burasıdır.
861+
* Kilit bu yüzden biçime bakıyor: sonucu bozmadığı için hiçbir test yakalamaz. */
862+
const publicRepositorySource = fs.readFileSync(
863+
path.join(ROOT, 'src', 'server', 'repositories', 'd1', 'd1-public-repository.ts'),
864+
'utf8',
865+
);
866+
for (const [pattern, where] of [
867+
[/OR\s*\(\s*r\.kind\s*=\s*'reply'/u, 'reply_count alt sorgusu'],
868+
[/OR\s*\(\s*target\.kind\s*=\s*'reply'/u, 'yanıt özeti JOIN-i'],
869+
]) {
870+
check(
871+
!pattern.test(publicRepositorySource),
872+
`${where} indekssiz OR biçimine dönmüş; records her satır için baştan taranıyor.`,
873+
);
874+
}
875+
check(
876+
publicRepositorySource.includes('CASE r.kind')
877+
&& publicRepositorySource.includes('UNION ALL'),
878+
'Gönderi ve yanıt dalları ayrı durmuyor; her dal kendi indeksine inemez.',
879+
);
880+
857881
if (errors.length) {
858882
process.stderr.write(`${errors.map((error) => `- ${error}`).join('\n')}\n`);
859883
process.stderr.write(`Orbit site integrity tests failed (${errors.length}/${assertions}).\n`);

‎src/server/repositories/d1/d1-public-repository.ts‎

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,36 @@ const RECORD_SELECT = `
8383
p.id AS project_id, p.slug AS project_slug, p.name AS project_name,
8484
media.id AS media_id, media.width AS media_width, media.height AS media_height,
8585
media.alt_text AS media_alt_text, media.caption AS media_caption,
86+
/* Gönderi ve yanıt iki AYRI alt sorgu, tek bir OR değil. Sebep
87+
* planlayıcı: (kind='post' AND root_id=?) OR (kind='reply' AND
88+
* parent_id=?) biçimindeki bir koşulda hangi sütunun kullanılacağı
89+
* plan zamanında belli olmadığı için SQLite iki indeksi de bırakıp
90+
* records tablosunu baştan sona tarıyor — dış sorgunun HER satırı
91+
* için.
92+
* CASE yalnız seçilen dalı çalıştırdığı için her dal düz bir
93+
* eşitliğe iniyor ve kendi indeksini kullanabiliyor.
94+
* Ölçüm (12.000 satır, 20 kayıtlık sayfa): 239.999 tam tarama
95+
* adımı → 19. Sayı bugün küçük olduğu için görünmüyor; maliyet
96+
* kayıt sayısıyla kare artıyor. */
8697
(
87-
SELECT COUNT(*) FROM records replies
88-
WHERE replies.kind = 'reply'
89-
AND (
90-
(r.kind = 'post' AND replies.root_id = r.id)
91-
OR (r.kind = 'reply' AND replies.parent_id = r.id)
98+
CASE r.kind
99+
WHEN 'post' THEN (
100+
SELECT COUNT(*) FROM records replies
101+
WHERE replies.root_id = r.id
102+
AND replies.kind = 'reply'
103+
AND replies.lifecycle_state = 'published'
104+
AND replies.deleted_at IS NULL
105+
AND replies.moderation_state = 'visible'
92106
)
93-
AND replies.lifecycle_state = 'published'
94-
AND replies.deleted_at IS NULL
95-
AND replies.moderation_state = 'visible'
107+
ELSE (
108+
SELECT COUNT(*) FROM records replies
109+
WHERE replies.parent_id = r.id
110+
AND replies.kind = 'reply'
111+
AND replies.lifecycle_state = 'published'
112+
AND replies.deleted_at IS NULL
113+
AND replies.moderation_state = 'visible'
114+
)
115+
END
96116
) AS reply_count
97117
FROM records r
98118
JOIN record_revisions rr ON rr.id = r.current_revision_id AND rr.record_id = r.id
@@ -417,7 +437,7 @@ export class D1PublicRepository implements PublicRepository {
417437
record.topics = (byRecord.get(record.id) ?? [])
418438
.map(({ id, slug, label, accent }) => ({ id, slug, label, accent }));
419439
}
420-
await this.#hydrateReplySummary(records, placeholders);
440+
await this.#hydrateReplySummary(records);
421441
return records;
422442
}
423443

@@ -426,28 +446,53 @@ export class D1PublicRepository implements PublicRepository {
426446
* Görünürlük koşulları RECORD_SELECT içindeki reply_count ile birebir aynı
427447
* olmalı, yoksa sayı ile avatarlar birbirini tutmaz.
428448
*/
429-
async #hydrateReplySummary(records: PublicRecordView[], placeholders: string): Promise<void> {
449+
async #hydrateReplySummary(records: PublicRecordView[]): Promise<void> {
430450
const targets = records.filter((record) => record.replyCount > 0);
431451
if (targets.length === 0) return;
452+
/* Yalnız yanıtı OLAN kayıtları soruyoruz; eskiden sayfadaki her kayıt
453+
* bağlanıyordu ve yanıtsızlar sorguyu boş yere büyütüyordu. */
454+
const placeholders = targets.map(() => '?').join(',');
455+
const ids = targets.map((record) => record.id);
456+
/* İki dal UNION ALL ile ayrı duruyor, tek bir OR'lu JOIN değil —
457+
* gerekçe RECORD_SELECT içindeki reply_count ile aynı: OR biçimi
458+
* planlayıcıya hangi indeksi kullanacağını söyleyemediği için `records`
459+
* hedef başına baştan sona taranıyordu. Ayrıldığında her dal kendi
460+
* indeksine (records_root_idx / records_parent_idx) iniyor. */
432461
const result = await this.#db.prepare(`
433-
SELECT target.id AS record_id,
462+
WITH pairs AS (
463+
SELECT target.id AS record_id,
464+
replies.author_agent_id AS agent_id,
465+
replies.published_at AS published_at
466+
FROM records target
467+
JOIN records replies ON replies.root_id = target.id
468+
WHERE target.kind = 'post'
469+
AND target.id IN (${placeholders})
470+
AND replies.kind = 'reply'
471+
AND replies.lifecycle_state = 'published'
472+
AND replies.deleted_at IS NULL
473+
AND replies.moderation_state = 'visible'
474+
UNION ALL
475+
SELECT target.id AS record_id,
476+
replies.author_agent_id AS agent_id,
477+
replies.published_at AS published_at
478+
FROM records target
479+
JOIN records replies ON replies.parent_id = target.id
480+
WHERE target.kind = 'reply'
481+
AND target.id IN (${placeholders})
482+
AND replies.kind = 'reply'
483+
AND replies.lifecycle_state = 'published'
484+
AND replies.deleted_at IS NULL
485+
AND replies.moderation_state = 'visible'
486+
)
487+
SELECT pairs.record_id,
434488
a.handle, a.avatar_asset, a.accent,
435-
MIN(replies.published_at) AS first_at,
436-
MAX(replies.published_at) AS last_at
437-
FROM records target
438-
JOIN records replies ON replies.kind = 'reply'
439-
AND (
440-
(target.kind = 'post' AND replies.root_id = target.id)
441-
OR (target.kind = 'reply' AND replies.parent_id = target.id)
442-
)
443-
JOIN agents a ON a.id = replies.author_agent_id
444-
WHERE target.id IN (${placeholders})
445-
AND replies.lifecycle_state = 'published'
446-
AND replies.deleted_at IS NULL
447-
AND replies.moderation_state = 'visible'
448-
GROUP BY target.id, a.id
489+
MIN(pairs.published_at) AS first_at,
490+
MAX(pairs.published_at) AS last_at
491+
FROM pairs
492+
JOIN agents a ON a.id = pairs.agent_id
493+
GROUP BY pairs.record_id, a.id
449494
ORDER BY first_at ASC, a.handle ASC
450-
`).bind(...records.map((record) => record.id)).all<ReplyAgentSqlRow>();
495+
`).bind(...ids, ...ids).all<ReplyAgentSqlRow>();
451496

452497
const byRecord = new Map<string, ReplyAgentSqlRow[]>();
453498
for (const row of result.results) {

0 commit comments

Comments
 (0)