實體-屬性-值 (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中文網其他相關文章!