Use Merge Selections to Improve Query Selection Performance

Published
If you experience performance issues when you run ad-hoc queries, “or” statements in your filter criteria could be the problem. For best results, queries should include at least one filter that eliminates most unwanted records, but “or” statements prevent a single filter from eliminating records. For this reason, queries with “and” statements perform much better than queries with “or” statements.

Fortunately, a workaround exists to avoid “or” statements in your query filter criteria. All you need to do is create multiple ad-hoc queries to replace “or” statements and then merge the resulting selections with the Merge selections task on the Query page.

The reason “or” statements are problematic is because they prevent queries from eliminating any possible results until they fully examine tables for both filters. For example, if a constituent query’s output includes all constituent records with either a specific constituency or a specific solicit code, then the query first checks all constituent records for the constituency and then checks all constituent records again for the solicit code. The query cannot eliminate any records from the results until it fully examines both filters, so performance suffers.

This differs from “and” statements where the pool of possible results gets smaller as queries examine each subsequent filter. For example, if a constituent query’s output includes all constituent records with both a specific constituency and a specific solicit code, then the query checks all constituent records for the constituency and eliminates any records without the constituency. Then it checks a much smaller pool of records for the solicit code and eliminates any records without the solicit code.

Since queries with “or” statements cannot reduce the pool of possible results this way, even relatively simple queries can run slowly. However, since “or” statements are basically just a way to merge two different result sets, we can avoid them and improve performance by creating multiple ad-hoc queries and merging their selections.

First, we create separate queries for the filters that we wanted to join with “or” statements. In our example above, we’d create one query for constituent records with the specific constituency and then create another query for constituent records with the specific solicit code.

Next, we click Merge selections on the Query page to merge the selections for our new queries.

QueryBlogPostImage1

On the Selection Record Type Search screen, we select the source view for  the queries and click Select. In our example, the source view is “Constituent.”

Finally, we merge selections on the Merge selections screen. For our example, we select our constituency and solicit code selections. Then under Include records that are in, we select either A or B so that the new merged selection includes any constituent records with either the constituency or the solicit code.

QueryBlogPostImage2

This workaround requires a little more initial setup than an adding an “or” statement to your filter criteria, but the merged selection has the same end result and it can generate much quicker.

For more information about how to merge selections, see the “Merge Two Selections” section of the Query and Import Guide.

Leave a Comment

Check back soon!

Share: