我有一个项目数据库,因为每个项目都有一组键值对,例如
place => london
field => engineering
因为我经常需要找到具有相同两个键值集的项目。id
我通过为每个key
和存储指定的 s 来创建支持表value
。
CREATE TABLE ItemKeyValues
(
ItemID int(11) unsigned,
KeyID mediumint(7) unsigned,
ValueID mediumint(7) unsigned,
PRIMARY KEY (ItemID,KeyID,ValueID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci KEY_BLOCK_SIZE=1
CREATE TABLE Pairs
(
PairID int(11) unsigned NOT NULL AUTO_INCREMENT,
Key1 mediumint(7) unsigned,
Value1 mediumint(7) unsigned,
Key2 mediumint(7) unsigned,
Value2 mediumint(7) unsigned,
UNIQUE INDEX(Key1,Value1,Key2,Value2),
PRIMARY KEY (ItemID,KeyID,ValueID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci KEY_BLOCK_SIZE=1
CREATE TABLE ItemPairs
(
PairID int(11) unsigned NOT NULL,
ItemID int(11) unsigned,
PRIMARY KEY (PairID,ItemID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_general_ci KEY_BLOCK_SIZE=1
(为简单起见,我删除了索引和 FK)
例如,表格的一行Pairs
是
PairID Key1 Value1 Key2 Value2
1 3 (for place) 88 (for London) 10 (for field) 9 (for engineering)
我需要填写最后两张表。
- 我怎样才能从表中的
INSERT
所有组合?Key1,Value1,Key2,Value2
ItemKeyValues (KeyID,ValueID)
- 我怎样才能
UPDATE
从桌子上ItemPairs
桌子ItemPairs
?
性能非常重要,因为表ItemKeyValues
超过 1 亿行(尽管这是一次性操作)。
小提琴
PS。我通过删除索引和选项简化了您的结构。
聚苯乙烯。您的 DDL 脚本包至少包含 1 个错误(复制粘贴错误?)。