query-optimization

query-optimization is a cursor rule for Cursor from YuDefine/nuxt-supabase-starter. It costs 0 tokens per session (1,671 once invoked), scanned A, original, MIT.

A database performance rule that requires measuring PostgreSQL queries with EXPLAIN ANALYZE under the same user permissions and row-level security (RLS) conditions used in production.

In plain words
What is it for?
It is for investigating slow endpoints, pagination and filtering, RLS policy changes, timeouts, and index design.
Why use it?
It prevents misleading performance results from administrator-only tests and helps identify full-table scans, inefficient indexes, and slow policy joins.

Cursor rule for Cursor

Install

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.

agentmods
npx agentmods add rules/yudefine/nuxt-supabase-starter/query-optimization
Clone the repo
git clone --depth 1 https://github.com/YuDefine/nuxt-supabase-starter

Made for: Cursor.

Wrote 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.

agentmods badge for query-optimization

README.md
[![agentmods](https://agentmods.dev/badge/rules/yudefine/nuxt-supabase-starter/query-optimization.svg)](https://agentmods.dev/rules/yudefine/nuxt-supabase-starter/query-optimization)
Your own site
<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>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 1,671 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

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.

ModelPer sessionOnce 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

Measured yesterday against content hash 587f31ca11ca, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

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.

template/.cursor/rules/query-optimization.mdc · 141 lines

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(...))

Read the full file on GitHub · 141 lines

Changes

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.

  1. yesterday First seen · 141 lines · 0 tokens per session scan A 587f31ca11ca

Subscribe to this mod's changes

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.