数据库数据文件中的数据是如何组织的
作为学习练习,我尝试用 C# 编写一个简单的嵌入式数据库。一切都很顺利,但在将数据保存到磁盘时我真的陷入困境。
作为我的问题之一的示例..我可能需要将数据“插入”到数据文件的中间。对于顺序文件访问来说,这显然是不可能的。由于明显的性能原因,每次插入时重写整个文件的后半部分并不是一个选择。
我能想到的唯一解决方案是写入每个表,然后在文件中添加一些空白空间。空白空间将用于写入新数据,每次表用完其可用空间时,文件将需要重组/增长。
我想我的问题是..典型数据库的数据文件中的数据“看起来是什么样子”?新数据如何/在哪里写入文件?
As a learning exercise I am attempting to write a simple embedded database in C#. Everything is going fine but I am getting really stuck when it comes to saving the data to disk.
As an example of one of my problems.. I may need to "insert" data into the middle of the data file. This clearly isn't possible with sequential file access. Re-writing the entire last half of the file every time there is an insert isn't an option for obvious performance reasons.
The only solution I can imagine is to write each table followed by some empty space in the file. The empty space will be used to write new data, and the file will need restructuring / growing each time a table uses up its available space.
I guess my questions are.. exactly what does the data "look like" inside a typical DB's data file? How / where is new data written in the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,数据库会使用B树来存储数据(其中键是行的主键,值是行的内容)和索引。通过这种方式,您可以在
O(log n)
时间内将行插入到任意位置。例如,请参阅 SQLite 数据库的文件格式,它描述了 SQLite 如何在内部使用 B 树节点只存储指针,叶节点只存储数据。
另请参阅:http://en.wikipedia.org/wiki/B-tree#Insertions_and_deletions_cause_trouble< /a> ,这似乎解决了您遇到的问题。
Generally databases will use a B-tree to store both the data (where the key will be the row's primary key, and the value will be the content of the row) and indexes. This way you can insert rows into arbitrary locations in
O(log n)
time.Ex, see the file format for SQLite databases, which describes how SQLite uses a B-tree where internal nodes only store pointers and leaf nodes only store data.
See also: http://en.wikipedia.org/wiki/B-tree#Insertions_and_deletions_cause_trouble , which seems to address the issue you're having.
大卫·沃尔弗的回答是错误的。数据库的数据不存储在B树中。 B 树(通常是 B+ 树)仅在内部节点中存储键和子指针,在叶节点中存储键和数据指针。 B+ 树通常不存储数据(它们可能会为关系表存储数据)。数据库的数据存储在以块组织的数据文件中。
The answer of David Wolever is wrong. The data of a database is not stored in B-trees. The B-trees (usually B+-trees) store only keys and child pointers in inner nodes and keys and data pointers in leaf nodes. The B+-trees usually don't store data (they might do it for relation tables). The data of a database is stored in its data files which are organdized in blocks.