Search code examples
ruby-on-railsrubypostgresqlruby-on-rails-4activerecord

Which one is best for performance from 'order' and 'sort_by'?


Which one is best for performance ?

1) order (Database side calculation)

2) sort_by (Ruby side calculation)

As per my thought sort_by should be fast then order. Because order is perform from database side so it is slower and sort_by perform on ruby side after getting result but i am confusing by seeing this result ex.

=> Normal query (take more time then order and sort_by query) :-

> User.all
SELECT "users".* FROM "users"
#<Benchmark::Tms:0x000000089e4398 @cstime=0.0, @cutime=0.0, @label="", @real=6.515499990200624e-05, @stime=0.0, @total=0.0, @utime=0.0>

=> Query using order with benchmark (take less time then normal query and sort_by query) :-

> User.order(:name)  # `order` query is perform in sql so should take more time
User Load (0.6ms)  SELECT "users".* FROM "users"   ORDER BY "users"."name" ASC
#<Benchmark::Tms:0x00000007ec48c8 @cstime=0.0, @cutime=0.0, @label="", @real=0.00014305600052466616, @stime=0.0, @total=0.0, @utime=0.0>

=> Query using sort_by with benchmark (take more then order query and less time then normal query) :-

> User.all.sort_by(&:name) # Not perform `order` from database side so should be faster then `order`
SELECT "users".* FROM "users"
#<Benchmark::Tms:0x0000000897e5c0 @cstime=0.0, @cutime=0.0, @label="", @real=0.0019228710007155314, @stime=0.0, @total=0.0, @utime=0.0>

Conclusion :-

Normal query real time :- 6.515499990200624e-05

order query real time :- 0.00014305600052466616

sort_by query real time :- 0.0019228710007155314

From this conclusion can we say that :-

It's take too much time for normal query then applying order query.

we have to use order method to decrease query perform time ?

I always prefer sort_by as per my thinking.

Which one is the best :(


Solution

  • Database processing is few orders of significance faster, than Ruby. And database processing scales exclusively well, whereas Ruby's processing slowdown is proportional to the increase of the size of data you are processing.

    Processing with Ruby drastically increases both time and (especially) memory consumption, and it can easily overload the memory and never actually finish the processing having the dataset is "big".

    Some calculations with 1_000_000 rows with Ruby would take few tens of seconds, whereas PostgreSQL would finish it within <1 second.