Performance Tuning for Salesforce

Overview

Performance tuning patterns optimize Salesforce org performance for large data volumes, complex queries, and high user concurrency. This guide covers query optimization, LDV handling, governor limit mitigation, and caching strategies.

Core Principle: Optimize for performance proactively through selective queries, efficient data access patterns, caching, and async processing. Performance is a feature, not an afterthought.

Prerequisites

Required Knowledge:

Recommended Reading:

When to Use Performance Tuning

Use Performance Tuning When

Avoid Performance Tuning When

Query Optimization Patterns

Pattern 1: Selective Query Optimization

Purpose: Optimize queries to be selective (return <10% of records).

Implementation:

Best Practices:

Pattern 2: Query Result Optimization

Purpose: Minimize data retrieved by queries.

Implementation:

Best Practices:

Pattern 3: Query Bulkification

Purpose: Design queries to handle bulk operations efficiently.

Implementation:

Best Practices:

Large Data Volume (LDV) Patterns

Pattern 1: LDV Query Optimization

Purpose: Optimize queries for large data volumes.

Implementation:

Best Practices:

Pattern 2: LDV Data Access Patterns

Purpose: Optimize data access for large volumes.

Implementation:

Best Practices:

Pattern 3: LDV Reporting Optimization

Purpose: Optimize reports for large data volumes.

Implementation:

Best Practices:

Caching Strategies

Pattern 1: Platform Cache

Purpose: Use Platform Cache to reduce query load.

Implementation:

Example:

public class CacheService {
    public static Object getCachedData(String key) {
        Cache.OrgPartition orgPartition = Cache.Org.getPartition('local.Default');
        return orgPartition.get(key);
    }
    
    public static void setCachedData(String key, Object data, Integer ttlSeconds) {
        Cache.OrgPartition orgPartition = Cache.Org.getPartition('local.Default');
        orgPartition.put(key, data, ttlSeconds);
    }
}

Best Practices:

Pattern 2: Custom Settings Cache

Purpose: Use Custom Settings for configuration caching.

Implementation:

Best Practices:

Pattern 3: Custom Metadata Cache

Purpose: Use Custom Metadata for package-deployable configuration.

Implementation:

Best Practices:

Governor Limit Mitigation

Pattern 1: Async Processing

Purpose: Move processing to async to avoid governor limits.

Implementation:

Best Practices:

Pattern 2: Governor Limit Monitoring

Purpose: Monitor governor limit usage proactively.

Implementation:

Best Practices:

Pattern 3: Resource Optimization

Purpose: Optimize resource usage to stay within limits.

Implementation:

Best Practices:

Q&A

Q: What are performance tuning patterns for Salesforce?

A: Performance tuning patterns optimize: (1) Query performance (selective queries, indexed fields), (2) LDV handling (large data volume optimization), (3) Governor limit mitigation (async processing, resource optimization), (4) Caching strategies (Platform Cache, Custom Settings), (5) Data access patterns (efficient data retrieval). Performance tuning ensures orgs perform well at scale.

Q: How do I optimize SOQL queries for performance?

A: Optimize by: (1) Use indexed fields (Id, Name, Email, External IDs), (2) Ensure selectivity (queries return <10% of records), (3) Select only needed fields (avoid SELECT *), (4) Use LIMIT clauses (restrict result sets), (5) Use Query Plan (verify selectivity), (6) Avoid functions on indexed fields (UPPER, LOWER break indexes). Query optimization is critical for performance.

Q: How do I handle Large Data Volumes (LDV)?

A: Handle LDV by: (1) Optimize queries (selective, indexed), (2) Batch processing (process in batches), (3) Incremental processing (process only changed records), (4) Data archiving (archive old data), (5) Data partitioning (partition by date/category), (6) Async processing (use Batch/Queueable). LDV requires careful optimization.

Q: How do I use Platform Cache for performance?

A: Use Platform Cache by: (1) Cache frequently accessed data (reduce query load), (2) Use appropriate partitions (session vs org cache), (3) Set TTL appropriately (time-to-live), (4) Handle cache misses (fallback to queries), (5) Monitor cache usage (track cache hit rates). Platform Cache reduces query load and improves performance.

Q: How do I mitigate governor limit violations?

A: Mitigate by: (1) Async processing (move to Batch/Queueable), (2) Query optimization (reduce query count), (3) DML optimization (reduce DML operations), (4) Resource optimization (optimize CPU/heap), (5) Limit monitoring (monitor limit usage), (6) Alerting (alert when approaching limits). Governor limit mitigation requires optimization and monitoring.

Q: What are caching strategies for Salesforce?

A: Caching strategies: (1) Platform Cache (session/org cache for frequently accessed data), (2) Custom Settings (configuration caching, no queries), (3) Custom Metadata (package-deployable configuration, cached), (4) Report Caching (cache report results), (5) LWC Caching (cache component data). Caching reduces query load and improves performance.

Q: How do I monitor performance?

A: Monitor by: (1) Track query performance (monitor query execution time), (2) Monitor governor limits (track limit usage), (3) Track page load times (monitor user experience), (4) Create performance dashboards (visualize performance metrics), (5) Alert on degradation (alert when performance degrades). Performance monitoring enables proactive optimization.