agentleFS
huiferhuifer/CloudSaver/skills/d1-cost-optimizer/SKILL.md

d1-cost-optimizer

Skill2 starsChanged 5 months agoApache-2.0View on GitHub

Cloudflare D1 database cost optimization expert. Analyzes SQL query Row Reads consumption, identifies cost explosion risks, and provides index optimization, query rewriting, and denormalization strategies. Trigger when user mentions D1, Row Reads, query optimization, or database cost.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use EXPLAIN QUERY PLAN to identify:

    line 44

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Cloudflare D1 Cost Optimization Expert

Core Principle

D1 billing is based on Row Reads (rows scanned), not rows returned. Cost explosion typically comes from uncontrolled Row Reads.

Common Anti-Patterns

Anti-PatternProblemReference
Missing indexesFull table scansreferences/unindexed-join.md
Deep OFFSETScans discarded rowsreferences/deep-pagination.md
SELECT *Fetches unnecessary datareferences/select-star-all.md
Full table scanNo WHERE clausereferences/full-table-scan.md
Query in loopN+1 problemreferences/subquery-in-loop.md
LIKE '%xxx%'Leading wildcardreferences/like-wildcard.md
ORDER BY without indexTemporary sortreferences/order-by-no-index.md
Function on columnPrevents index usereferences/function-on-indexed-column.md
OR across columnsCannot use indexreferences/or-instead-of-union.md
NOT IN subqueryNULL issuesreferences/not-in-subquery.md
Type mismatchImplicit conversionreferences/implicit-type-conversion.md
Multiple aggregationsMultiple scansreferences/multiple-aggregations.md
COUNT(*) without indexFull scanreferences/select-count-star.md
UNION vs UNION ALLUnnecessary dedupreferences/union-all-vs-union.md
COUNT queriesExpensive aggregationreferences/denormalization-trigger.md
Repeated queriesNo cachingreferences/worker-cache.md

Audit Workflow

Step 1: Analyze Current Query Row Reads

Use EXPLAIN QUERY PLAN to identify:

  • SCAN TABLE = Full table scan = Cost bomb
  • SEARCH TABLE USING INDEX = Using index = Good
  • USE TEMP B-TREE FOR = Extra sorting = Needs optimization

Step 2: Check Missing Indexes

-- List all indexes
SELECT name, sql FROM sqlite_master WHERE type='index' AND sql IS NOT NULL;

-- Check foreign keys
SELECT * FROM pragma_foreign_key_list('<table_name>');

Step 3: Match Anti-Pattern

Identify which anti-pattern the query matches and apply the corresponding solution from the reference file.

Step 4: Verify Optimization

# Check query plan after optimization
wrangler d1 execute <database> --command "EXPLAIN QUERY PLAN <optimized_query>"

Benchmark Reference

ScenarioBeforeAfterImprovement
Unindexed JOIN1B Row Reads2099.9998%
Deep OFFSET10,020 reads2099.8%
Denormalized COUNT1M reads2099.998%
Worker Cache hit20 reads0100%
SELECT * vs specific200KB20KB90%
Query in loop10,0001,00090%
LIKE wildcard100,00050099.5%
OR vs UNION100,000299.998%

Output Format

## D1 Cost Audit Report

### 1. Current Status
- Database: <name>
- Tables: X
- Total rows: X

### 2. Anti-Patterns Detected
| Query | Anti-Pattern | Reference | Fix |
|-------|--------------|-----------|-----|
| ... | ... | ... | ... |

### 3. Index Check
- Missing indexes: X
- Redundant indexes: X

### 4. Recommendations (by priority)
1. [P0] Fix: <anti-pattern> → Expected improvement: XX%
   Reference: references/<file>.md

### 5. Implementation
Apply fixes from referenced files.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.