Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChannelBroker: keep count of active contacts in ChannelBrokerState #2303

Open
ysbaddaden opened this issue Sep 12, 2023 · 0 comments
Open

ChannelBroker: keep count of active contacts in ChannelBrokerState #2303

ysbaddaden opened this issue Sep 12, 2023 · 0 comments

Comments

@ysbaddaden
Copy link
Contributor

The ChannelBroker.count_active_contacts/1 method makes a SUM() query to the SQL database. Aggregates are usually slow in SQL databases, and we make a lot of these calls: one before each attempt to queue a new contact, in order to know whether we're under or over capacity.

This may not be a problem with a single survey (there are indexes and it should avoid a sequence scan), but it may become one with multiple large surveys running in parallel.

A potential solution could be to add a counter in ChannelBrokerState for the number of active contacts, and mutate it as we activate / increment / decrement / deactivate / reenqueue / ... instead of counting all the time. We'd only count from the database on init/1 (the complexity lies in making sure the count is always correct).

See https://github.com/instedd/surveda/pull/2297/files#r1308375626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant