为此查询创建索引Heroku说这是我最慢的

时间:2019-06-20 22:44:22

标签: ruby-on-rails database activerecord

Heroku说这是我最慢的查询。您的迁移将如何创建索引以加快速度?

它是由created_at::asc排序的...不确定如何将其用于索引...

这是Heroku仪表板中的查询:

SELECT "line_movements"."game_moneyline_home"
FROM "line_movements"
WHERE "line_movements"."event_id" = $1
ORDER BY  "line_movements"."created_at" ASC

通过索引加快速度?

1 个答案:

答案 0 :(得分:1)

您可以索引event_id。 然后,如果您真的想加快查询速度,请为created_at使用整数时间戳。 您可以做类似的事情

#add column created_at_int:integer to line_movements
#line_movement.rb
after_create :store_unix_time
def store_unix_time
  update_attributes created_at_int: created_at.to_i
end
#and get the time converted back by
def created_at_timestamp
  DateTime.parse(Time.at(created_at_int).to_s).in_time_zone
end

sql按整数排序比按日期时间排序要快。