Intermediate 25 min read

How Salesforce Executes a SOQL Query

By the end of this lesson, you'll be able to:

  • Name the major stages Salesforce goes through when running a SOQL query
  • Explain where the query optimizer and indexes fit into that sequence
  • Connect this sequence back to the selectivity concept from the previous lesson

Prerequisites: "Query Selectivity and Performance"

The simplified sequence

When a SOQL query runs, Salesforce roughly works through:

  1. Parsing — the query text is checked for valid syntax.
  2. Permission and sharing checks — which records the running user is even allowed to see (covered in depth in a later Security module).
  3. Query optimization — the platform's optimizer decides the fastest way to actually retrieve matching rows, favoring indexed fields when the WHERE clause allows it.
  4. Execution — the underlying database retrieves the matching rows.
  5. Returning results — rows are converted into sObject records and handed back to Apex as a List.

Where selectivity fits in

Step 3 is exactly where the previous lesson's "selectivity" concept becomes concrete: the optimizer can only take the fast, indexed path when the query's WHERE clause actually allows it. A non-selective query forces the optimizer into a slower, broader scan — the direct cause of the timeout risk discussed previously.

Why sharing rules matter, previewed

List<Account> accounts = [SELECT Id, Name FROM Account]; // does this see every Account, or only some?
  1. List<Account> accounts = [SELECT Id, Name FROM Account]; — step 2 of the sequence means whether this query returns every Account or only the ones the currently running user is allowed to see depends on something outside this line entirely: the sharing mode of the Apex class the query is running in.

Apex classes do not enforce the running user's record-level sharing by default — a class has to explicitly opt in with the with sharing keyword for that. Exactly what that means, and why the default matters, is covered fully in the upcoming Security module — this lesson's job is just to place the decision point correctly in the overall sequence.

Exercise

As a comment, explain why the exact same SOQL query, written identically in two different Apex classes, could return two different sets of records.

Show hint

Think about step 2 in the sequence — what varies isn't the query text, but something about the class running it.

APEX

How Salesforce Executes a SOQL Query Quiz

1. In the simplified query execution sequence, what determines whether the query optimizer can use a fast, indexed path?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

This closing lesson walks through what actually happens between writing [SELECT ...] and getting a List<sObject> back — tying together selectivity, indexes, and sharing rules into one coherent picture.