如何提升 RailS 应用的性能?
- - ITeye资讯频道「铁路很慢」,你也许听过这个笑话,那么我们的 Rails 框架呢. 如果说 Rails 慢,那么如何提升 Rails APP 的性能就成了开发者们最关注的问题. 也许你听说过很多提升 RoR APP 性能的方法,它们有难有易,我们需要在选择其中最能帮助开发者脱离性能困境的. 这里列举了几种不同的提升 Rails 应用性能的方法.
class AddIndexToClientIndustry < ActiveRecord::Migration def change add_index :client_industries, :client_id end end
CREATE INDEX addresses_addressable_id_addressable_type_idx ON addresses USING btree (addressable_id,addressable_type); t1 = Time.now c = Company.find(178389) a = c.addresses.first t2 = Time.now puts "---Operation took #{t2-t1} seconds---” Result with index: ---Operation took 0.012412 seconds---
DROP INDEX addresses_addressable_id_addressable_type_idx; t1 = Time.now c = Company.find(178389) a = c.addresses.first t2 = Time.now puts "---Operation took #{t2-t1} seconds---” Result without index: ---Operation took 0.378073 seconds---
<% @clients.each do |client| %> <tr> <td><%= client.id %></td> <td><%= client.business_name %></td> <td><%= client.industries.first.name %></td> </tr> <% end %> # app/controllers/clients_controller.rb def index @clients = Client.all end
Processing by ClientsController#index as HTML SELECT "clients".* FROM "clients" SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON "industries"."id" = "client_industries"."industry_id" WHERE "client_industries"."client_id" = 1 LIMIT 1 SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON "industries"."id" = "client_industries"."industry_id" WHERE "client_industries"."client_id" = 2 LIMIT 1 SELECT "industries".* FROM "industries" INNER JOIN "client_industries" ON "industries"."id" = "client_industries"."industry_id" WHERE "client_industries"."client_id" = 3 LIMIT 1 …
# app/controllers/clients_controller.rb def index @clients = Client.includes(:industries).all end
Processing by ClientsController#index as HTML SELECT "clients".* FROM "clients" SELECT "client_industries".* FROM "client_industries" WHERE "client_industries"."client_id" IN (1, 2, 3) SELECT "industries".* FROM "industries" WHERE "industries"."id" IN (1, 5, 7, 8, 4)
t1 = Time.now Company.where(:country_id=>1).find do |c| puts "do something!" if ['Mattski Test'].include?(c.common_name) end t2 = Time.now puts "---Operation took #{t2-t1} seconds---”
1 query, taking 46.65 seconds
t1 = Time.now Company.where(:country_id=>1).find_each do |c| puts "do something!" if ['Mattski Test'].include?(c.common_name) end t2 = Time.now puts "---Operation took #{t2-t1} seconds---"
100 queries, taking 15.53 seconds in total (3x faster)