sf-soql-sosl-optimization

Diagnoses and rewrites slow or non-selective SOQL and SOSL queries on Salesforce.

2|Updated Sep 12, 2026
One-click install
npx skills add https://github.com/grzmol/vibe-force --skill sf-soql-sosl-optimization-grzmol
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sf-soql-sosl-optimization
Source: https://github.com/grzmol/vibe-force/tree/main/skills/sf-soql-sosl-optimization
Command: npx skills add https://github.com/grzmol/vibe-force --skill sf-soql-sosl-optimization-grzmol

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Salesforce queries on large objects time out, throw QUERY_TIMEOUT or OPERATION_TOO_LARGE errors, and fail batch start methods when filters are not selective. This Skill provides the optimizer thresholds, measurement tools, and rewrite patterns needed to make SOQL and SOSL queries index-driven and fast. ## Core Features & Use Cases - Selectivity Diagnosis: Measure filter selectivity with GROUP BY ROLLUP counts, the REST Query Plan explain endpoint, and SOQL_EXECUTE_EXPLAIN debug logs, then compare against documented standard and custom index thresholds. - Query Rewriting: Fix anti-patterns such as negative operators, null filters, leading wildcards, formula-field filters, OFFSET paging past 2,000 rows, and aggregate queries assigned directly to lists. - Safe Dynamic SOQL: Build injection-safe dynamic queries with Database.queryWithBinds, allowlisted identifiers, and String.escapeSingleQuotes, plus user-mode security enforcement with WITH USER_MODE. - Use Case: A batch job's start method fails with QUERY_TIMEOUT on a 5-million-row Account object. Use this Skill to measure the filter's cardinality, confirm relativeCost above 1.0 via the explain endpoint, request a custom index or rewrite the WHERE clause, and verify the new plan is index-driven. ## Quick Start Ask the assistant to diagnose why a specific SOQL query is timing out and rewrite it to be selective using the sf-soql-sosl-optimization skill.

Frequently Asked Questions about sf-soql-sosl-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix a non-selective SOQL query in Salesforce?

Measure the filter's row count against the selectivity thresholds: under 30% of the first million rows for standard indexes, under 10% for custom indexes. Then rewrite the WHERE clause to use an indexed field, remove negative operators like !=, and confirm with the REST explain endpoint that relativeCost drops below 1.0.

When should I use SOSL instead of SOQL?

Use SOSL when you do not know which object or field holds the value and need an efficient cross-object text search, since SOSL tokenizes words and runs against search indexes. Use SOQL when you know the object and field, or need counts, sorting, relationship traversal, or number and date filters.

Why does my SOQL query fail with NUMBER_OUTSIDE_VALID_RANGE?

This error occurs when OFFSET exceeds the 2,000-row maximum. Replace OFFSET-based paging with keyset pagination: filter on an indexed monotonically increasing field such as Id greater than the last seen value, ordered ascending with a LIMIT.

How do I prevent SOQL injection in dynamic Apex queries?

Use Database.queryWithBinds with a bind map and AccessLevel.USER_MODE so values are never concatenated. For fragments that cannot be bound, such as field names or sort direction, validate against an allowlist from Schema.getGlobalDescribe and apply String.escapeSingleQuotes to remaining free text.

Which Salesforce fields cannot be custom indexed?

Long and rich text areas, non-deterministic formula fields, encrypted text, multi-select picklists, currency fields in multi-currency orgs, binary fields, and formulas calling TEXT() on a picklist cannot be custom indexed. External ID and Unique custom fields are indexed automatically.

What is the maximum number of rows a SOQL query can return in Apex?

Apex retrieves at most 50,000 rows per transaction across all SOQL queries, with 100 queries synchronous and 200 asynchronous. For larger reads use Database.getQueryLocator in Batch Apex or Apex cursors, which reach 50 million rows, or Bulk API 2.0 query jobs.