简体   繁体   English

存储二进制或图像文件的最佳方式

[英]Best way storing binary or image files

What is the best way storing binary or image files? 存储二进制文件图像文件的最佳方法是什么?

  1. Database System 数据库系统
  2. File System 文件系统

Would you please explain , why? 你能解释一下 ,为什么?

There is no real best way, just a bunch of trade offs. 没有真正最好的方法,只是一堆权衡。

Database Pros : 数据库优点
1. Much easier to deal with in a clustering environment. 1.在集群环境中处理起来容易得多。
2. No reliance on additional resources like a file server. 2.不依赖文件服务器等其他资源。
3. No need to set up "sync" operations in load balanced environment. 3.无需在负载平衡环境中设置“同步”操作。
4. Backups automatically include the files. 4.备份自动包含文件。

Database Cons : 数据库缺点
1. Size / Growth of the database. 1.数据库的大小/增长。
2. Depending on DB Server and your language, it might be difficult to put in and retrieve. 2.根据数据库服务器和您的语言,可能难以放入和检索。
3. Speed / Performance. 3.速度/性能。
4. Depending on DB server, you have to virus scan the files at the time of upload and export. 4.根据数据库服务器,您必须在上载和导出时对文件进行病毒扫描。


File Pros : 文件优点
1. For single web/single db server installations, it's fast. 1.对于单个Web /单数据库服务器安装,它很快。
2. Well understood ability to manipulate files. 2.熟悉操作文件的能力。 In other words, it's easy to move the files to a different location if you run out of disk space. 换句话说,如果磁盘空间不足,可以很容易地将文件移动到其他位置。
3. Can virus scan when the files are "at rest". 3.文件“静止”时可以扫描病毒。 This allows you to take advantage of scanner updates. 这允许您利用扫描程序更新。

File Cons : 档案缺点
1. In multi web server environments, requires an accessible share. 1.在多Web服务器环境中,需要可访问的共享。 Which should also be clustered for failover. 哪个也应该进行群集以进行故障转移。
2. Additional security requirements to handle file access. 2.处理文件访问的其他安全要求。 You have to be careful that the web server and/or share does not allow file execution. 您必须小心Web服务器和/或共享不允许文件执行。
3. Transactional Backups have to take the file system into account. 3.事务备份必须考虑文件系统。


The above said, SQL 2008 has a thing called FILESTREAM which combines both worlds. 上面说过,SQL 2008有一个名为FILESTREAM的东西,它结合了两个世界。 You upload to the database and it transparently stores the files in a directory on disk. 您上传到数据库,它将文件透明地存储在磁盘上的目录中。 When retrieving you can either pull from the database; 检索时,您可以从数据库中提取; or you can go direct to where it lives on the file system. 或者您可以直接转到它在文件系统上的位置。

Pros of Storing binary files in a DB: 在数据库中存储二进制文件的优点:

  • Some decrease in complexity since the data access layer of your system need only interface to a DB and not a DB + file system. 由于系统的数据访问层仅需要与数据库而不是DB +文件系统的接口,因此复杂性会降低一些。
  • You can secure your files using the same comprehensive permissions-based security that protects the rest of the database. 您可以使用保护数据库其余部分的基于权限的全面安全性来保护文件。
  • Your binary files are protected against loss along with the rest of your data by way of database backups. 通过数据库备份,可以保护二进制文件以及其他数据的丢失。 No separate filesystem backup system required. 不需要单独的文件系统备份系统。

Cons of Storing binary files in a DB: 在数据库中存储二进制文件的缺点:

  • Depending on size/number of files, can take up significant space potentially decreasing performance (dependening on whether your binary files are stored in a table that is queried for other content often or not) and making for longer backup times. 根据文件的大小/数量,可能占用大量空间可能会降低性能(取决于您的二进制文件是否存储在经常查询其他内容的表中)以及延长备份时间。

Pros of Storing binary files in file system: 在文件系统中存储二进制文件的优点:

  • This is what files systems are good at. 这就是文件系统擅长的。 File systems will handle defragmenting well and retrieving files (say to stream a video file to through a web server) will likely be faster that with a db. 文件系统将很好地处理碎片整理并且检索文件(比如通过Web服务器流式传输视频文件)可能比使用db更快。

Cons of Storing binary files in file system: 在文件系统中存储二进制文件的缺点:

  • Slightly more complex data access layer. 稍微复杂的数据访问层。 Needs its own backup system. 需要自己的备份系统。 Need to consider referential integrity issues (eg deleted pointer in database will need to result in deletion of file so as to not have 'orphaned' files in the filesystem). 需要考虑引用完整性问题(例如,数据库中删除的指针将需要导致文件的删除,以便在文件系统中没有'孤立'文件)。

On balance I would use the file system. 总的来说,我会使用文件系统。 In the past, using SQL Server 2005 I would simply store a 'pointer' in db tables to the binary file. 在过去,使用SQL Server 2005,我只需将db表中的“指针”存储到二进制文件中。 The pointer would typically be a GUID. 指针通常是GUID。

Here's the good news if you are using SQL Server 2008 (and maybe others - I don't know): there is built in support for a hybrid solution with the new VARBINARY(MAX) FILESTREAM data type. 如果您使用的是SQL Server 2008(也许还有其他人 - 我不知道),这是个好消息:内置支持使用新的VARBINARY(MAX)FILESTREAM数据类型的混合解决方案。 These behave logically like VARBINARY(MAX) columns but behind the scenes, SQL Sever 2008 will store the data in the file system. 这些行为在逻辑上类似于VARBINARY(MAX)列,但在幕后,SQL Sever 2008会将数据存储在文件系统中。

There is no best way. 没有最好的办法。

What? 什么? You need more info? 你需要更多信息?

There are three ways I know of... One, as byte arrays in the database. 我知道有三种方法......一种是数据库中的字节数组。 Two, as a file with the path stored in the database. 二,作为文件存储在数据库中的路径。 Three, as a hybrid (only if DB allows, such as with the FileStream type). 三,作为混合(仅当DB允许时,例如使用FileStream类型)。

The first is pretty cool because you can query and get your data in the same step. 第一个非常酷,因为您可以在同一步骤中查询和获取数据。 Which is always nice. 这总是很好。 But what happens when you have LOTS of files? 但是当你有很多文件时会发生什么? Your database gets big. 你的数据库变大了。 Now you have to deal with big database maintenance issues, such as the trials of backing up databases that are over a terabyte. 现在,您必须处理大型数据库维护问题,例如备份超过1 TB的数据库的试验。 And what happens if you need outside access to the files? 如果您需要外部访问文件会发生什么? Such as type conversions, mass manipulation (resize all images, appy watermarks, etc)? 如类型转换,大规模操作(调整所有图像大小,应用水印等)? Its much harder to do than when you have files. 比你有文件要困难得多。

The second is great for somewhat large numbers of files. 第二个对于大量文件很有用。 You can store them on NAS devices, back them up incrementally, keep your database small, etc etc. But then, when you have LOTS of files, you start running into limitations in the file system. 您可以将它们存储在NAS设备上,逐步备份它们,保持数据库小等等。但是,当您有大量文件时,就会开始遇到文件系统中的限制。 And if you spread them over the network, you get latency issues, user rights issues, etc. Also, I take pity on you if your network gets rearranged. 如果你将它们传播到网络上,就会出现延迟问题,用户权利问题等。另外,如果您的网络被重新安排,我会对您表示同情。 Now you have to run massive updates on the database to change your file locations, and I pity you if something screws up. 现在你必须在数据库上运行大量更新来改变你的文件位置,如果有什么东西搞砸了,我会很同情你。

Then there's the hybrid option. 那就是混合选项。 Its almost perfect--you can get your files via your query, yet your database isn't massive. 它几乎是完美的 - 您可以通过查询获取文件,但您的数据库并不大。 Does this solve all your problems? 这会解决你所有的问题吗? Probably not. 可能不是。 Your database isn't portable anymore; 您的数据库不再可移植; you're locked to a particular DBMS. 你被锁定在特定的DBMS上。 And this stuff isn't mature yet, so you get to enjoy the teething process. 而这些东西还不成熟,所以你可以享受出牙过程。 And who says this solves all the different issues? 谁说这解决了所有不同的问题?

Fact is, there is no "best" way. 事实是,没有“最好”的方式。 You just have to determine your requirements, make the best choice depending on them, and then suck it up when you figure out you did the wrong thing. 你只需要确定你的要求,根据它们做出最好的选择,然后当你弄清楚你做错了什么的时候把它吸了起来。

I like storing images in a database . 我喜欢将图像存储在数据库中 It makes it easy to switch from development to production just by changing databases (no copying files). 只需更改数据库(无复制文件),即可轻松从开发切换到生产。 And the database can keep track of properties like created/modified dates just as well as the File System. 并且数据库可以像文件系统一样跟踪创建/修改日期等属性。

I personally never store images IN the database for performance purposes. 我个人从不将图像存储在数据库中以达到性能目的。 In all of my sites I have a "/files" folder where I can put sub-folders based on what kind of images i'm going to store. 在我的所有网站中,我都有一个“/ files”文件夹,我可以根据我要存储的图像放置子文件夹。 Then I name them on convention. 然后我按照惯例命名它们。

For example if i'm storing a profile picture, I'll store it in "/files/profile/" as profile_2.jpg (if 2 is the ID of the account). 例如,如果我正在存储个人资料图片,我会将其存储在“/ files / profile /”中作为profile_2.jpg(如果2是帐户的ID)。 I always make it a rule to resize the image on the server to the largest size I'll need, and then smaller ones if I need them. 我总是规定将服务器上的图像调整到我需要的最大尺寸,然后再根据需要调整尺寸。 So I'd save "profile_2_thumb.jpg" and "profile_2_full.jpg". 所以我保存“profile_2_thumb.jpg”和“profile_2_full.jpg”。

By creating rules for yourself you can simply in the code call img src="/files/profile__thumb.jpg" 通过为自己创建规则,您只需在代码中调用img src =“/ files / profile__thumb.jpg”

Thats how I do it anyway! 多数民众赞成我怎么做!

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

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