假设我有下表
CREATE TABLE `entities` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`timestamp` TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`data` VARCHAR(255),
PRIMARY KEY (`id`,`timestamp`)
);
每个实体通常只能由 引用id
,除了每个实体有多个修订版,由 消除歧义timestamp
。我的大部分查询将选择最近的修订,只有一小部分插入新修订,而选择所有过去的修订则更少。我预计id
平均每次只有大约十几个修订。
选择最新版本的最有效(就性能和存储空间而言)方法是什么?这个问题有公认的做法吗?
在我看来,有两种方法:(1)围绕一个GROUP BY
CREATE VIEW groupedEntities AS
SELECT id, max(timestamp) AS maxt FROM entities GROUP BY id;
CREATE VIEW currentEntities AS
SELECT a.id, data, timestamp FROM groupedEntities AS a
INNER JOIN entities AS b ON b.id=a.id AND b.timestamp=a.maxt
WHERE timestamp <= CURRENT_TIMESTAMP;
SELECT * FROM currentEntities WHERE id=?;
请注意,<=CURRENT_TIMESTAMP
允许通过将时间戳设置为遥远的未来来“删除”实体。并且 (2) 创建一个单独的表来存储当前的修订
CREATE TABLE currentEntities (
`id` INT(10) UNSIGNED PRIMARY KEY,
`timestamp` TIMESTAMP,
CONSTRAINT FOREIGN KEY (`id`, `timestamp`)
REFERENCES `entities` (`id`,`timestamp`)
);
SELECT * FROM currentEntites INNER JOIN groupedEntities WHERE id=?;
还是其他选项(3)?