简体   繁体   English

SQL Server 2008 - 存储文件

[英]SQL Server 2008 - Storing Files

I am building a MVC3 web app against a SQL Server 2008 database. 我正在构建一个针对SQL Server 2008数据库的MVC3 Web应用程序。 The web app will allow users to upload photos and documents. 该网络应用程序将允许用户上传照片和文档。

Currently the files are stored in a column of type "image". 目前,文件存储在“image”类型的列中。

Is this an okay thing to do, or is it an outdated approach? 这是一件好事,还是过时的做法?

Is there any advantages in moving storage to another data type or start using FILESTREAM? 将存储移动到另一种数据类型或开始使用FILESTREAM有什么好处吗?

Stats - 500 (users) x 7 (avg documents each) x 2MB (avg doc size) 统计 - 500(用户)x 7(每个平均文件)x 2MB(平均文件大小)

EDIT 1 编辑1

Can anyone comment; 谁能评论; is what I'm doing currently--storing as "image" data type--bad? 是我目前正在做的 - 存储为“图像”数据类型 - 糟糕?

You should store the images on the filesystem. 您应该将图像存储在文件系统上。 See this thread: Storing Images in DB - Yea or Nay? 看到这个主题: 在DB中存储图像 - 是或不是?

As for the documents, are they just static files the user uploads/downloads, or will you search for content within the documents? 至于文档,它们只是用户上传/下载的静态文件,还是在文档中搜索内容? Do you need to do anything special with the documents? 你需要对文件做些什么特别的事吗?

(Moving my comments to an answer) (将我的评论转移到答案)

From MSDN : ntext , text , and image data types will be removed in a future version of Microsoft SQL Server. MSDN将在Microsoft SQL Server的未来版本中删除 ntext textimage数据类型。 Avoid using these data types in new development work, and plan to modify applications that currently use them. 避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。 Use nvarchar(max), varchar(max), and varbinary(max) instead. 请改用nvarchar(max),varchar(max)和varbinary(max)。

It's "bad" if you care about the data type's planned obsolescence. 如果您关心数据类型的计划过时,那就太“糟糕”了。 It sounds like varbinary(max) or even filestream are the preferred data types going forward. 听起来像varbinary(max)甚至文件filestream是未来的首选数据类型。

Storing files in the database vs. file system is a separate debate that everyone else seems to be jumping on instead. 将文件存储在数据库与文件系统之间是一个单独的争论,其他人似乎都在跳跃。

您可能想要在SQL SERVER 2008中查看新的FILESTREAM数据类型。阅读链接 - 它将为您提供一些指导,以确定这是否是适当的存储类型。

There's a really good paper by Microsoft Research called To Blob or Not To Blob . 微软研究院发表了一篇非常好的论文,名为To Blob或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 如果您的图片或文档的大小通常低于256K,则将它们存储在数据库VARBINARY列中会更有效

  • 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) 如果您的图片或文档的大小通常超过1 MB,则将它们存储在文件系统中会更有效(并且使用SQL Server 2008的FILESTREAM属性,它们仍处于事务控制之下并且是数据库的一部分)

  • 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. 如果您决定将图片放入SQL Server表中,我强烈建议您使用单独的表来存储这些图片 - 不要将员工foto存储在employee表中 - 将它们保存在单独的表中。 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. 这样,员工表可以保持精简,平均且非常高效,假设您并不总是需要选择员工foto作为查询的一部分。

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". 我们称之为“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: 现在,无论何时创建需要存储VARCHAR(MAX)或VARBINARY(MAX)列的新表,都可以为大数据指定此文件组:

 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! 查看文件组的MSDN简介,并使用它!

In my experience it is better to store your files on disk and store a path to the file in your DB. 根据我的经验,最好将文件存储在磁盘上并存储数据库中文件的路径。 This frees up your DB to focus on the stuff it should be doing rather than file storage. 这样可以释放数据库,专注于它应该做的事情,而不是文件存储。

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

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