sf-soql-optimization
Skill15 starsChanged 7 months agoMITView on GitHub
SOQL optimization — selective queries, index strategy, query plans, relationship efficiency, large data volumes. Use when queries hit limits or designing for 100K+ records. Do NOT use for general Apex, LWC, or Flow.
In this repository58 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- asideSkill
- checkpointSkill
- configure-sccSkill
- continuous-agent-loopSkill
- mcp-server-patternsSkill
- model-routeSkill
- prompt-optimizerSkill
- refactor-cleanSkill
- resume-sessionSkill
- save-sessionSkill
- search-firstSkill
- security-scanSkill
- sessionsSkill
- sf-2gp-security-reviewSkill
- sf-agentforce-developmentSkill
- sf-agentforce-mcp-actionsSkill
- sf-apex-async-patternsSkill
- sf-apex-best-practicesSkill
- sf-apex-constraintsSkill
- sf-apex-cursorSkill
- sf-apex-enterprise-patternsSkill
- sf-apex-testingSkill
- sf-api-designSkill
- sf-approval-processesSkill
- sf-aura-developmentSkill
- sf-build-fixSkill
- sf-cli-referenceSkill
- sf-data-modelingSkill
- sf-debuggingSkill
- sf-deployment-constraintsSkill
- sf-deploymentSkill
- sf-devops-ci-cdSkill
- sf-docs-lookupSkill
- sf-e2e-testingSkill
- sf-experience-cloudSkill
- sf-flow-developmentSkill
- sf-governor-limitsSkill
- sf-harness-auditSkill
- sf-helpSkill
- sf-integrationSkill
- sf-lwc-constraintsSkill
- sf-lwc-developmentSkill
- sf-lwc-testingSkill
- sf-metadata-managementSkill
- sf-platform-events-cdcSkill
- sf-quickstartSkill
- sf-security-constraintsSkill
- sf-securitySkill
- sf-soql-constraintsSkill
- sf-tdd-workflowSkill
- sf-testing-constraintsSkill
- sf-trigger-constraintsSkill
- sf-trigger-frameworksSkill
- sf-visualforce-developmentSkill
- strategic-compactSkill
- update-docsSkill
- update-platform-docsSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 5 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
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
- Distinctive: line 44
Every iteration of a loop containing a SOQL query multiplies the query count toward the 100/200 limit.
- Distinctive: line 310
Use when querying objects with polymorphic lookups (Task.WhoId, Task.WhatId, Event.WhoId, Event.WhatId) to avoid multiple queries or …
- Distinctive: line 330
Use the Developer Console Query Plan to understand query execution before deploying.
- Distinctive: line 334
Write query and click "Query Plan" instead of "Execute"
- Very specific: line 342
If you see TableScan on a large object:
- Distinctive: line 344
Add a more selective condition using an indexed field
≈ “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.
Files
SOQL Optimization
Poorly written SOQL is the most common cause of governor limit exceptions and slow page loads in Salesforce. This skill covers optimization procedures, query plan analysis, and index strategy.
@../_reference/SOQL_PATTERNS.md @../_reference/GOVERNOR_LIMITS.md
When to Use
- SOQL queries hitting governor limits (100-query limit, CPU time, or heap size)
- Investigating slow page loads, timeout errors, or CPU violations in Apex code
- Adding new SOQL queries to triggers, batch jobs, or service classes
- Auditing code for SOQL-in-loops anti-patterns during code review
- Designing queries for large data sets (100K+ records) requiring selective filters
- Troubleshooting
System.LimitException: Too many SOQL querieserrors
Selectivity Fundamentals
The query optimizer decides whether to use an index or perform a full table scan based on selectivity — the percentage of records a query expects to return. See @../_reference/SOQL_PATTERNS.md for threshold tables.
// Potentially NOT selective — if more than 10% of Contacts have Status = 'Active'
List<Contact> contacts = [SELECT Id FROM Contact WHERE Status__c = 'Active'];
// Selective — CreatedDate is indexed, LAST_N_DAYS:30 likely returns < 10%
List<Contact> recentContacts = [
SELECT Id FROM Contact
WHERE CreatedDate = LAST_N_DAYS:30
];Use the Query Plan Tool in Developer Console to verify actual query plans. These thresholds are guidelines; the optimizer considers data distribution, available indexes, and org-specific factors.
SOQL in Loops — The Fix
Every iteration of a loop containing a SOQL query multiplies the query count toward the 100/200 limit.
The Fix — Query Once, Store in Map
// Collect all IDs first
List<Account> accounts = [SELECT Id, OwnerId FROM Account WHERE Type = 'Customer'];
Set<Id> ownerIds = new Set<Id>();
for (Account acc : accounts) {
ownerIds.add(acc.OwnerId);
}
// Single query for all related records
Map<Id, User> ownerMap = new Map<Id, User>(
[SELECT Id, Name, Email FROM User WHERE Id IN :ownerIds]
);
// Now iterate — zero SOQL queries in this loop
for (Account acc : accounts) {
User owner = ownerMap.get(acc.OwnerId);
if (owner != null) {
sendWelcomeEmail(acc, owner);
}
}Trigger Context Pattern
trigger OpportunityTrigger on Opportunity (after update) {
Set<Id> oppIds = Trigger.newMap.keySet();
Map<Id, List<OpportunityLineItem>> itemsByOppId = new Map<Id, List<OpportunityLineItem>>();
for (OpportunityLineItem item : [
SELECT Id, OpportunityId, Quantity, UnitPrice
FROM OpportunityLineItem
WHERE OpportunityId IN :oppIds
]) {
if (!itemsByOppId.containsKey(item.OpportunityId)) {
itemsByOppId.put(item.OpportunityId, new List<OpportunityLineItem>());
}
itemsByOppId.get(item.OpportunityId).add(item);
}
for (Opportunity opp : Trigger.new) {
List<OpportunityLineItem> items = itemsByOppId.get(opp.Id);
if (items == null) items = new List<OpportunityLineItem>();
// Process items...
}
}Query Optimization Checklist
Use Indexed Fields in WHERE Clause
// Filter on RecordTypeId (indexed) rather than custom non-indexed fields
[SELECT Id FROM Account WHERE RecordTypeId = :RETAIL_RECORD_TYPE_ID]
// Combine indexed + non-indexed for compound selectivity
[SELECT Id FROM Account
WHERE Type = 'Customer'
AND Custom_Category__c = 'Retail']Select Only Fields You Need
// Select only what the calling code actually uses
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];Use LIMIT When Possible
Account acc = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
List<Account> recentAccounts = [
SELECT Id, Name, CreatedDate
FROM Account
ORDER BY CreatedDate DESC
LIMIT 50
];Avoid LIKE with % Prefix
// Trailing wildcard CAN use the index
[SELECT Id FROM Account WHERE Name LIKE 'Acme%']
// For full-text search — use SOSL instead
List<List<SObject>> results = [FIND 'Corp' IN NAME FIELDS RETURNING Account(Id, Name)];Use Bind Variables
// Bind variables are safe and performant
List<Account> accounts = [SELECT Id FROM Account WHERE Name = :accountName];
// For dynamic SOQL — use Database.queryWithBinds
Map<String, Object> bindVars = new Map<String, Object>{
'accountName' => accountName,
'minRevenue' => minimumRevenue
};
String safeQuery = 'SELECT Id, Name FROM Account WHERE Name = :accountName AND AnnualRevenue >= :minRevenue';
List<Account> accounts2 = Database.queryWithBinds(safeQuery, bindVars, AccessLevel.USER_MODE);Relationship Queries
Parent-to-Child (Subquery)
List<Account> accounts = [
SELECT Id, Name,
(SELECT Id, FirstName, LastName, Email
FROM Contacts
WHERE Email != null
ORDER BY LastName)
FROM Account
WHERE Type = 'Customer'
WITH USER_MODE
];Limits: Max 1 level of subquery depth. Child subqueries do NOT count as separate SOQL queries. Max 20 subqueries per query.
Child-to-Parent (Dot Notation)
List<Contact> contacts = [
SELECT Id, FirstName, LastName,
Account.Name,
Account.Owner.Name,
Account.Owner.Email
FROM Contact
WHERE AccountId != null
WITH USER_MODE
];Limits: Max 5 levels of parent traversal. Max 35 relationship traversals total per query.
Aggregate Queries
// Use COUNT() instead of loading records to count
Integer customerCount = [SELECT COUNT() FROM Account WHERE Type = 'Customer'];
// AggregateResult for grouped/summed data
List<AggregateResult> results = [
SELECT Type, COUNT(Id) recordCount, SUM(AnnualRevenue) totalRevenue
FROM Account
WHERE Type != null
GROUP BY Type
HAVING COUNT(Id) > 5
ORDER BY COUNT(Id) DESC
];Dynamic SOQL — Safe Pattern
public List<SObject> buildDynamicQuery(
String objectName, List<String> fields,
String whereClause, Integer maxRecords
) {
Schema.DescribeSObjectResult describe =
Schema.getGlobalDescribe().get(objectName)?.getDescribe();
if (describe == null) {
throw new InvalidQueryException('Unknown object: ' + objectName);
}
Map<String, Schema.SObjectField> fieldMap = describe.fields.getMap();
List<String> validatedFields = new List<String>();
for (String field : fields) {
if (fieldMap.containsKey(field.toLowerCase())) {
validatedFields.add(field);
}
}
if (validatedFields.isEmpty()) validatedFields.add('Id');
String soql = 'SELECT ' + String.join(validatedFields, ', ') +
' FROM ' + objectName;
if (String.isNotBlank(whereClause)) {
soql += ' WHERE ' + whereClause;
}
soql += ' LIMIT ' + Math.min(maxRecords, 2000);
return Database.queryWithBinds(soql, new Map<String, Object>(), AccessLevel.USER_MODE);
}Large Data Volume Patterns
Custom Indexes
For heavily-queried fields not indexed by default, request a custom index from Salesforce Support. Provide object name, field API name, and typical query pattern.
Skinny Tables
For objects with >10 million records, Salesforce Support can create a skinny table — a narrow copy with only the most-queried fields.
Batch Apex for LDV Processing
public class LargeDataProcessingBatch implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([
SELECT Id, Status__c FROM Account
WHERE CreatedDate < :Date.today().addYears(-5)
AND Status__c = 'Active'
]);
}
public void execute(Database.BatchableContext bc, List<Account> scope) { /* ... */ }
public void finish(Database.BatchableContext bc) {}
}SOSL for Full-Text Search
String searchTerm = 'Acme Holdings';
List<List<SObject>> searchResults = [
FIND :searchTerm
IN ALL FIELDS
RETURNING
Account(Id, Name, Type WHERE Type = 'Customer'),
Contact(Id, FirstName, LastName, AccountId)
LIMIT 50
];Modern SOQL Features
TYPEOF for Polymorphic Lookups
List<Task> tasks = [
SELECT Id, Subject,
TYPEOF What
WHEN Account THEN Name, Phone
WHEN Opportunity THEN Amount, StageName
END
FROM Task
WHERE OwnerId = :userId
WITH USER_MODE
];Use when querying objects with polymorphic lookups (Task.WhoId, Task.WhatId, Event.WhoId, Event.WhatId) to avoid multiple queries or instanceof checks.
Semi-Join and Anti-Join Subqueries
-- Semi-join: accounts that have contacts with emails
SELECT Id, Name FROM Account
WHERE Id IN (SELECT AccountId FROM Contact WHERE Email != null)
-- Anti-join: accounts with no closed opportunities
SELECT Id, Name FROM Account
WHERE Id NOT IN (SELECT AccountId FROM Opportunity WHERE IsClosed = true)Inner subquery can only return one field. Max one level of nesting. More efficient than querying both objects and filtering in Apex.
Query Plan Tool
Use the Developer Console Query Plan to understand query execution before deploying.
- Open Developer Console > Query Editor
- Enable Query Plan: Help menu > Preferences > Enable Query Plan
- Write query and click "Query Plan" instead of "Execute"
| Result | Meaning |
TableScan | Full table scan — potentially slow on large objects |
Index | Index used — fast and selective |
Cost | Estimated relative cost — lower is better |
If you see TableScan on a large object:
- Add a more selective condition using an indexed field
- Request a custom index from Salesforce Support
- Restructure the query to use SOSL
Related
- Agent:
sf-apex-agent— For interactive, in-depth guidance - Constraints:
sf-soql-constraints— Hard rules for SOQL safety and compliance
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
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.

