A developer is troubleshooting a transaction that exceeds governor limits due to SOQL queries. Which THREE techniques help prevent hitting SOQL query limits in Apex? (Choose THREE.)
Trap 1: Executing SOQL queries inside a for loop iterating over all child…
Placing a SOQL query inside a loop causes a separate database round-trip for every single iteration, which rapidly exhausts the synchronous governor limit of 100 queries. Developers are often tempted by this pattern when attempting to retrieve child records dynamically for every parent record processed in a collection. It would only be the correct choice if querying a tiny, fixed number of records outside governor limits, or when leveraging lazy loading in specific UI extensions where query counts are strictly controlled and bounded.
Trap 2: Calling Database.query() for every single record processed in a…
Executing SOQL queries inside a loop or for every individual record triggers a distinct query against the database on each iteration, causing the transaction to rapidly exhaust the strict governor limit of 100 SOQL queries per transaction in bulk operations. This approach appears attractive when developers need to dynamically construct queries based on specific field values of individual records or implement polymorphic queries where filter criteria change per record. While `Database.query()` is the correct mechanism for constructing dynamic SOQL strings at runtime—such as building flexible search filters with variable user inputs—it must be supplied with a collection of values or a single aggregated string rather than being executed inside an iterative loop processing records.
- A
Using maps and collections to store query results and perform in-memory lookups instead of querying inside loops.
Querying inside a loop easily breaches the synchronous governor limit of 100 SOQL queries per transaction. Storing query results in maps and collections allows developers to fetch all required data in a single aggregate query upfront, satisfying the core constraint of reducing query execution frequency. Subsequent lookups are then performed entirely in-memory against the collection rather than hitting the database repeatedly, drastically lowering resource consumption and preventing limits from being exceeded.
- B
Writing queries that retrieve only the specific fields and records required using WHERE clauses.
Restricting retrieved data directly addresses the governor limit on total SOQL queries by fetching only necessary fields and records within a single database round-trip. Using selective `WHERE` clauses ensures that the heap size and database processing overhead remain minimal, preventing both query row limits and heap size exceptions in complex transactions. This targeted data retrieval directly satisfies the constraint of mitigating SOQL-related governor limit breaches during execution.
- C
Using relationship queries (parent-to-child or child-to-parent) to retrieve related data in a single query.
Relationship queries directly address the governor limit on the total number of SOQL queries issued per transaction (the 100-query limit) by retrieving parent and child records in a single database round-trip. By joining related objects within one statement instead of executing separate queries iteratively inside a loop, developers drastically reduce the cumulative query count. This consolidation prevents the transaction from exceeding the synchronous execution threshold, making it a foundational best practice for scalable Apex code.
- D
Executing SOQL queries inside a for loop iterating over all child records.
Why it fails: Placing a SOQL query inside a loop causes a separate database round-trip for every single iteration, which rapidly exhausts the synchronous governor limit of 100 queries. Developers are often tempted by this pattern when attempting to retrieve child records dynamically for every parent record processed in a collection. It would only be the correct choice if querying a tiny, fixed number of records outside governor limits, or when leveraging lazy loading in specific UI extensions where query counts are strictly controlled and bounded.
- E
Calling Database.query() for every single record processed in a trigger.
Why it fails: Executing SOQL queries inside a loop or for every individual record triggers a distinct query against the database on each iteration, causing the transaction to rapidly exhaust the strict governor limit of 100 SOQL queries per transaction in bulk operations. This approach appears attractive when developers need to dynamically construct queries based on specific field values of individual records or implement polymorphic queries where filter criteria change per record. While `Database.query()` is the correct mechanism for constructing dynamic SOQL strings at runtime—such as building flexible search filters with variable user inputs—it must be supplied with a collection of values or a single aggregated string rather than being executed inside an iterative loop processing records.