Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize the query for better performance

So I have this function.

Let say I have millions of posts.

How can I optimize this function?

def fun
  Post.all.each do |post|
    if post.user.present?
       post.active = true
    else
       post.active = false
    end
    post.save
  end
end

Like do this in fewer line with better performance because this is not a very good approach.

like image 813
script Avatar asked May 02 '26 23:05

script


2 Answers

This should do the trick - and it's FAST...

Post.update_all("active = (user_id IS NOT NULL)")
like image 143
k00ka Avatar answered May 05 '26 11:05

k00ka


Here's another option that does it in two queries without any raw SQL (just plain ol' Rails):

Post.where(user_id: nil).update_all(active: false)
Post.where.not(user_id: nil).update_all(active: true)

And, believe it or not, this actually runs faster in the database than doing it in one query that's using an expression – active = (user_id IS NOT NULL) – to populate active.

Here are the speed results from testing on a table with only 20,000 records:

# Single (expression-based) query
<Benchmark::Tms:0x00007fd251a52780 @cstime=0.0, @cutime=0.0, @label="", @real=2.3656239999982063, @stime=0.0, @total=0.009999999999999787, @utime=0.009999999999999787>

# Two (purely column-based) queries
<Benchmark::Tms:0x00007fd2518c36d0 @cstime=0.0, @cutime=0.0, @label="", @real=2.309347999995225, @stime=0.0, @total=0.0, @utime=0.0>
like image 36
jeffdill2 Avatar answered May 05 '26 13:05

jeffdill2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!