Filtering One Search Vs Multiple Searches

Just a quick one.

Is it more efficient to have multiple searches on one page to populate various elements, or one search that is then filtered?

For example:

I want to pull information from a database of People that tallies what colour they like.

Do I:

a) Search the database for how many [People] like [Red] and count them. Then do the same search for all of the other elements.

or

b) Hide an RG on the page that does one search for all [People] then filter the results for each colour in each element and count the results.

Itā€™s more efficient to do less searchesā€¦ so load the data you need then filter it on the page.

Of course, itā€™s not as simple as thatā€¦ it depends on where and when you need to show the data, and whether youā€™re optimising for WU or for perceived page speed etc.

But generally, Iā€™d recommend doing less searches, at least at the same time.


Butā€¦ if, as your post suggest, youā€™re just counting the things in the database, then thatā€™s completely different.

In that case you definitely donā€™t want to load all the data to the page, and then filter it, just to count it.

Doing separate agg searches (counts) is much better for that, although still not optimal.

A better way to do it is to search all the data, grouped by whatever it is you need to count (e.g. colour) with an aggregation of ā€˜countā€™.

That way, you only make a single database query, and just get the counts of each colour in a single, low-cost, response from the database, rather than 3 separate counts (and definitely rather than loading all the data to the page).

1 Like

I have my ā€œpagesā€ on one page, and theyā€™re shown and hidden based on the URL params.

In that scenario, Iā€™d be best loading the (slimmed down where possible) data and filtering it for whatever I need it for, then?