I am building a web application connected to MySQL database that is supposed to work like an e-shop. User should be able to filter products, but I want to display the number of results that would be added to result with each choice, like in this picture:

showing number of results

This should work across all parameters (like all display, battery or price options), as well as showing the total number of results with current filter, that would change each time the user changes something in the filter.

I know this is a common feature, but I need to get the general idea how this works. These are my ideas:

  • there might be a tool that periodically calculates and caches the number of results for each possible combination in the background.
  • or are the SELECT COUNT(*) FROM database WHERE <conditions> queries running each time the user checks a field in the filter, for each possible combination? But that would take too long, right?

I would be grateful for any ideas, especially article links or anything related to this topic.


I am not sure but you might want to do that on the front end using javascript. So :

a) fetch all your results

b) filter them on the front end upon request.

Not sure if JavaScript has caches filters and stuff. You could have a look on this but in any case given you have 1000 products this should run in no time. And yes, polling the database each time you need a subset seems like a very bad solution to me too.

  • Why do you say polling the database for subsets seems like a very bad solution? Isn't this what databases are good at, filtering data for us quickly and returning just the results you want? Filtering on the front end means that you are relying on low performance devices being able to do this quickly. And the larger your product list grows, the worse the performance get's for your front end. – Jack Dec 9 '19 at 11:22
  • Well, maybe I did not say that correctly . Yes, that what a DB is for, though it usually make sense to cache frequent requests . So, what I tried to say that sheer database polling should be the simplest solution . When your product list grows and given your users use low performance devices a different approach maybe suitable but my answer is for a product list of 1000 products. – Cap Baracudas Dec 9 '19 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.