user984621
user984621

Reputation: 48453

Rails app is fast on the staging server, 20x slower in production

I am facing a weird problem, after testing some new features on the staging server, I decided to move them on production. The database is on both servers totally the same.

I am running this code:

def an_action
  search_start = Time.now
  @a1 = Time.now
  @results = find_hotels
  @a2 = Time.now
  @b1 = Time.now
  @results[:hotels].uniq_by {|x| x["hotel_name"]}.each do |result|
    @markers << "...saving some data..."
  end
  @b2 = Time.now
  search_end = Time.now # search started
  @total_time = search_end - search_start
end

private
def find_hotels
  @hotels = Hotel.includes(:services).within(distance, origin: city).where('nonstop = ?', 0).order("distance ASC")
  return {:hotels => @hotels}
end

and in the view, I am printing out the times that I measure in controller's action:

<div>
  Hotels: <%= @results[:hotels].count %>, 
  Markers: <%= @markers.count %>, 
  A: <%= @a2-@a1 %>, 
  B: <%= @b2-@b1 %>, 
  Total: <%= @total_time %>
</div>

And now comes the problems:

staging server:

  Hotels: 476, 
  Markers: 342, 
  A: 0.254782386, 
  B: 0.863244492, 
  Total: 1.118588403

production server:

  Hotels: 476, 
  Markers: 342, 
  A: 20.257541647, 
  B: 0.422051075, 
  Total: 20.679673465

localhost:

  Hotels: 476, 
  Markers: 342, 
  A: 0.84956, 
  B: 2.98855, 
  Total: 3.83876

Production and staging server are the same (only staging has about 10GB bigger disk space). I thought that the problem might be in the MySQL indexes, but on both servers the time of loading is the same (about 0.90s).

Why on the production server is the loading time so high? I am not sure where to even start with looking for the problem, I never faced this issue before.

My localhost is slower than the staging server too, but not as much as the production server.

Thank you guys in advance for your time.

Upvotes: 2

Views: 317

Answers (1)

Flugsio
Flugsio

Reputation: 11

To investigate what indexes are used, run the query prefixed with EXPLAINED on both sql servers. For example EXPLAIN SELECT * FROM users; then compare the two results.

To get the sql, use this line raise @hotels.to_sql or look in the development log.

Upvotes: 1

Related Questions