Courseiva
Performance →mediumMultiple Choice

SF-PD2 Performance Practice Question

A developer is optimizing an Apex trigger that processes a large number of records. The trigger currently performs a SOQL query inside a loop to check for duplicate records. This causes a 'Too many SOQL queries: 101' error when more than 100 records are processed. What is the best way to refactor the trigger to avoid this error?

⚠ Common exam trap

The trap here is thinking that limiting records per query or using dynamic SOQL changes the query count, when the governor limit tracks the number of query executions, not the size of results.

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 store the results for lookup.

The correct approach is to bulkify the trigger by moving the SOQL query outside the loop and using a Map for efficient lookups. This reduces the number of queries to one, well within governor limits. It is a fundamental Apex best practice to avoid SOQL or DML operations inside loops, ensuring scalability and performance.

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 Database.query with a bind variable inside the loop to dynamically build the query.

    Why it's wrong here

    Using Database.query inside the loop still executes a query for each iteration, which counts against the governor limit. Dynamic SOQL does not inherently reduce the number of queries; it only changes how the query is constructed. The fundamental issue of querying inside a loop remains, so the error will still occur.

  • ✗

    Use a SOQL query with a LIMIT clause inside the loop to reduce the number of records returned.

    Why it's wrong here

    Adding a LIMIT clause inside the loop does not reduce the number of queries executed; it only limits the number of records returned per query. The governor limit counts the number of queries, not the number of records. This approach still results in one query per loop iteration, so the error persists.

  • ✗

    Enable deferred sharing on the trigger to reduce the number of SOQL queries.

    Why it's wrong here

    Deferred sharing (with sharing or without sharing) affects record-level access and query performance but does not reduce the number of SOQL queries executed. It controls whether the trigger runs in system or user mode, which can impact which records are returned, but it does not address the governor limit for the number of queries.

  • ✓

    Move the SOQL query outside the loop and use a Map to store the results for lookup.

    Why this is correct

    Moving the SOQL query outside the loop and storing results in a Map is a best practice for bulkification. It reduces the number of queries to one, regardless of the number of records processed. The Map allows efficient lookup of existing records by a key, such as a unique field, avoiding repeated database calls within 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 →

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.