简体   繁体   English

为什么在MySQL中存储二进制数据?

[英]Why store binary data in MySQL?

I'm a little confused - what are the pros of storing binary data in DB? 我有点困惑 - 在DB中存储二进制数据的优点是什么? Is it for security reasons, or there are some more complicated motives i don't see? 是出于安全原因,还是有一些我看不到的更复杂的动机?

Thanks for your time. 谢谢你的时间。

As opposed to what? 与什么相反? Putting it in the filesystem? 把它放在文件系统中?

The drawbacks to using the filesystem for binary file storage would be: 使用文件系统进行二进制文件存储的缺点是:

  • you don't get ACID compliance; 你没有得到ACID合规;

  • if you might be hosting the application on more than one server (eg load-balancing, failover), you have to work out some kind of shared file storage to avoid the backend getting out of sync; 如果您可能在多个服务器上托管应用程序(例如负载平衡,故障转移),则必须设计某种共享文件存储以避免后端不同步;

  • having only one storage backend instead of two makes deployment simpler. 只有一个存储后端而不是两个存储后端使部署更简单。

So with database-only storage, you don't have to worry about creating a folder with write access for the web user, the whole app can be read-only. 因此,对于仅限数据库的存储,您不必担心为Web用户创建具有写访问权限的文件夹,整个应用程序可以是只读的。 If you need to move the application, you can just check it out of source control and point it to the database without having to copy more file data across. 如果需要移动应用程序,只需将其从源代码管理中检出并将其指向数据库,而无需复制更多文件数据。 Your database backups can be covering everything instead of having to have a separate file backup step. 您的数据库备份可以涵盖所有内容,而不必具有单独的文件备份步骤。 And so on. 等等。

On the other hand the drawbacks to database BLOB storage: 另一方面数据库BLOB存储的缺点:

  • unwieldy for very large files; 对于非常大的文件很笨重;

  • you don't get to use the web server to serve the files up efficiently for free. 您无法使用Web服务器免费有效地提供文件。

The same advantages storing anything in a database gives you 将任何内容存储在数据库中的相同优点可以为您提

  • You can index the data on another column or primary key providing fast retrieval of the data 您可以索引另一列或主键上的数据,从而快速检索数据
  • Its accessible from one location and deals with row-level concurrency issues for you 它可以从一个位置访问,并为您处理行级并发问题
  • It can appropriately cache commonly used data 它可以适当地缓存常用数据
  • You can fine tune the table engine used to store the data 您可以微调用于存储数据的表引擎
  • You can model relationships between the binary data and rows in other tables (users/pages/whatever) 您可以建模二进制数据与其他表中的行之间的关系(用户/页面/等等)

I don't see why it matters that the data is text or binary, a database gives you a ton of advantages. 我不明白为什么重要的是数据是文本或二进制,数据库给你带来了很多好处。

I'll assume the other alternative is to store binary data in a file and storing the filename instead of data in the corresponding record of the DB. 我假设另一种方法是将二进制数据存储在一个文件中,并将文件名而不是数据存储在DB的相应记录中。 The problem with that approach is that it is not consistent: anything can happen to your file, it may become damaged or deleted, and the DBMS (MySQL in this case) can't do anything about it: it cannot ensure consistency because it does not know anything about your file. 这种方法的问题在于它不一致:任何事情都可能发生在您的文件中,它可能会被损坏或删除,而DBMS(在这种情况下是MySQL)无法对它做任何事情:它无法确保一致性,因为它确实不知道你的文件。 Another thing, the DBMS can optimize access to your data (it may store it in the memory and not read it from disk every time you have to access it) while with the file-based approach, you'd have to wait until the query is completed, then open the file and read the data from disk. 另一方面,DBMS可以优化对数据的访问(它可以将其存储在内存中,而不是每次必须访问时都从磁盘读取),而使用基于文件的方法时,您必须等到查询完成后,打开文件并从磁盘读取数据。 It would reduce performance dramatically. 它会大大降低性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM