-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Use pluck(:value).first to avoid loading entire row and using try! #3282
Conversation
The SQL queries to determine each promotions code or batches of codes count are currently the slowest queries on the promotions index page. In our application containing approximately 32 Million codes and counting they've significantly slowed down the page by approximately 80-100 seconds currently. This was caused by the SQL's ORDER BY clause trying to sort all the records as they were being counted which was not necessary. Then when calling `.first` to initialize the ActiveRecordR object it would add even further delay to the rendering approximately 5 seconds. By using `pluck(:code)` the ORDER BY statement is dropped, and we don't have to initialize a full ActiveRecord object either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@JDutil thank you for this PR Out of curiosity, I'm trying to reproduce the issue, I created ~20K records, and this is what I get: p = Spree::Promotion.first
p.codes.size
# => 20012
p.codes.take.try!(:value)
# (15.0ms) SELECT "spree_promotion_codes".* FROM "spree_promotion_codes" WHERE "spree_promotion_codes"."promotion_id" = ? ORDER BY "spree_promotion_codes"."created_at" ASC LIMIT ? [["promotion_id", 1], ["LIMIT", 1]]
p.codes.pluck(:value).first
# (87.6ms) SELECT "spree_promotion_codes"."value" FROM "spree_promotion_codes" WHERE "spree_promotion_codes"."promotion_id" = ? ORDER BY "spree_promotion_codes"."created_at" ASC [["promotion_id", 1]] So, it seems that both ways you get the In order to add the ordering clause I had to add a Can you help me in understanding if my assumptions are wrong, or I'm missing some part? Thank you! |
@spaghetticode that's because the main issue was actually patched by someone else recently for v2.9: #3224 Our application is still running an older version using I still believe using |
@spaghetticode ah sorry I didn't read your full comment first I was just assuming you noticed the |
@JDutil I did the test using the standard Solidus sandbox, with SQLite. |
My tests are with Postgres. I'll have to try comparing with sqlite on the latest version, but I'm guessing it's sqlite adding the order by clause to that query. Unless there was something that changed between versions. |
@spaghetticode sorry for the delay getting back to you on this, but I just got around to testing this out on the latest master branch's sandbox for both SQLite & PostgreSQL which both behaved the same for me without an ORDER BY clause: SQLite:
PostgresQL:
I'm not sure why you're seeing an ORDER BY clause in your console... |
@spaghetticode actually I think we had another misunderstanding here.
That's because
When I created this PR it was for the older version using So my PR description was actually misleading because I'm not actually changing that anymore. What this PR does do though is that it improved the SQL query by only selecting the Sorry for the confusion here, but thought I was fixing more than it turned out after rebasing with the #3224 change. I've updated the PR title & description to better reflect the actual change here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @JDutil for this change and the clear explanation. 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JDutil thank you for further clarifying the context, I agree that pluck
is an improvement over take
👍
Update: The ordering was actually already fixed in #3224 but this continues to improve on the code & query by only loading the value attribute as a String instead of loading the entire database row & initializing a full ActiveRecord object.
Overview
The SQL queries to determine each promotions code or batches of codes
count are currently the slowest queries on the promotions index page.
In our application containing approximately 32 Million codes and
counting they've significantly slowed down the page by approximately
80-100 seconds currently.
This was caused by the SQL's ORDER BY clause trying to sort all the
records as they were being counted which was not necessary. Then when
calling
.first
to initialize the ActiveRecord object it would addeven further delay to the rendering approximately 5 seconds.
By using
pluck(:code)
the ORDER BY statement is dropped, and we don'thave to initialize a full ActiveRecord object either.
Checklist: