SQL数据库设计与一些用户定义的字段

时间:2017-04-12 03:09:46

标签: sql database postgresql database-design entity-attribute-value

我正在开发一个数据库模式来处理数据收集,然后报告这些数据。

在需求讨论之后,似乎实体 - 属性 - 值(EAV)解决方案或平面表解决方案都没有问题 - 因为数据有点稀疏但不是高度稀疏

但是,用户定义的字段将来会成为必须的,但我知道使用EAV表查询和优化RDBMS会变得复杂。

我已经看过讨论here了,我认为可以选择类似选项1 的选项。例如,有许多设置字段,然后是一些备用字段,用户可以定义标签。

在报告方面,使用这种方法而不是使用EAV是否有任何不足之处?

1 个答案:

答案 0 :(得分:4)

你会后悔EAV,特别是在报道

  1. 在尝试任何操作之前,请确保您了解现有的数据模型模式:Ready to use database model patterns

  2. 熟悉表继承:How can you represent inheritance in a database?

  3. 考虑允许用户修改自己的架构:https://martinfowler.com/bliki/UserDefinedField.html

  4. EAV几乎总是一个非常糟糕的主意。如果在尝试上述操作后仍需要自定义字段,请使用带索引的blob类型(如JSON或XML):http://backchannel.org/blog/friendfeed-schemaless-mysql。 Postgres的二进制Traceback (most recent call last): File "/home/anton/Projects/pr/core/run/stuff.py", line 26, in <module> main() File "/home/anton/Projects/pr/core/run/stuff.py", line 23, in main SomeObj().call() File "/home/anton/Projects/pr/core/run/stuff.py", line 18, in call return Session.query(SomeModel).filter(SomeModel.id == self.some_method()).all() File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2613, in all return list(self) File "/home/anton/Projects/pr/core/model/cachingquery.py", line 74, in __iter__ return Query.__iter__(self) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2761, in __iter__ return self._execute_and_instances(context) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2776, in _execute_and_instances result = conn.execute(querycontext.statement, self._params) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 914, in execute return meth(self, multiparams, params) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement compiled_sql, distilled_params File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context context) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1341, in _handle_dbapi_exception exc_info File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context context) File "/home/anton/Projects/pr/.venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute cursor.execute(statement, parameters) sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'MagicMock' [SQL: 'SELECT "table".id AS table_id \nFROM "table" \nWHERE "table".id = %(id_1)s'] [parameters: {'id_1': <MagicMock name='some_method()' id='140314377476560'>}] 速度很快,允许索引/查询