Getting it into your agent
One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.
npx agentmods add rules/yudefine/nuxt-supabase-starter/query-optimizationgit clone --depth 1 https://github.com/YuDefine/nuxt-supabase-starterWrote this? Show the measurements
A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.
[](https://agentmods.dev/rules/yudefine/nuxt-supabase-starter/query-optimization)<a href="https://agentmods.dev/rules/yudefine/nuxt-supabase-starter/query-optimization"><img src="https://agentmods.dev/badge/rules/yudefine/nuxt-supabase-starter/query-optimization.svg" alt="Measured on agentmods" height="20"></a>What it costs to keep this loaded
Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.
| Model | Per session | Once invoked |
|---|---|---|
| Fable 5 | $0.00000 | $0.01671 |
| Opus 5 | $0.00000 | $0.00835 |
| Sonnet 5 | $0.00000 | $0.00334 |
| Haiku 4.5 | $0.00000 | $0.00167 |
Grade A, and why
query-optimization scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.
A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.
Nothing flagged
None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.
How it starts
The opening of the file, as written. The whole thing — 141 lines — stays where its author put it; the contents beside it link to each section on GitHub.
Query Optimization
本檔是 clade 投影,NEVER 就地編輯。專案特化寫進自家
.cursor/rules/local/;要改本檔請回 clade 源檔並 propagate。
RLS 會放大任何 N+1 或 full scan。MUST 在以下情境先跑 EXPLAIN ANALYZE 再合併:
- 新增涉及 policy join 的表
- 修改既有 RLS policy 的 WHERE 條件
- 新增 API endpoint 含 pagination / filter
- 遇到
PGRST003(504 timeout)或使用者抱怨變慢 - 部署在 edge runtime 時出現 timeout(Cloudflare Workers 30 秒)
EXPLAIN ANALYZE 模板
-- 1. 模擬目標 role(不是 postgres superuser!)
set local role authenticated;
set local request.jwt.claims to '{"sub": "<user_uuid>", "role": "authenticated"}';
-- 2. 跑實際 query
explain (analyze, buffers, verbose, format text)
select ... from public.<table> where ...;
-- 3. 還原
reset role;
Superuser 跑 EXPLAIN 會 bypass RLS,測出來的 plan 跟 production 完全不同 — 一定要 set local role。
讀 plan 的重點
| Plan 片段 | 意義 | 處理 |
|---|---|---|
Seq Scan on xxx |
全表掃描 | 檢查 WHERE 欄位是否有 index |
Rows Removed by Filter: > 1000 |
Index 取太多又濾掉 | Composite index 或 partial index |
Subquery Scan ... InitPlan |
(SELECT auth.uid()) 有快取 |
✅ 正確模式 |
Filter: (auth.uid() = user_id) |
沒快取 | 改 subselect (SELECT auth.uid()) |
Nested Loop + 高 rows |
Policy 內 per-row JOIN | 改 subselect 預先算 ID set |
Index 設計原則
必須加 index 的欄位
- Policy WHERE/USING 引用的欄位 —
user_id,tenant_id,org_id等(RLS per-row 檢查) - FK 欄位(
xxx_id)— JOIN 效能 + delete cascade - 常用 filter 欄位(
status,created_at,type) - Order by 欄位(用於 pagination)
Index 類型選擇
| 資料特性 | Index 類型 | 範例 |
|---|---|---|
等值查找(= ?) |
B-tree(預設) | create index on x (user_id) |
範圍查找(> ?) |
B-tree | create index on x (created_at) |
| 不常更新的遞增 timestamp | BRIN | create index on x using brin(created_at) |
| JSONB 欄位過濾 | GIN | create index on x using gin(details) |
| 全文搜尋 | GIN (tsvector) |
create index on x using gin(to_tsvector(...)) |
What this file has done since we first saw it
Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.
- yesterday First seen · 141 lines · 0 tokens per session scan A 587f31ca11ca
query-optimization is a cursor rule published in the GitHub repository YuDefine/nuxt-supabase-starter (45 stars, last pushed yesterday), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,671 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.
Other cursor rules, from other repositories
sveltekit-spa
You are an expert SvelteKit engineer. Follow these rules ALWAYS when generating or editing code.
supabase
Supabase development guidelines, commands, and database patterns.
git-commits
Git commit safety — commits are human-only; AI suggests, never executes.
database-patterns
Prisma та БД — buero.de (PostgreSQL).
aws-rds-best-practices
AWS RDS PostgreSQL best practices - database configuration, connection management, authentication, backup, and performance optimization standards.
postgres-sql-style-guide
Guidelines for writing Postgres SQL.