Courseiva
Performance →mediumMultiple Choice

SF-PD2 Performance Practice Question

A developer is writing an Apex trigger on the Account object that performs a SOQL query inside a loop to retrieve related Contacts. To adhere to performance best practices and avoid governor limits, what is the most efficient pattern to implement?

⚠ Common exam trap

Candidates often use a map inside the loop or attempt to query without pre-collecting IDs, which still results in hitting the SOQL limit during bulk operations.

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

✓

Collect related IDs in a Set, query all Contacts once, and store them in a Map for lookup.

Retrieving data inside a loop creates an N+1 query problem, which rapidly consumes SOQL limits and degrades performance. By collecting IDs into a Set and performing a single bulkified query before processing the records, the developer reduces the total number of round-trips to the database. This pattern is essential for maintaining application scalability and ensuring the trigger remains performant as the data volume grows within the production environment.

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 the Database.getQueryLocator method within the loop to retrieve records.

    Why it's wrong here

    Database.getQueryLocator is primarily designed for Batch Apex and is not intended for use within a trigger loop. Using it in this context would not resolve the governor limit issue and would significantly impact the overall execution speed and memory footprint of the transaction.

  • ✗

    Move the SOQL query into a static method that is called by the trigger.

    Why it's wrong here

    Moving the query into a static method does not change the fact that the query is still being called inside a loop. The fundamental issue of executing a query for every iteration remains, which will still lead to hitting governor limits as the batch size increases.

  • ✓

    Collect related IDs in a Set, query all Contacts once, and store them in a Map for lookup.

    Why this is correct

    Bulkifying the query by using a Set of IDs allows for a single SOQL statement. Storing the results in a Map provides O(1) time complexity for subsequent record lookups, which is significantly more efficient than nested loops or repeated database queries within the execution context.

  • ✗

    Increase the SOQL query limit by using the @ReadOnly annotation on the method.

    Why it's wrong here

    The @ReadOnly annotation is meant for read-only pages and does not increase the SOQL limit within a standard trigger execution context. It provides no mechanism to bypass governor limits for DML operations or standard record processing, making it irrelevant for this performance-related architectural problem.

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.