/optimize-soql — Find and fix SOQL anti-patterns
Identify and resolve SOQL anti-patterns in your Salesforce codebase.
Wraps agents/soql-optimizer/AGENT.md. Scans a scope for queries-in-loops, non-selective WHEREs, missing security clauses, and SELECT-star patterns. Returns ranked fixes.
Step 1 — Collect inputs
Ask:
1. What scope should be analyzed?
File or folder path. Example: force-app/main/default/classes/AccountService.cls
or: force-app/main/default/classes/
2. Expected record count for any objects queried? (optional — escalates severity for LDV)
3. Target-org alias? (optional — enables live-org context)
Step 2 — Load the agent
Read agents/soql-optimizer/AGENT.md fully + every skill and template listed in its Mandatory Reads.
Step 3 — Execute
Follow the 5-step plan:
- Extract every SOQL query in scope
- Classify (query-in-loop, select-star, non-selective, no-security, etc.)
- Propose before/after fixes for P0 and P1 findings
- Recommend centralization if > 3 queries on the same sObject
- (Optional) org-side validation
Step 4 — Deliver
- Summary counts (files, queries, findings by severity)
- Findings table (file, line, severity, code, one-liner)
- Per-finding before/after code block for P0 and P1
- Centralization recommendation
- Citations
Step 5 — Recommend follow-ups
/refactor-apexon any class whose queries should move into aBaseSelectorsubclass./score-deploymentbefore shipping selector changes to production.
What this command does NOT do
- Does not modify files.
- Does not create custom indexes in the org.
- Does not attempt to rewrite
Database.query(<variable>)dynamic SOQL.