Courseiva
Performance →mediumMultiple Choice

SF-PD2 Performance Practice Question

A developer is building a Lightning Web Component that displays a list of Accounts with their related Contacts. The component currently performs a separate SOQL query for each Account to retrieve its Contacts, resulting in 200 queries when 200 Accounts are displayed. The developer wants to reduce the number of queries to improve performance and avoid hitting governor limits. Which approach should be used?

⚠ Common exam trap

The trap here is assuming that caching or client-side grouping can replace a proper relationship query, when the real issue is the number of database round trips within a single transaction.

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 Accounts and their related Contacts.

Using a single SOQL query with a subquery retrieves both parent and child records in one call, eliminating the N+1 query problem. This approach is efficient, respects governor limits, and is the recommended pattern for displaying related records in Lightning Web Components. It ensures that only the necessary data is retrieved and reduces round trips to the database.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Use a SOQL query to retrieve all Contacts and then use Apex to group them by AccountId.

    Why it's wrong here

    Retrieving all Contacts with a single query and grouping them in Apex is possible, but it requires querying all Contacts in the org, which may be inefficient and could hit the 50,000 record query limit. It also does not filter by the specific Accounts displayed, potentially retrieving unnecessary data. A subquery is more precise and efficient.

  • ✓

    Use a single SOQL query with a subquery to retrieve Accounts and their related Contacts.

    Why this is correct

    A single SOQL query with a subquery (e.g., SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account) retrieves all parent and child records in one query, reducing the number of queries to 1. This is efficient and well within governor limits, as the subquery counts as part of the same query. It avoids the N+1 query problem and is a standard performance optimization for related data.

  • ✗

    Use a separate SOQL query for each Account but cache the results in Platform Cache to reduce database calls.

    Why it's wrong here

    Caching results in Platform Cache can reduce database calls on subsequent requests, but the initial request still performs 200 queries, which may hit the governor limit of 100 SOQL queries per transaction. Caching does not address the fundamental N+1 query problem and is not a reliable solution for a single transaction that must retrieve all data.

  • ✗

    Use Database.query with a bind variable to retrieve all Accounts and then use a Map to associate Contacts.

    Why it's wrong here

    Database.query with a bind variable can retrieve Accounts, but it does not automatically retrieve related Contacts. The developer would still need to query Contacts separately or use a subquery. Without a subquery, this approach does not solve the N+1 problem; it merely changes the syntax of the parent query. It does not reduce the number of queries for child records.

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.