DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • How To Optimize the Salesforce CRM Analytics Dashboards Using SAQL
  • Enhancing Performance: Optimizing Complex MySQL Queries for Large Datasets
  • SQL Data Storytelling: A Comprehensive Guide
  • Unleash Peak Performance in Java Applications: Overview of Profile-Guided Optimization (PGO)

Trending

  • Maintain Chat History in Generative AI Apps With Valkey
  • Packages for Store Routines in MariaDB 11.4
  • Getting Started With Microsoft Tool Playwright for Automated Testing
  • Enhance IaC Security With Mend Scans
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Fine Tune Your Salesforce Queries

Fine Tune Your Salesforce Queries

There are certain optimization techniques you need to follow to make your Salesforce query efficient. This article will shed light on those techniques.

By 
Jaseem Pookandy user avatar
Jaseem Pookandy
·
Feb. 22, 24 · Tutorial
Like (1)
Save
Tweet
Share
3.2K Views

Join the DZone community and get the full member experience.

Join For Free

We create custom applications in Salesforce that are built on top of customer or transactional data from Salesforce data tables. Writing efficient queries is crucial to maintaining the performance of these applications and ensuring that we don't run into Salesforce limits. There are certain optimization techniques you need to follow to make your query efficient. This article will shed light on those techniques.

About Query Performance

It is obvious that the performance of your query completely depends on the complexity of data you currently have in your production org. You can write an efficient query that works in one environment but could fail in a different environment. So it is important to understand the current state of your data in the production environment. You should also have some idea regarding the future growth of your production data so that you can plan your queries accordingly. Make planning queries as part of your development cycle. Make a routine to revisit old queries in your production environment to make sure that those queries are still efficient. This article will also walk you through tools you could use to measure query performance in Salesforce. 

The first step towards an optimized query is to use index fields in your WHERE clause filters.

Indexes

Certain fields are indexed out of the box in Salesforce. When writing queries, make sure you use these fields as filters so that your query is optimized. 

List of fields to use to optimize query

Other than the default indexed fields, you can also make custom fields as indexes. You need to contact Salesforce support to do this. Please note that external ID from the list above falls under the custom index category. 

If indexing is the way to go, is it all about making all filters as index and you are good? The answer is, "No." Salesforce applies a "Selectivity Threshold" for your queries, and as long as your query is pulling data under that threshold, your query is selective or optimized. What are the thresholds? 

  • Standard index threshold: 30% of the first 1M records and 15% of the remaining records with a maximum limit of 1M records
  • Custom index threshold: 10% of the first 1M records and 5% of the remaining records with a maximum limit of 333,333 records

For example, consider the following query and suppose you have approximately 2 million opportunity records. 

Java
 
SELECT Id, name from Opportunity where RecordTypeId = '1234'


Since you are using a standard index, your threshold is 450k (300k + 150k). If your query returns more than 450k records, your query is not selective, and Salesforce Optimizer would rather go for a table scan. If you use a custom index filter, your threshold would be 150k. 

Common Mistakes in Queries

  1. Using != or NOT - Even if the field is indexed, using != or NOT will not make your queries selective. Instead, use IN or =. 
  2. Use %wildcards% - If you are running into using %wildcards in your queries, step back and ask yourself if SOSL is a better option. 
  3. Avoid nulls - Consider the following code:
Java
 
List<Opportunity> oppList = [SELECT id, customLookup__c from Opportunity];
list<String> Ids = new List<String>();
for (Opporunity opp : oppList){
Ids.add(opp.customLookup__c);
}

List<customObject__c> objs = [SELECT id from customObject__c where Id in: Ids];


If you have an opportunity with a customlookup__c=null, your list will have a null value, and your query on the customObject__c is not going to be selective. To fix this, add customLookup__c to Ids list only if customLookup__c !=null. 

4. Deleted records can affect query performance - Use isDeleted = false or empty the recycle bin to improve query performance. 

Check Your Query Performance

There are two ways you can check the performance of your query without actually running the query.

  1. Salesforce Query REST resource with an 'Explain' parameter 
  2. Query Plan from the developer console

Choosing an HTTP method to perform on the REST API service URI


Help menu -> Preferences -> Enable Query Plan
Query Plan/Query Editor

Query Performance Attributes

When you run the query plan using the above two methods, you will see "query plans" with the following key attributes. 

  • Cardinality - The estimated number of records the leading operation type would return 
  • SObject Cardinality - The approximate count of records for the query object
  • Leading operation type - The primary operation type that Salesforce will use to optimize your query; Two common types are index and tablescan. 
  • relativeCost or Cost - Cost of the query compared to the selectivity threshold; If cost is above 1, the query is not selective. 

Please note that you would find multiple plans in the response most of the time. The plan with the minimum cost will be used by Salesforce.

Conclusion

Writing efficient queries is critical in maintaining the performance of your system in Salesforce. There are certain techniques to make your query effective, such as using indexes as filters and keeping your query results within the selectivity threshold. There are also tools such as query plan in the developer console or using the explain parameter in query REST resource to measure the query performance. Make sure you use these techniques and tools in your design sessions so that you can write selective queries and ensure the top performance of your applications.

Query language Data (computing) optimization Customer relationship management

Opinions expressed by DZone contributors are their own.

Related

  • How To Optimize the Salesforce CRM Analytics Dashboards Using SAQL
  • Enhancing Performance: Optimizing Complex MySQL Queries for Large Datasets
  • SQL Data Storytelling: A Comprehensive Guide
  • Unleash Peak Performance in Java Applications: Overview of Profile-Guided Optimization (PGO)

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: