-
Notifications
You must be signed in to change notification settings - Fork 411
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
Arel Extra #189
Arel Extra #189
Conversation
Thank you Pedro! Since it will be an AR/Arel-only extra, it would probably be better if we could name it as something like I have a couple of questions:
|
Codecov Report
@@ Coverage Diff @@
## dev #189 +/- ##
===================================
Coverage 100% 100%
===================================
Files 25 26 +1
Lines 514 528 +14
===================================
+ Hits 514 528 +14
Continue to review full report at Codecov.
|
updated PR description |
now it will work with all collections, grouped and non-grouped (using #group_values) |
Hi Pedro, it's starting to look very good now! Thank you! Just a few of comments:
|
always use `over` keyword even in non grouped collections
Hi Domizio, followed your suggestion and removed condition. |
Than you Pedro. |
When using this in normal collections is not very performant, because it will execute a |
Does that mean that the condition would save time after all? |
BTW, I like the idea to use Arel to get the right count instead of having to pass a custom count... we should check if we could find a way that does so efficiently, even if it doesn't "taste" so good :). I am thinking that it would be a great extra to use in ALL the apps that use AR if it would be efficient. I mean: why would anyone use the vanilla backend if arel does it better? |
.limit(1).pluck(sql).first
Made a smallbenchmark using demo data from [movielens] (Small: 100,000 ratings)(https://grouplens.org/datasets/movielens/): https://gist.github.com/pedrocarmona/00d6980758343cbd5abae4787a7250f6 it shows that pagy_arel_conditional_count is the best |
WOW. The vanilla pagy count with group is crazy slow compared to arel! In that specific count it looks like the condition slows it down just a bit when it is grouped and improves a bit more in case of non-grouped, which is probably related to the total complexity of the query, since the condition is kind of fixed time IMO.
What do you think? |
Regarding benchmark: Updated the benchmark, I had a loop inside of 1000 iterations inside which was not need. I also added 2 alternatives to the vanilla to check the damage of using Regarding pagy PR changes: Between benchmark For me the vanilla has advantage of being super simple and readable code, and also, is built on top of the public active record api, which will not easily change - the I think most people should choose the vanilla pagy backend because it is built on top of stable public rails api. The |
Thank you for using After the benchmarks I think I am going to change the way pagy counts the grouped collections: the |
0abc740
to
d526027
Compare
Hey Pedro, do you plan other changes/additions or the code is complete? |
Hi Domizio, While making the docs, I realised I didn't tested both |
Edit 18 September
Initial PR
Improve the query for counting rows of an active record collection with group by.
When user have an active record relation with includes group by clauses, the
collection.count(:all)
retrieves a hash.In
backend.rb
, pagy checks if a result is a hash and returns count based on that. In this PR is introducedaggregated
extra, in which the values are never loaded into active record as an hashes, and it knows how to perform count all of rows in group by relations.