Courseiva
Performance →hardMultiple Choice

SF-PD2 Performance Practice Question

A developer is building a Visualforce page that displays a custom object's related list with 5,000 child records. The controller currently performs a SOQL query inside a getter method that is called multiple times during page rendering, and users report the page takes over 30 seconds to load. The developer needs to improve performance without changing the page's functionality. Which approach should the developer take?

⚠ Common exam trap

The trap here is assuming that setting readOnly or using asynchronous execution will automatically fix slow rendering, when the real issue is that a getter re-runs its query on every evaluation.

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 into the constructor and store the results in a private member variable, then have the getter return that cached collection.

Visualforce getters are invoked each time the expression is evaluated during rendering, so a query inside a getter runs repeatedly. Caching the query result in the controller constructor executes it only once per request. This reduces database round trips and CPU usage, improving page load time. The other options either violate Apex rules, use the wrong query language, or do not eliminate repeated execution.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Set the page's readOnly attribute to true so that the query is executed in a read-only context and no longer counts against governor limits.

    Why it's wrong here

    The readOnly attribute allows more query rows and avoids some limits, but it does not prevent the getter from being called multiple times. The page would still execute the same SOQL repeatedly, so the performance problem would remain. readOnly is useful for read-heavy pages but does not address redundant query execution.

  • ✓

    Move the SOQL query into the constructor and store the results in a private member variable, then have the getter return that cached collection.

    Why this is correct

    A getter is invoked repeatedly during Visualforce rendering, so placing the query inside it causes redundant database calls. Moving the query to the constructor executes it once per request and caches the result in a member variable. This reduces query count, avoids repeated database work, and directly addresses the slow page load while preserving the displayed related list.

  • ✗

    Add a @future annotation to the getter method so the SOQL query executes asynchronously and does not block the page render.

    Why it's wrong here

    @future methods must be static and return void, and they cannot return query results to a Visualforce getter. Using it here would not provide data to the page and would cause a compile error. Asynchronous execution also does not reduce the number of queries or improve rendering time for data that must appear synchronously on the page.

  • ✗

    Replace the SOQL query with a SOSL search to retrieve the child records more efficiently across multiple objects.

    Why it's wrong here

    SOSL is designed for text-based searches across multiple objects and returns results in a different structure than SOQL. It is not appropriate for retrieving a known set of child records related to a parent. Switching to SOSL would complicate the controller logic and would not resolve the repeated execution problem caused by the getter.

About these practice questions

Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.