Pro & Cons of storing files(pictures) in a SQL Server for a website

余生颓废 提交于 2019-12-20 03:07:30

问题


I'm creating an Asp.Net MVC website.

I've in the past, for heavy application, multiple layer application, used the database to store files.

But now I'm questioning myself, is this a good idea for a website? In a performance view?

It has several pros to me:

  • Allows me to control easily if the connected user has the right to display the image(Required for my project)
  • Permits to be sure that we have consistent data(otherwise we can have an existing file but no info in the database and the opposite
  • I need a fail-over webserver, and those files will be imported from a third server, so if those files are in the database, I only need to have a working ASP.Net website and a replicated database on the failover server, no need to sync files.

But it has some cons too:

  • There SOME big files(it's a minority, but it will happen), like 100-200MB, and I'm not sure it's good to have this kind of file in a database?(it's more like a question ;) )
  • I'm not sure that it will have good performances?

What do you think? Is this reasonable? I searched on the Internet, but I didn't found some kinds of arguments for website. My question is mostly about FILESTREAM VS FILESYSTEM, I'm sure that FileStream is slower, but a lot? Because if it's only some percent, the gain of functionality worth it.


回答1:


If the files are integral and actively changing part of the system and them have to be backed up along with the other data - you can store them inside the DB, but try to use the FILESTREAM fields if you use sql server 2005+ and your files are big enough - say 500k+

If the files are static content, you can store them outside with only pointers in DB. This not prevents to take into account all your custom permissions machine.

Storing and working with files inside DB is usually slower, than in filesystem, but all depends on your needs.




回答2:


There's a really good paper by Microsoft Research called To Blob or Not To Blob.

Their conclusion after a large number of performance tests and analysis is this:

  • if your pictures or document are typically below 256K in size, storing them in a database VARBINARY column is more efficient

  • if your pictures or document are typically over 1 MB in size, storing them in the filesystem is more efficient (and with SQL Server 2008's FILESTREAM attribute, they're still under transactional control and part of the database)

  • in between those two, it's a bit of a toss-up depending on your use

If you decide to put your pictures into a SQL Server table, I would strongly recommend using a separate table for storing those pictures - do not store the employee foto in the employee table - keep them in a separate table. That way, the Employee table can stay lean and mean and very efficient, assuming you don't always need to select the employee foto, too, as part of your queries.

For filegroups, check out Files and Filegroup Architecture for an intro. Basically, you would either create your database with a separate filegroup for large data structures right from the beginning, or add an additional filegroup later. Let's call it "LARGE_DATA".

Now, whenever you have a new table to create which needs to store VARCHAR(MAX) or VARBINARY(MAX) columns, you can specify this file group for the large data:

 CREATE TABLE dbo.YourTable
     (....... define the fields here ......)
     ON Data                   -- the basic "Data" filegroup for the regular data
     TEXTIMAGE_ON LARGE_DATA   -- the filegroup for large chunks of data

Check out the MSDN intro on filegroups, and play around with it!




回答3:


Why use db instead of txt files? Because its faster it uses indexes. Storing whole files in db is never a good practice. Use db as index (pointers) to the normal img files.

As far as your pros / cons go:

  • you can easily controll if the user has the right to see the image if you display the image with asp / php and set root image folder outside web root

  • storing whole files in db is cca 10 times slower (I know for mysql testing but its similar for mssql - http://blog.sitek.com.au/2008/03/comparison-between-storing-imagesfiles-in-mysql-and-on-filesystem/)

  • if you have files in db you wont be able to use CDNs (http://en.wikipedia.org/wiki/Content_delivery_network)



来源:https://stackoverflow.com/questions/8739188/pro-cons-of-storing-filespictures-in-a-sql-server-for-a-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!