SF-PD2 Performance Practice Question
A developer is optimizing a Visualforce page that displays a large list of Accounts with related Contacts. The page currently uses a standard controller with an <apex:pageBlockTable> and a controller extension that queries all Contacts for each Account in a loop. Users report that the page loads slowly and sometimes times out. Which technique should the developer implement to improve performance?
⚠ Common exam trap
The trap here is thinking that pagination or caching alone can solve the performance issue, but they do not address the underlying inefficient query pattern that causes multiple queries per record.
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
✓
Use a single SOQL query with a subquery to retrieve all Accounts and their Contacts, then iterate over the results to build a map for the page.
Using a single SOQL query with a subquery to fetch Accounts and their Contacts in one call eliminates the N+1 query problem, where a separate query is made for each Account's Contacts. This reduces database load, avoids governor limits, and significantly improves page load time. The developer should then process the results into a map for efficient access in the Visualforce page.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Enable lazy loading on the <apex:pageBlockTable> by setting the 'reRender' attribute to load Contacts only when a user expands a row.
Why it's wrong here
The 'reRender' attribute is used for partial page updates via AJAX, not for lazy loading of related records. It does not reduce the initial query load. Moreover, there is no built-in lazy loading for standard tables in Visualforce; this would require custom JavaScript and controller logic, which is not the intended solution here.
- ✓
Use a single SOQL query with a subquery to retrieve all Accounts and their Contacts, then iterate over the results to build a map for the page.
Why this is correct
A single query with a subquery (e.g., 'SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account') retrieves all necessary data in one call. This eliminates the N+1 query problem, reduces database round trips, and avoids the SOQL query limit. The developer can then map Contacts by AccountId for efficient rendering in the Visualforce page.
- ✗
Implement pagination using the StandardSetController to limit the number of Accounts displayed per page, thereby reducing the number of Contacts queried.
Why it's wrong here
Pagination via StandardSetController limits the number of Account records displayed, but if the controller extension still queries Contacts per Account in a loop, the N+1 problem persists for the displayed records. Pagination alone does not fix the inefficient query pattern; it only reduces the dataset size, not the query count per record.
- ✗
Cache the Contact records in Platform Cache and retrieve them on subsequent page loads to avoid repeated queries.
Why it's wrong here
Platform Cache can improve performance for repeated access, but it does not address the initial N+1 query problem. The first page load would still execute many queries and potentially time out. Caching is a complementary optimization, not a replacement for proper query design. The core issue is the inefficient querying strategy.
About these practice questions
One of 226 original SF-PD2 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.