Ruby on Rails
Saturday, July 27, 2013
Woops, looks like I can't edit my last post. The units are in milliseconds.
So TimedAsset.order("id").pluck(:id).last(200) <-- my old method (16.8, 18.5, 15.5, 15.6, 47.7, 17.7, 17.8)
means that I did that query in rails console 7 times, and the duration for each query was 16.8 milliseconds, 18.5 milliseconds .... 17.8 milliseconds.
This is on a ubuntu server virtual machine (1 core) within vmware on an I5-3570k.
On Saturday, July 27, 2013 2:58:51 PM UTC-4, marcin longlastname wrote:
-- On Saturday, July 27, 2013 2:58:51 PM UTC-4, marcin longlastname wrote:
It took a while for this to show on on groups, resulting in me going to sleep before I could update this.It seems that rails actually cache's active record queries? Or postgres? Here is an example of what I am referring to.If this is true, I might not even have to really worry that much about doing this that much!Anyways, after doing some experimentation within the rails console (hoping to avoid possible rails caching) and using what Fredrick recommended, here are my results.TimedAsset.order("id").pluck(:id).last(30) <-- my old method (62.5, 16.6, 18.5, 15.6, 43.6, 15.5, 17.5) SELECT "timed_assets"."id" FROM "timed_assets" ORDER BY idTimedAsset.order("id desc").limit(30).pluck(:id) <-- frederick's method (1.0, 1.6, 0.9, 0.9, 0.9, 12.4, 1.6)SELECT "timed_assets"."id" FROM "timed_assets" ORDER BY id desc LIMIT 200TimedAsset.order("id").pluck(:id).last(200) <-- my old method (16.8, 18.5, 15.5, 15.6, 47.7, 17.7, 17.8) SELECT "timed_assets"."id" FROM "timed_assets" ORDER BY idTimedAsset.order("id desc").limit(200).pluck(:id) <-- frederick's method (1.1, 1.7, 1.7, 1.0, 1.7, 1.9, 1.8)SELECT "timed_assets"."id" FROM "timed_assets" ORDER BY id desc LIMIT 200Postgres documentation for LIMIT: http://www.postgresql.org/docs/9.2/static/queries- limit.html I should look at the postgres/rails documentation more to see how limit and pluck actually work! From what I see, my method orders all the rows within postgres, postgres gets the ID column, and then rails throws out everything except the last 200 rows. Frederick's method does the same, but the extra rows are thrown out within postgres instead. I assume that the speed bump is because postgres handles the disposal of the extra rails?Anyways, thank you a whole bunch for the help frederick!
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/46dc6f64-0024-4137-8868-3f670de68892%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment