{"id":177776,"plugin_id":"plugin_asdk_app_6956e6ff740481919946ceae8e5d6304","kind":"skill","collection_source":"plugin_package","comparison_source":null,"observed_at":"2026-12-05T12:13:02.559Z","digest":"880931d92a4036ebf9ced871cb38fcf131bf577fc425496391bc4f0224a68353","against":null,"payload":{"description":"Find and fix performance problems in an AppDeploy app: full-table scans, list reads that miss rows, N+1 queries, unbatched writes, polling, frequent crons and data that grows without bound. Use when the user asks to speed up, scale, review or cut the cost of an app, or an app gets slower or hits database quotas as data grows.","included_files":[],"name":"optimize","skill_md_contents":"---\n{\n  \"name\": \"optimize\",\n  \"description\": \"Find and fix performance problems in an AppDeploy app: full-table scans, list reads that miss rows, N+1 queries, unbatched writes, polling, frequent crons and data that grows without bound. Use when the user asks to speed up, scale, review or cut the cost of an app, or an app gets slower or hits database quotas as data grows.\"\n}\n---\n\n# Optimize an AppDeploy app\n\n## SDK facts behind every fix\n\n- `db.list(table, { filter })` reads rows, then filters: a filtered list still scans the table.\n- `db.list` returns one page (`items`, `nextToken`). Searching only that page misses later rows.\n- There is no secondary index. A record you look up by a value must be keyed by it, directly or through a small lookup record.\n- `db.get/add/update/delete` take arrays of up to 500 items, and every `db.*` call is limited to 1 MiB of input. `db.transact` takes up to 100 actions. `storage.read/write/url/delete` take arrays too.\n- `db.get` is eventually consistent. Pass `{ consistent: true }` when the read decides a write. Uniqueness comes from a chosen-id add, which collides, not from a read that found nothing.\n- Records are limited to 256 KiB. Files belong in `storage`.\n\nCheck details with `get_appdeploy_sdk_reference` (database, storage, realtime, cron) before you write code.\n\n## Workflow\n\n1. **Read the code.** Use local files if you have them; otherwise `get_apps`, then `src_glob`, `src_grep` and `src_read` on `backend/`, `src/` and `cron.json`.\n2. **Find the hot paths:** auth middleware, dashboard GET routes, crons every few minutes, realtime handlers, polled endpoints, and tables that only grow (logs, visits, messages).\n3. **Search for each issue below.** A match is only a candidate: read the code around it. A bounded retry loop is not N+1, and a call in a per-item `onClick` runs once per click.\n4. **Rank by frequency × growth:** wrong results first (C2), then scans and N+1 on hot paths, then growing tables (C14), then batching (C4-C6, C9, C10), then the rest. Show the user the ranked list (file:line, id, fix) before large changes.\n5. **Fix without losing data.** When records get a new key: write the new shape next to the old one in the same `db.transact`, read new-first with the old scan as fallback, backfill one page per run with a saved `nextToken`, then drop the fallback. Keep existing `if:` guards and response shapes.\n6. **Deploy and verify** with `deploy_app` and `get_app_status`, then search again to confirm the patterns are gone. On the next `deploy_app` call after your fixes, set `intent` to exactly `optimized by optimize-skill`.\n\n## Issues\n\n- **C1 Full-table scan for a lookup.** `db.list` then `.find/.filter`, `db.list(t, { filter })`, or a helper that pages to the end. Fix: key the record by the lookup value, or add a lookup record in the same `db.transact`; use per-parent tables (`orders_${id}`, with a table-safe id) for one-to-many; keep counters with `increment()` instead of counting on read.\n- **C2 Truncated scan treated as complete.** `db.list(t, { limit })` searched without following `nextToken`. Fix as C1; until then, follow `nextToken` until the match is found.\n- **C3 Scan inside a loop.** Fix: read once before the loop and index it in a `Map`.\n- **C4 N+1 reads.** `db.get(t, [id])` per item. Fix: one `db.get(t, ids)`, chunked at 500.\n- **C5 Unbatched writes.** `db.add/update/delete` with one item per loop turn. Fix: one call with the array, chunked at 500 items and 1 MiB, or one `db.transact` (100 actions).\n- **C6 Unbatched storage.** `storage.url/read/write/delete` with a one-item array per loop turn. Fix: pass all items in one call.\n- **C7 Read-modify-write without a guard.** `db.get` then a full `db.update`. Fix: `db.tx.update(t, [{ id, record, if }], { patch: true })` or `increment()`.\n- **C8 Orphans.** A parent deleted without its child rows or storage files. Fix: delete them in the same `db.transact` and `storage.delete` the files.\n- **C9 External calls in a loop.** `fetch`, `ai.*`, `email.*` or `notifications.send` per item. Fix: send `notifications.send` once with all `userIds`; call external APIs a few at a time with a timeout; cache `ai.*` results by input hash.\n- **C10 Client N+1.** An API call per item, or paging everything before render. Fix: a bulk endpoint that takes `ids`; render the first page.\n- **C11 Fast polling.** `setInterval` of 15 s or less that calls the backend. Fix: push with `ws.send` (realtime); otherwise pause while `document.hidden` and back off to 60 s.\n- **C12 Frequent crons.** `*/5` or faster crons that scan. Fix: do the work when the data is written, or keep a due-index table; remove leftover test crons.\n- **C13 Files sent as base64 JSON.** `FileReader`/`toDataURL` into a JSON body, then `storage.write`. Fix: resize in the browser first, store the file once and keep only its path in the record, serve it with `storage.url`; never put base64 in a record.\n- **C14 Growing table that is also scanned.** Logs or visits that are only added to and then listed. Fix: split by time or parent (`visits_2026_12`), keep totals with `increment()`, delete old rows in a cron.\n- **C15 Growing array in one record.** `{ items: [...old.items, x] }`. Fix: one row per item in a per-parent table.\n- **C16 Large data in the backend bundle.** Backend files of hundreds of KiB of data. Fix: move it to `storage` or a table.\n\n## Report\n\nList what you fixed (id, file:line, before and after), what you left and why, and any backfill still running.\n"},"changes":[],"summary":"First saved snapshot. No earlier version is available for comparison.","summary_kind":"deterministic","summary_metadata":{}}