У меня есть набор физических параметров, связанных с различными предметами. Например:
Item, p1, p2, p3
a, 1, 2, 3
b, 4, 5, 6
[...]
, где px
обозначает параметр x
.
Я мог бы пойти дальше и сохранить базу данных в точности так, как она была представлена; схема будет
CREATE TABLE t1 (item TEXT PRIMARY KEY, p1 FLOAT, p2 FLOAT, p3 FLOAT);
Я мог бы получить параметр p1
для всех элементов с оператором:
SELECT p1 FROM t1;
Второй альтернативой является схема типа:
CREATE TABLE t1 (id INT PRIMARY KEY, item TEXT, par TEXT, val FLOAT)
Это кажется намного проще, если у вас много параметров (как у меня). Однако поиск параметров выглядит очень неловко:
SELECT val FROM t1 WHERE par == 'p1'
Что вы посоветуете? Стоит ли переходить на «сводную» (первую) версию или id, par, val
(вторую) версию?
Большое спасибо.
EDIT
Для справки, я нашел следующий шаблон персистентности на сайте Примеры SQLAlchemy (вертикальное отображение):
"""Mapping a vertical table as a dictionary.
This example illustrates accessing and modifying a "vertical" (or
"properties", or pivoted) table via a dict-like interface. These are tables
that store free-form object properties as rows instead of columns. For
example, instead of::
# A regular ("horizontal") table has columns for 'species' and 'size'
Table('animal', metadata,
Column('id', Integer, primary_key=True),
Column('species', Unicode),
Column('size', Unicode))
A vertical table models this as two tables: one table for the base or parent
entity, and another related table holding key/value pairs::
Table('animal', metadata,
Column('id', Integer, primary_key=True))
# The properties table will have one row for a 'species' value, and
# another row for the 'size' value.
Table('properties', metadata
Column('animal_id', Integer, ForeignKey('animal.id'),
primary_key=True),
Column('key', UnicodeText),
Column('value', UnicodeText))
Because the key/value pairs in a vertical scheme are not fixed in advance,
accessing them like a Python dict can be very convenient. The example below
can be used with many common vertical schemas as-is or with minor adaptations.
"""