関連テーブル
user.rb section.rb
class Section < ActiveRecord::Base
class CreateUsers < ActiveRecord::Migration
acts_as_nested_set
def self.up
acts_as_section_map
create_table :users do ¦t¦
has_many :users
t.column :name, :string, :null=>false
end
t.column :section_id, :integer
end
end
def self.down
drop_table :users
end
end
class User < ActiveRecord::Base
belongs_to :section
end
遅すぎる
User Load(0.000204) SELECT * FROM users
WHERE (users.section_id = 124)
Completed in 18.13171 (0 reqs/sec) ¦ Rendering:
7.49291 (41%) ¦ DB: 10.46457 (57%) ¦ 200 OK
[http://localhost/welcome]
表示に18秒っておい!
21.
インデックスを張ってみる
User Load(0.000182) SELECT * FROM users
WHERE (users.section_id = 124)
Completed in 10.01978 (0 reqs/sec) ¦ Rendering:
6.97071 (69%) ¦ DB: 2.87357 (28%) ¦ 200 OK
[http://localhost/welcome]
10秒に短縮!
まだ使い物にならないけど
22.
すばやさの種を投入
class AddDepthToSections <ActiveRecord::Migration
def self.up
add_column :sections,:depth,:integer
Section.set_depth
end
def self.down
remove_column :sections,:depth
end
end
depthという項目を追加し、
値をセットする。depthというのは
文字通り深さの値(levelの結果)
23.
すばやさの種の結果
User Load(0.000180) SELECT * FROM users WHERE
(users.section_id = 124)
Completed in 0.84391 (1 reqs/sec) ¦ Rendering: 0.40329 (47%) ¦
DB: 0.27316 (32%) ¦ 200 OK [http://localhost/welcome]
1秒切ってメデタシメデタシ
benchmark
% ./script/performance/benchmarker 100'Section.find(:all).map(&:level)'
user system total real
#1 5.790000 0.470000 6.260000 ( 10.331682)
% ./script/performance/benchmarker 100 'Section.find(:all).map(&:depth)'
user system total real
#1 0.610000 0.010000 0.620000 ( 0.784886)
ちなみにindex張る前は
% ./script/performance/benchmarker 100 'Section.find(:all).map(&:level)'
user system total real
#1 6.180000 0.530000 6.710000 ( 20.693455)
20.693 0.784=26.394
26倍早くなった!!