如果我有几个对象,每个对象基本上都有一个Profile
,我用什么来存储随机属性,有什么优点和缺点:
- 将序列化哈希存储在记录的列中,与将序列化哈希存储在记录的列中不同。
- 存储一堆键/值对象
belong_to
主要对象。
Code
假设您有如下 STI 记录:
class Building < ActiveRecord::Base
has_one :profile, :as => :profilable
end
class OfficeBuilding < Building; end
class Home < Building; end
class Restaurant < Building; end
Each 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% 的情况下我不需要按自定义搜索settings
。只是想知道在性能和未来出现问题的可能性方面有何权衡。及定制数量settings
可能会在 10-50 之间。
我宁愿选择第二个选项,即设置表,因为它遵循 ActiveRecord 面向对象的约定。但我想知道在这种情况下是否会导致性能成本过高。
注意:我只是想知道 RDBMS 方面的问题。这非常适合 MongoDB/Redis/CouchDB/等。但我想纯粹了解 SQL 方面的优缺点。