MySQL blob:如何获取存储数据的子集
我想使用 MYSQL 作为大量文件的存储系统。 我只想读/写列中存储的数据的一部分(数据存储为字节),因此我不必将整个文件加载到应用程序中(因为它可以大于 GB)。 因此,简而言之,我希望在 blob 列中进行随机读/写访问,而不将整个数据加载到内存中。 是否有可用的函数来执行这些操作?谢谢。
I would like to use MYSQL as a storage system for a huge number of files.
I would like to read/write just a portion of the data stored in a column (data is stored as bytes) so I don't have to load the entire file into the application (because it can be > than a GB).
So, in brief, I would like to have random read/write access in a blob column without loading the entire data into memory.
Are there functions available to perform these operations? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL 将 blob 与字符串(或多或少)相同:
所以所有常见的字符串函数都适用于 blob。特别是,您可以使用
substring
只抓取 Blob 的一部分。也就是说,将多 GB 数据文件作为 BLOB 存储在关系数据库中并不是最好的做法。您最好将文件的元数据存储在数据库中,并将文件本身保留在文件系统中;文件系统非常擅长管理文件,关系数据库擅长处理结构化数据。
MySQL treats blobs the same as strings (more or less):
So all the usual string functions work on blobs. In particular, you can use
substring
to grab just part of of a blob.That said, storing a multi-gigabyte data file in a relational database as a BLOB isn't the best thing to do. You'd be better off storing the file's metadata in the database and leaving the file itself in the file system; file systems are pretty good at managing files, relational databases are good at handling structured data.
你可以尝试这个方法。将文件的元数据(如路径、名称等)存储在数据库中并将文件存储在目录下。
您可以从数据库中获取文件路径并以随机访问模式读取文件。使用文件偏移量,您可以获得所需的存储数据子集。
You can try this approach. Store the meta data of your files (like path, name, etc.) in the database and store the files under a directory.
From the database you can fetch the filepath and the read the file in random access mode. Using the file-offset you can get the required subset of the stored data.
您可以使用例如 MID() [1] 来剪切 BLOB 的部分;虽然我更喜欢将文件存储在文件系统中,而不是数据库中。 MySQL 在 BLOB 上的性能相当差。
[1]
http://dev.mysql.com/doc/refman /5.1/en/string-functions.html#function_mid
You could use e.g. MID() [1] to cut portions of the BLOB; though I would prefer to store files in the file system, not in a database. MySQL performs rather poor on BLOBs.
[1]
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_mid