实体-属性-值 (EAV) 数据库因其局限性而受到批评,特别是低效的设计和报告方面的挑战。通过根据类型分离实体属性,可以在保持EAV跟踪历史数据的优势的同时克服这些缺点。
建议的模式引入了一个主属性表,该表对每种实体类型的属性进行分类。这允许处理各种属性类型,包括选项、整数、日期、字符串、文本和小数。
<code>entity_type { id, type, // 例如,“博客”、“用户”、“产品”等 created_at } entity { id, entity_type_id, created_at } attr { id, entity_id, type, name, created_at } option { id, attr_id, entity_id, multiple, // 允许多个值? name, created_at } attr_option { id, attr_id, entity_id, option_id, option, created_at } attr_int { attr_id, entity_id, int, created_at } attr_relation { attr_id, entity_id, entity_fk_id, created_at } attr_datetime { attr_id, entity_id, datetime, created_at } attr_string { attr_id, entity_id, var_char, created_at } attr_text { attr_id, entity_id, text, created_at } attr_decimal { attr_id, entity_id, decimal, created_at }</code>
检索实体类型:
<code> SELECT * FROM entity_type et LEFT JOIN entity e ON e.entity_type_id = et.id WHERE e.id = ?</code>
获取实体属性:
<code> SELECT * FROM attr WHERE entity_id = ?</code>
检索属性值:
<code> SELECT * FROM attr_option, attr_int, attr_relation, attr_text, ... WHERE entity_id = ?</code>
查找实体的关系:
<code> SELECT * FROM entity AS e LEFT JOIN attr_relation AS ar ON ar.entity_id = e.id WHERE ar.entity_id = 34 AND e.entity_type = 2;</code>
尽管比传统的EAV设计功能有所改进,但仍需考虑一些潜在问题:
以上是如何设计一个EAV数据库来进行属性区分的高效历史数据管理?的详细内容。更多信息请关注PHP中文网其他相关文章!