SF-PD2 Performance Practice Question
A developer is building an Apex controller for a Visualforce page that must display a list of 2,000 Contacts for an Account. The current implementation uses a SOQL query inside a loop to fetch Contacts for each Account row, resulting in sluggish page loads and occasional governor limit errors. Which approach should the developer take to improve performance?
⚠ Common exam trap
The trap here is thinking that adding a LIMIT or switching to SOSL solves the loop query problem, when the real fix is to query once and correlate with a Map.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Move the SOQL query outside the loop and use a Map to correlate Contacts to Accounts.
The correct approach is to bulkify the code by moving the SOQL query outside the loop and using a Map to correlate child records to parents. This reduces the number of queries to one, avoiding governor limits and improving performance. The other options either do not eliminate the loop query or introduce inappropriate tools.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Move the SOQL query outside the loop and use a Map to correlate Contacts to Accounts.
Why this is correct
This approach eliminates redundant queries by fetching all Contacts in a single query and then using a Map keyed by AccountId to efficiently associate them with each Account. It reduces the number of SOQL queries from potentially thousands to one, avoiding governor limits and dramatically improving page load time.
- ✗
Add a LIMIT clause to the SOQL query inside the loop to reduce the number of records returned per Account.
Why it's wrong here
Adding a LIMIT reduces the number of records per query but still executes a query for each Account iteration. The total number of SOQL queries remains high, potentially exceeding governor limits and causing slow performance. It does not address the core issue of querying inside a loop.
- ✗
Use Database.getQueryLocator to handle the Contacts and bind the result to the page.
Why it's wrong here
Database.getQueryLocator is useful for large data sets in batch Apex or Visualforce with pagination, but it does not directly address the issue of querying inside a loop. It would require significant refactoring and may not be suitable for displaying a fixed list of 2,000 Contacts on a single page.
- ✗
Replace the SOQL query with a SOSL search to retrieve all Contacts at once.
Why it's wrong here
SOSL is designed for text-based searches across multiple objects, not for retrieving all child records for a given parent. Using SOSL here would be inefficient and may not return all Contacts. It also does not solve the loop query problem because SOSL would still be called repeatedly if left inside the loop.
About these practice questions
This SF-PD2 question is part of Courseiva's 226-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Salesforce exam blueprint
This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.