OBLYTECH Logo
OBLYTECH Logo

Selective Queries as a Design Review Gate for Salesforce Automation

A selective query review is a design check that asks whether each query in Salesforce automation narrows a large object to a small share of its records. Adding it to design review catches automation that scans a large object without a usable filter while the fix is still a design change, not a production incident.

Why selective queries belong in design review

Many performance problems in Salesforce automation start in a developer org. A query returns the right records against two hundred test rows, then runs against a production object holding several hundred thousand. The query text looked reasonable when it was built. Nobody asked how many rows it would touch once the data grew.

Design review is the cheapest place to ask that question. A reviewer who reads the query, the object it targets and the expected volume can stop the problem before a release date depends on it. After go-live, the same fix becomes a change request, a regression pass and a conversation with the teams that rely on the output.

A query that scans an entire table without a usable filter can time out. Small test data sets often hide the problem, so a query that passed testing can fail once the object grows after release.

How a query becomes selective

A query is selective when its filter lets the platform return a small share of an object’s rows, usually by using an index. Some standard fields are indexed by the platform, and the query optimizer chooses execution paths from statistics about how the data is distributed. A filter on an indexed standard field therefore gives the optimizer a stronger starting point, although the reviewer should still confirm the field in the design.

A filter on a custom field needs one more question in review. The reviewer should confirm how that field is indexed in the target object before assuming the query will stay selective, because a filter on an unindexed field can leave the query scanning most of the table.

Selectivity belongs to the whole query, not to one clause. A condition that matches half the rows of a large table is not selective, however tidy it looks in the code. The reviewer’s question is direct: after all conditions are applied, how many rows remain at the volume the object will reach?

Platform limits that turn a slow query into a failed transaction

Synchronous Apex transactions run inside fixed platform limits. The three that matter to query review are the number of queries a transaction may issue, its CPU time and its heap size. A design that already uses most of the query allowance leaves little margin for triggers that other teams add later, or for a data pattern nobody tested.

The current figures are set by the platform and can change, so look them up for the release you run rather than working from memory. The admin-side view of the same limits is covered in the governor limits guide for admins.

Heap and CPU failures follow the same pattern. Retrieving all fields on a record when the logic reads two adds heap. Running a long calculation in the same synchronous transaction that queries records adds CPU time. A query that passes review alone can still fail the transaction once the rest of the logic runs, so the review should add up all three measures together.

A design review checklist for query-heavy automation

Apply this checklist to any automation that reads records. The reviewer should answer each line from the design document, without running the code.

  1. Name the object and its expected volume. Record today’s row count and the count expected in two years, taken from a count against a production-sized data set or from the business owner.
  2. Describe the filter in plain words. If the filter cannot be written as “records where this field equals this value, changed in the last N days,” the reviewer cannot judge it.
  3. Mark the filter fields as standard or custom. Some standard fields are indexed by the platform. Custom fields need the indexing check described above.
  4. List the retrieved fields. Keep the fields the logic reads and return the rest to the author.
  5. Count queries per transaction. Include queries from triggers that other teams have added to the same object and event, since they draw on the same limit.
  6. Look for queries inside loops. A query that runs once for each record in a collection multiplies the query count by the size of that collection.
  7. Decide where the work runs. Confirm whether the logic runs synchronously in the user’s transaction or in a batch or scheduled job, and check that the record volume fits that choice.

Running the query gate in the release process

A checklist works when the release process asks for it. Add a line to the design review template that links each automation to its query record, and make the release ticket incomplete without that line. The reviewer signs the query line, not the whole design.

Testing is where the review becomes concrete. Build and time the query in a sandbox that holds data volumes close to production, not a few hundred developer records. Record the timing and the row count together. A timing result without the row count tells the next reviewer little, because the same query takes a different time at a different volume. Sandbox refresh timing decides whether that data exists when the test runs, so schedule the refresh before the test window, following the steps in sandbox refresh planning.

Read the release notes for the version under test before the run. A platform change that goes unnoticed can make a test result hard to interpret. The steps for that read are in the release notes review before each sandbox test guide.

For help planning this work, see Salesforce implementation.

Handling queries that fail review

A failed review does not cancel the feature. It means the query needs a different shape. Four options cover the usual cases: narrow the filter so fewer rows match, move the work into a batch or scheduled job that processes records in chunks, replace the query with a lookup on a key the caller already holds, or accept the query with a written volume ceiling and a named owner.

Bulkification and caching are part of the fix. Processing records in bulk keeps the query count from rising with the number of records changed, and caching reference data that changes rarely removes repeated lookups. These are design changes, so they belong in the same review rather than in a late tuning pass.

Write the decision into the review record. Name the chosen option, the expected row count after the change and the date the volume will be measured again. A ceiling without a re-measurement date tends to get forgotten, and the query then runs against a larger object than the reviewer approved.

Questions about running selective query reviews

Which automation should go through the query check first?

Start with automation that reads the largest objects in the org, scheduled jobs that process whole objects, and integrations that pull records in bulk. These carry the highest risk of a scan that outgrows its window. Lookups against small reference objects can wait for the normal review cycle.

Should existing automation be reviewed as well as new builds?

Both, in sequence. Begin with a one-time inventory of automation that touches the largest objects and review those first. After that, apply the gate to new builds and to any change to an existing query. The inventory finds queries already at risk, and the gate keeps new ones from joining them.

What should the reviewer do when the business needs the full data set?

Ask what the business does with the full set. Reports and syncs often fit a scheduled batch with a defined window better than a synchronous lookup in a user’s transaction. If the full set must be available on screen in real time, the design may need a different data source or a summary object. That decision is larger than a query review, so escalate it to the design owner.

Does a passing sandbox test prove that a query is selective?

It shows that the query ran without error at the volume the sandbox holds. It does not show that the query will stay selective as the object grows, because the optimizer’s choices depend on how the data is distributed. Measure the volume in production on a schedule, and treat the sandbox result as one entry in the review record.

Who signs off on the query line in the review?

The technical owner of the release signs the query line, because that person owns the transaction budget. The business owner confirms the volume figures, since they know how the data grows. Field access is a separate question, and the checks for it are set out in the field-level security checks for compliance reviewers guide.

What should a query review record contain?

Keep the query text, the object name, the filter fields, the expected and measured row counts, the fields retrieved, the number of queries in the transaction, and the reviewer’s decision. Store the record next to the design document so the release ticket points to one place. A reviewer in the next cycle can then see what changed without rebuilding the analysis.