[fix](query) improve performance total count using cache #57
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
count
might seem innocent, but it quickly can get very painful if you are working with millions of records.In this PR, we first change
count(r.d)
bycount(*)
, the second is slightly more performant, it might be counter intuitive but an explanation is giving here why https://www.cybertec-postgresql.com/en/postgresql-count-made-fast/There is a tiny of refactoring to have a single source for counting through
total_count
Finally, we introduce a caching, this is definitely arguable.
I can understand many good reason for trying to keep the deps as low as possible in kaffy.
We could certainly implement our own cache: https://thoughtbot.com/blog/make-phoenix-even-faster-with-a-genserver-backed-key-value-store if really using cachex.
Having say that cachex seems to be a very well tested packages.
Personally I have been using https://github.com/melpon/memoize heavily in a backend service production but it seems that cachex might be a better fit for phoenix and it's certainly more active.