それぞれが基本的に を持つ複数のオブジェクトがある場合Profile
、ランダムな属性を格納するために使用しているもの、長所と短所は何ですか:
- シリアル化されたハッシュをレコードの列に格納することと、
belong_to
メインオブジェクトである一連のキー/値オブジェクトを保存します。
コード
次のような STI レコードがあるとします。
class Building < ActiveRecord::Base
has_one :profile, :as => :profilable
end
class OfficeBuilding < Building; end
class Home < Building; end
class Restaurant < Building; end
各has_one :profile
オプション 1. シリアル化されたハッシュ
class SerializedProfile < ActiveRecord::Base
serialize :settings
end
create_table :profiles, :force => true do |t|
t.string :name
t.string :website
t.string :email
t.string :phone
t.string :type
t.text :settings
t.integer :profilable_id
t.string :profilable_type
t.timestamp
end
オプション 2. キー/値ストア
class KeyValueProfile < ActiveRecord::Base
has_many :settings
end
create_table :profiles, :force => true do |t|
t.string :name
t.string :website
t.string :email
t.string :phone
t.string :type
t.integer :profilable_id
t.string :profilable_type
t.timestamp
end
create_table :settings, :force => true do |t|
t.string :key
t.text :value
t.integer :profile_id
t.string :profile_type
t.timestamp
end
あなたならどちらを選びますか?
99% の確率で custom で検索する必要はないと仮定しますsettings
。パフォーマンスと将来の問題の可能性に関して、どのようなトレードオフがあるのか 疑問に思っています. カスタムの数はsettings
、10 ~ 50 の範囲である可能性があります。
ActiveRecord のオブジェクト指向の規則に従っているため、設定テーブルを使用する 2 番目のオプションを使用したいと思います。しかし、このような状況では、パフォーマンス コストが高すぎるのではないかと考えています。
注: RDBMS に関してのみ疑問に思っています。これは、MongoDB/Redis/CouchDB などに最適です。しかし、私は純粋にSQLの長所と短所を知りたい.