There might be several questions about this already but I'd like to get an updated answer since most answer are old now. Which will be better in storage of images, database or filesystem?
可能有几个问题已经存在,但我想得到一个更新的答案,因为现在大多数答案已经过时了。哪个在存储图像,数据库或文件系统方面会更好?
I think facebook is using database because images can be in various extensions :| See this links for example: jpeg jpg png gif
我认为facebook正在使用数据库,因为图像可以有各种扩展名:|请参阅此链接,例如:jpeg jpg png gif
5 个解决方案
#1
2
Although it may seem so, this is not a matter of taste, but a performance problem. I have experimented both ways, for projects that are now alive and kicking, and learnt my lesson during their development period.
虽然看起来似乎如此,但这不是品味问题,而是性能问题。我已经尝试了两种方式,对于现在活着和踢的项目,并在开发期间吸取了教训。
Storing files in database may seem a nice way to create the all-in-one setup that is easy to deploy, maintain etc. But when it comes to larger files (e.g. hi-res images), it may significantly increase you loading time.
将文件存储在数据库中似乎是创建易于部署,维护等一体化设置的好方法。但是当涉及到较大的文件(例如高分辨率图像)时,它可能会显着增加您的加载时间。
On the other hand, speaking of smaller images, as user avatars, product thumbnails, other thumbnails, it may increase your loading speed, by making fewer requests to the server's file system. In this specific case, it all depends on the SQL skill you may have and the optimization level of your queries.
另一方面,谈到较小的图像,作为用户头像,产品缩略图,其他缩略图,它可以通过减少对服务器文件系统的请求来提高加载速度。在这种特定情况下,这一切都取决于您可能具有的SQL技能和查询的优化级别。
I will give you a suggestion, which I have used myself by now in projects involving many images, and not only those.
我会给你一个建议,我现在用它来处理涉及许多图像的项目,而不仅仅是那些。
Create a table in your database, with roughly the following structure:
在数据库中创建一个表,大致具有以下结构:
id INT(5) AUTO_INCREMENT PRIMARY KEY
filename VARCHAR(255)
fullsize_path CHAR(255)
image BLOB
mime CHAR(25)
size INT(20)
Err... you get the idea, the BLOB field is where your image is going. In case it's a thumbnail for something, the fullsize_path will not be left empty and mention the path to the fully sized image.
呃...你明白了,BLOB字段就是你的图像。如果它是某个缩略图,则fullsize_path不会留空并提及完整大小图像的路径。
In this way, when displaying, for example, a page with products, all the queries will be SQL-based, but when a specific product is accessed, the fullsize_path will tell the browser where the big brother can be found.
这样,当显示例如带有产品的页面时,所有查询都将基于SQL,但是当访问特定产品时,fullsize_path将告诉浏览器可以找到大哥的位置。
Of course, you have more things to worry about when deploying something like this, and I will outline a few here:
当然,在部署这样的东西时你还有更多的事情需要担心,我将在这里概述一些:
- SQL server performance
- SQL服务器性能
- SQL-hosted images' maximum size (in bytes)
- SQL托管图像的最大大小(以字节为单位)
- The amount of images to be stored
- 要存储的图像数量
Of course, doing some performance tests before is an invaluable practice!
当然,之前进行一些性能测试是非常宝贵的做法!
#2
1
I would recommend storing the files in an images/ folder or something like that on your website, and I would save their name with a random number or id and then store that information in a database. That would be a nice combo of both!
我建议将文件存储在图像/文件夹或类似的网站上,我会用随机数或ID保存他们的名字,然后将这些信息存储在数据库中。这将是两者的一个很好的组合!
#3
0
I have done systems using both ways of storage before and it usually depends on the context.
我之前使用两种存储方式完成了系统,它通常取决于上下文。
For systems accessing large number of files with low server count (usually 1 server), store in files are easier to develop and maintain.
对于访问服务器数量较少的大量文件(通常是1个服务器)的系统,存储文件更容易开发和维护。
For systems with high server count, and all servers need access to the files. some times i set it up with files and rsync. and some times i store the file in the database and let replication handle it.
对于服务器数量较多的系统,所有服务器都需要访问这些文件。有时我用文件和rsync设置它。有时我将文件存储在数据库中,让复制处理它。
The reason Facebook or other sites store their images in databases is most likely because of that. they need the files to be accessed in a lot of servers without needing th overhead of copying the file around.
Facebook或其他网站将其图像存储在数据库中的原因很可能是因为这一点。他们需要在很多服务器*问文件,而不需要复制文件的开销。
#4
0
Storing images in a database is much secure than storing data in file system. But in performance wise it is much efficient to store images in a file system. We have to choose the best way according to our requirements.
将图像存储在数据库中比将数据存储在文件系统中要安全得多。但是在性能方面,将图像存储在文件系统中是非常有效的。我们必须根据我们的要求选择最佳方式。
#5
0
You may want to take a look at the Filetable option as well. Pluses are simpler programming model (can use T-SQL and Windows API) and simpler management (via SSMS), minuses are that this makes the database larger. If you pick this option, you will want to place the filetables on a dedicated filegroup(s) and you will want to adjust your backup strategy to include filegroup backups.
您可能还想查看Filetable选项。优点是更简单的编程模型(可以使用T-SQL和Windows API)和更简单的管理(通过SSMS),因为这会使数据库变大。如果选择此选项,则需要将文件表放在专用文件组上,并且需要调整备份策略以包括文件组备份。
Another possibility is to take a look at RBS (remote BLOB store), which shipped as part of the SQL Server 2012 Feature Pack (not in the main product).
另一种可能性是查看RBS(远程BLOB存储),它作为SQL Server 2012 Feature Pack的一部分提供(不在主产品中)。
#1
2
Although it may seem so, this is not a matter of taste, but a performance problem. I have experimented both ways, for projects that are now alive and kicking, and learnt my lesson during their development period.
虽然看起来似乎如此,但这不是品味问题,而是性能问题。我已经尝试了两种方式,对于现在活着和踢的项目,并在开发期间吸取了教训。
Storing files in database may seem a nice way to create the all-in-one setup that is easy to deploy, maintain etc. But when it comes to larger files (e.g. hi-res images), it may significantly increase you loading time.
将文件存储在数据库中似乎是创建易于部署,维护等一体化设置的好方法。但是当涉及到较大的文件(例如高分辨率图像)时,它可能会显着增加您的加载时间。
On the other hand, speaking of smaller images, as user avatars, product thumbnails, other thumbnails, it may increase your loading speed, by making fewer requests to the server's file system. In this specific case, it all depends on the SQL skill you may have and the optimization level of your queries.
另一方面,谈到较小的图像,作为用户头像,产品缩略图,其他缩略图,它可以通过减少对服务器文件系统的请求来提高加载速度。在这种特定情况下,这一切都取决于您可能具有的SQL技能和查询的优化级别。
I will give you a suggestion, which I have used myself by now in projects involving many images, and not only those.
我会给你一个建议,我现在用它来处理涉及许多图像的项目,而不仅仅是那些。
Create a table in your database, with roughly the following structure:
在数据库中创建一个表,大致具有以下结构:
id INT(5) AUTO_INCREMENT PRIMARY KEY
filename VARCHAR(255)
fullsize_path CHAR(255)
image BLOB
mime CHAR(25)
size INT(20)
Err... you get the idea, the BLOB field is where your image is going. In case it's a thumbnail for something, the fullsize_path will not be left empty and mention the path to the fully sized image.
呃...你明白了,BLOB字段就是你的图像。如果它是某个缩略图,则fullsize_path不会留空并提及完整大小图像的路径。
In this way, when displaying, for example, a page with products, all the queries will be SQL-based, but when a specific product is accessed, the fullsize_path will tell the browser where the big brother can be found.
这样,当显示例如带有产品的页面时,所有查询都将基于SQL,但是当访问特定产品时,fullsize_path将告诉浏览器可以找到大哥的位置。
Of course, you have more things to worry about when deploying something like this, and I will outline a few here:
当然,在部署这样的东西时你还有更多的事情需要担心,我将在这里概述一些:
- SQL server performance
- SQL服务器性能
- SQL-hosted images' maximum size (in bytes)
- SQL托管图像的最大大小(以字节为单位)
- The amount of images to be stored
- 要存储的图像数量
Of course, doing some performance tests before is an invaluable practice!
当然,之前进行一些性能测试是非常宝贵的做法!
#2
1
I would recommend storing the files in an images/ folder or something like that on your website, and I would save their name with a random number or id and then store that information in a database. That would be a nice combo of both!
我建议将文件存储在图像/文件夹或类似的网站上,我会用随机数或ID保存他们的名字,然后将这些信息存储在数据库中。这将是两者的一个很好的组合!
#3
0
I have done systems using both ways of storage before and it usually depends on the context.
我之前使用两种存储方式完成了系统,它通常取决于上下文。
For systems accessing large number of files with low server count (usually 1 server), store in files are easier to develop and maintain.
对于访问服务器数量较少的大量文件(通常是1个服务器)的系统,存储文件更容易开发和维护。
For systems with high server count, and all servers need access to the files. some times i set it up with files and rsync. and some times i store the file in the database and let replication handle it.
对于服务器数量较多的系统,所有服务器都需要访问这些文件。有时我用文件和rsync设置它。有时我将文件存储在数据库中,让复制处理它。
The reason Facebook or other sites store their images in databases is most likely because of that. they need the files to be accessed in a lot of servers without needing th overhead of copying the file around.
Facebook或其他网站将其图像存储在数据库中的原因很可能是因为这一点。他们需要在很多服务器*问文件,而不需要复制文件的开销。
#4
0
Storing images in a database is much secure than storing data in file system. But in performance wise it is much efficient to store images in a file system. We have to choose the best way according to our requirements.
将图像存储在数据库中比将数据存储在文件系统中要安全得多。但是在性能方面,将图像存储在文件系统中是非常有效的。我们必须根据我们的要求选择最佳方式。
#5
0
You may want to take a look at the Filetable option as well. Pluses are simpler programming model (can use T-SQL and Windows API) and simpler management (via SSMS), minuses are that this makes the database larger. If you pick this option, you will want to place the filetables on a dedicated filegroup(s) and you will want to adjust your backup strategy to include filegroup backups.
您可能还想查看Filetable选项。优点是更简单的编程模型(可以使用T-SQL和Windows API)和更简单的管理(通过SSMS),因为这会使数据库变大。如果选择此选项,则需要将文件表放在专用文件组上,并且需要调整备份策略以包括文件组备份。
Another possibility is to take a look at RBS (remote BLOB store), which shipped as part of the SQL Server 2012 Feature Pack (not in the main product).
另一种可能性是查看RBS(远程BLOB存储),它作为SQL Server 2012 Feature Pack的一部分提供(不在主产品中)。