If you are building the database, start by using a query to extract the data you want using a query. You can optimize this query by adding an index on
map(usersId). If the performance is adequate, you are done.
If performance is not sufficient, then you can consider storing the count separately. Maintaining the count requires triggers on
delete and possibly on
These triggers will have an effect on performance when adding and modifying data. This is usually small, but it can be important. If you are doing bulk-load operations, then you will need to manually handle the summarization values.
All this maintenance is a lot of work, and you should only go down that path if you really need to do it that way.