在 C++ 中删除部分文件的最快方法
我想知道在 C++ 中删除部分文件的最快方法是什么。
我知道编写第二个文件并跳过你想要的部分的方法。但我认为当你处理大文件时速度很慢。
数据库系统怎么样,它们如何如此快速地删除记录?
I wonder which is the fastest way to erase part of a file in c++.
I know the way of write a second file and skip the part you want. But i think is slow when you work with big files.
What about database system, how they remove records so fast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据库保留索引,元数据列出文件的哪些部分有效,哪些部分无效。要删除数据,只需更新索引以将该部分标记为无效,而主文件内容根本不必更改。
A database keeps an index, with metadata listing which parts of the file are valid and which aren't. To delete data, just the index is updated to mark that section invalid, and the main file content doesn't have to be changed at all.
数据库系统通常只是将已删除的记录标记为已删除,而不物理地恢复未使用的空间。他们稍后可以重新使用已删除记录占用的空间。这就是为什么他们可以快速删除数据库的某些部分。
快速删除文件部分的能力取决于您要删除的文件部分。如果要删除的文件部分位于文件末尾,则可以使用操作系统调用简单地截断该文件。
从中间删除文件的一部分可能会很耗时。您的选择是向前移动文件的其余部分,或者将整个文件复制到新位置,跳过已删除的部分。对于大文件来说,任何一种方法都可能非常耗时。
Database systems typically just mark deleted records as deleted, without physically recovering the unused space. They may later reuse the space occupied by deleted records. That's why they can delete parts of a database quickly.
The ability to quickly delete a portion of a file depends on the portion of the file you wish to delete. If the portion of the file that you are deleting is at the end of the file, you can simply truncate the file, using OS calls.
Deleting a portion of a file from the middle is potentially time consuming. Your choice is to either move the remainder of the file forward, or to copy the entire file to a new location, skipping the deleted portion. Either way could be time consuming for a large file.
我知道的最快的方法是将数据文件作为持久内存映射文件打开,然后简单地移动不需要的部分。比移动到第二个文件要快,但对于大文件来说仍然不太快。
The fastest way I know is to open data file as a Persisted memory-mapped file and simple move over the part you don't need. Would be faster than moving to second file but still not too fast with big files.