使用 MySQL 创建简单修订系统的最佳方法是什么?
我目前正在开发一个简单的修订系统,该系统使我能够存储单个文件的多个版本,到目前为止效果良好。
表结构如下(为简洁起见,删除了过时的列):
file_id file_revision file_parent file_name
--------------------------------------------------------
1 1 0 foo.jpg
2 2 1 foorevised.jpg
3 3 1 anotherrevision.jpg
其中:
file_id
为主键,自动递增file_revision
存储修订号,默认为 < code>1(当它是第一个时)file_parent
是修订版的顶级父级,当第一个时默认为0
。file_name
是文件名。
问题:
- 最好使用单个查询,我想检索所有文件...
- 但只有每个文件的最新版本...
- ...当仅存储一个修订版本(原始)时,这个应该被检索。
非常感谢任何指点。 提前致谢。
I am currently working on a simple revision system that enables me to store multiple versions of a single file, which works fine so far.
Table structure is as follows (obsolete columns removed for the sake of brevity):
file_id file_revision file_parent file_name
--------------------------------------------------------
1 1 0 foo.jpg
2 2 1 foorevised.jpg
3 3 1 anotherrevision.jpg
Where:
file_id
is the primary key, which auto incrementsfile_revision
stores the revision number, defaulting to1
when it's the firstfile_parent
is the top level parent of revision, defaulting to0
when first.file_name
being the file name.
The problem:
- Preferably using a single query I want to retrieve all files...
- But only the latest revision of each file...
- ... when only one revision is stored (original), this one should be retrieved.
Any pointers are greatly appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了检索,最有效的方法是添加一个像 is_latest 这样的列,您需要提前填充该列,然后当您想要抓取时
select * from table where file_id=1 and is_latest=true
文件 1 的最新版本。但是,显然这会使更新此表变得更加复杂。另一种方法是将文件的最新版本存储在一个表中,将历史版本存储在另一个表中。 如果您主要想选择最新版本的所有文件,
select * from table where is_latest=true
可能相当于全表扫描,即使 is_latest 已建立索引。 如果最新的行都在一个表中,数据库可以按顺序 IO 读取它们,而不必执行以下任一操作:1)在表中进行大量查找以查找所需的记录,或者 2)扫描整个表并丢弃大数据旧记录沿途的大量数据。假设您不想更改现有的表设计,您想要做的就是选择分组最大值,请参阅 本文介绍了在 mysql 中执行此操作的几种不同方法。
The most efficient way for the sake of retrieval is to add a column like is_latest which you need to populate in advance, then
select * from table where file_id=1 and is_latest=true
when you want to grab the latest version of file 1. Obviously this will make updating this table more complicated, however.Another way to do it would be to store the latest versions of the files in one table, and historical versions in another table. If you predominantly want to select all files that are the latest version,
select * from table where is_latest=true
could likely amount to a full table scan even if if is_latest is indexed. If the latest rows were all in one table the database can read them all out in sequential IO and not have to either 1) do a lot of seeks through the table to find just the records it needs or 2) scan the whole table discarding large amounts of data along the way for the old records.Assuming you don't want to change the existing table design, what you want to do is called selecting the groupwise maximum, see this article for several different ways to do it in mysql.
然后,我会对这样的查询进行变体:
SELECT * WHERE file_revision = 1 ORDER BY Time_Stamp GROUP BY file_revision
或对此类查询进行任何数量的变体,即 limit 1 或 Order by file_id 作为最高的也将是最新的, ETC..
I would then do variations on queries like this:
SELECT * WHERE file_revision = 1 ORDER BY Time_Stamp GROUP BY file_revision
Or any any number of variation on this type of query, ie limit 1 or Order by file_id as the highest will also be the latest, etc..