In this series of the SQL Server FILESTREAM (see TOC at bottom), We have gone through various aspects of this feature to store large size objects into the file systems.

在本系列SQL Server FILESTREAM(请参见底部的TOC)中,我们介绍了此功能的各个方面,以将大型对象存储到文件系统中。

In the previous SQL Server FILESTREAM articles, we have covered the following benefits from this feature:

在以前SQL Server FILESTREAM文章中,我们介绍了此功能的以下优点:

  • The benefit of NTFS data streaming NTFS数据流的好处
  • Less overhead on SQL Server for the large BLOB objects 大型BLOB对象在SQL Server上的开销更少
  • Backup includes the metadata along with the FILESTREAM container data 备份包括元数据以及FILESTREAM容器数据
  • FILESTREAM also allows Point-in-time restore for the database FILESTREAM还允许对数据库进行时间点还原
  • FILESTREAM provides the transactional consistency also which is the primary requirement of any database FILESTREAM还提供事务一致性,这也是任何数据库的主要要求

Now let’s review a few more features of SQL Server FILESTREAM.

现在,让我们回顾一下SQL Server FILESTREAM的其他功能。

FILESTREAM数据库容器 (FILESTREAM database Container)

We cannot use the FILESTREAM container for another database. This restriction also applies to the subfolder of the FILESTREAM container. In the previous article, we used the file system ‘ C:\sqlshack\Demo’ for the sample database.

我们不能将FILESTREAM容器用于另一个数据库。 此限制也适用于FILESTREAM容器的子文件夹。 在上一篇文章中,我们将文件系统'C:\ sqlshack \ Demo'用于示例数据库。

Database Name

FILESTREAM Container

(file system location)

Remarks

FileStreamDemodatabase_test

C:\sqlshack\Demo

It works fine.

FileStreamDemodatabase_test _New

C:\sqlshack\Demo

Error: Since other database is using this location.

FileStreamDemodatabase_test _New

C:\sqlshack\Demo\New

Error: we cannot use the child folder as well. In this case, the parent folder is being used by the FILESTREAM database ‘DemoSQL.’

数据库名称

FILESTREAM容器

(文件系统位置)

备注

FileStreamDemodatabase_test

C:\ sqlshack \ Demo

它工作正常。

FileStreamDemodatabase_test _New

C:\ sqlshack \ Demo

错误:由于其他数据库正在使用此位置。

FileStreamDemodatabase_test _New

C:\ sqlshack \ Demo \ New

错误:我们也不能使用子文件夹。 在这种情况下,FILESTREAM数据库'DemoSQL'将使用父文件夹。

检查数据库中是否启用了FILESTREAM (Checking whether FILESTREAM is enabled or not in database)

We can check whether the SQL Server FILESTREAM feature at database level using the filegroup.

我们可以使用文件组在数据库级别检查SQL Server FILESTREAM功能。

Use FileStreamDemodatabase_test
Go
SELECT  *  FROM  sys.filegroups

The SQL Server FILESTREAM filegroup type is ‘FD’ therefore we can check the property using the above command, or we can use the print statement to give the output.

SQL Server FILESTREAM文件组的类型为“ FD”,因此我们可以使用上述命令检查属性,也可以使用print语句提供输出。

IF EXISTS ( SELECT  *  FROM    sys.filegroups   WHERE   type = 'FD' )
BEGIN        PRINT 'FILESTREAM Filegroup Exists for the database; you can check the Physical file location'+@Physicalfilename   END
ELSE
BEGIN
PRINT 'FILESTREAM Filegroup does not exist for this database'END

If I run this query on the FILESTREAM database, we get the below output.

如果我在FILESTREAM数据库上运行此查询,则会得到以下输出。

Otherwise, we get the output below.

否则,我们将获得以下输出。

使用SQL Server FILESTREAM功能检查表是否可以保存FILESTREAM数据 (Checking whether the table can hold FILESTREAM data using SQL Server FILESTREAM feature)

We must have a UNIQUEIDENTIFIER column with ROWGUIDCOL property in the table to hold the FILESTREAM data. You can check this using join system tables and views.

我们必须在表中具有一个带有ROWGUIDCOL属性的UNIQUEIDENTIFIER列,以保存FILESTREAM数据。 您可以使用联接系统表和视图进行检查。

IF EXISTS ( SELECT  *  FROM    sys.key_constraints sc
INNER JOIN sys.indexes si
ON sc.unique_index_id = si.index_id
AND si.object_id = OBJECT_ID('DemoFileStreamTable_1')
INNER JOIN
( SELECT * ,                                   COUNT(*)
OVER ( PARTITION BY index_id,                                                   object_id )
AS ColCount                                 FROM   sys.index_columns ) ic
ON ic.index_id = si.index_id
AND ic.object_id = si.object_id
INNER JOIN sys.columns c
ON c.column_id = ic.column_id
AND c.object_id = ic.object_id
AND c.is_rowguidcol = 1 AND c.is_nullable = 0
WHERE   is_unique_constraint = 1 OR ( is_primary_key = 1                          AND ColCount = 1                        ))
BEGIN
PRINT 'Specified table can have FILESTREAM columns; it is having '     END
ELSE      BEGIN
PRINT 'You should modify the table to have UNIQUEIDENTIFIER column, currently it is not ready to have FILESTREAM columns'     END

We have included the table name in this query in which SQL Server FILESTREAM data needs to be added.

我们在此查询中包括了表名,其中需要添加SQL Server FILESTREAM数据。

If the table does not meet the requirements, we get the following output:

如果表不符合要求,我们将获得以下输出:

修改现有的VARBINARY(MAX)列以保存FILESTREAM数据 (Modifying existing VARBINARY(MAX) column to hold FILESTREAM data)

We might have a requirement to change the existing Varbinary(max) column to a FILESTREAM column. It might be due to the old versions of SQL Server (older than 2008) does not support FILESTREAM. Therefore, if we want to convert the existing column to FILESTREAM, we require to copy the data (objects) into the file system container. We cannot directly modify the table property to reflect these changes.

我们可能需要将现有的Varbinary(max)列更改为FILESTREAM列。 可能是由于SQL Server的旧版本(早于2008)不支持FILESTREAM。 因此,如果要将现有列转换为FILESTREAM,则需要将数据(对象)复制到文件系统容器中。 我们无法直接修改table属性以反映这些更改。

Suppose we have a table ‘customers’, which also holds customer images into the varbinary(Max) column.

假设我们有一个“客户”表,该表还将客户图像保存在varbinary(Max)列中。

We need to perform the below steps to change this.

我们需要执行以下步骤来更改此设置。

  • ALTER TABLE [dbo].[customers] ADD Image_temp VARBINARY(MAX) FILESTREAM NULL
    
  • UPDATE [dbo].[customers] SET  ItemImage_New = Imagedata
    
  • ALTER TABLE [dbo].[Items]  DROP COLUMN ItemImage
    
  • EXECUTE sp_rename      @objname = '[dbo].[customers].ItemImage_New ',   @newname = 'Imagedata',       @objtype = 'COLUMN'
    

使用表名称标识SQL Server FILESTREAM文件组名称 (Identifying the SQL Server FILESTREAM filegroup name using the table name)

We might want to know the FILESTREAM filegroup name in which the particular table belongs. We can do it using the join condition between catalog view sys.data_spaces and sys.tables. The catalog view (sys.data_spaces) shows the row for each data space such as filegroup, partition.

我们可能想知道特定表所属的FILESTREAM文件组名称。 我们可以使用目录视图sys.data_spaces和sys.tables之间的连接条件来做到这一点。 目录视图(sys.data_spaces)显示每个数据空间(例如文件组,分区)的行。

Now join this catalog view with sys.tables to get the detail of filegroup belonging to a table.

现在,将此目录视图与sys.tables结合在一起,以获取属于表的文件组的详细信息。

In the above image, we can see that the table belongs to ‘DemoFileStreamFILESTREAM filegroup.

在上图中,我们可以看到该表属于'DemoFileStreamFILESTREAM文件组。

SQL Server FILESTREAM表的触发器 (Triggers for SQL Server FILESTREAM tables )

We can create the DML triggers for the FILESTREAM columns as well in the table. Suppose we want to create a trigger for the update or insert activity notification any insert occurs in the FILESTREAM table using the AFTER INSERT trigger.

我们也可以在表中为FILESTREAM列创建DML触发器。 假设我们想为更新或插入活动通知创建一个触发器,使用AFTER INSERT触发器在FILESTREAM表中发生任何插入。

Execute the below code to create an AFTER INSERT trigger.

执行以下代码以创建AFTER INSERT触发器。

DECLARE @File varbinary(MAX);
SELECT
@File = CAST(
bulkcolumn as varbinary(max)
)
FROM
OPENROWSET(BULK 'C:\WideWorldImporters-Full.bak', SINGLE_BLOB) as MyData; INSERT INTO DemoFileStreamTable_1
VALUES
(
NEWID(),
'Installation file SQL',
@File
)

Now insert any FILESTREAM object in the table.

现在,在表中插入任何FILESTREAM对象。

DECLARE @File varbinary(MAX);
SELECT
@File = CAST(
bulkcolumn as varbinary(max)
)
FROM
OPENROWSET(BULK 'C:\WideWorldImporters-Full.bak', SINGLE_BLOB) as MyData; INSERT INTO DemoFileStreamTable_1
VALUES
(
NEWID(),
'Installation file SQL',
@File
)

In the output, we get the message about the size of the inserted object.

在输出中,我们得到有关插入对象大小的消息。

Similarly, we can create AFTER UPDATE trigger and display the required message. For example, in the trigger below, we defined a message to print for every update in the FILESTREAM object.

同样,我们可以创建AFTER UPDATE触发器并显示所需的消息。 例如,在下面的触发器中,我们定义了一条消息,以针对FILESTREAM对象中的每次更新进行打印。

CREATE TRIGGER TGR_FS_Update ON DemoFileStreamTable_1
AFTER UPDATE
AS
BEGIN
DECLARE @Filesize INT         SELECT  @Filesize = DATALENGTH([FILE])
FROM    INSERTED
PRINT   'The updated size of the object in FILESTREAM is :' + STR(@Filesize)
END

Now, we are going to replace the existing file with a new file so the trigger will give the updated size of the FILESTREAM object.

现在,我们将用新文件替换现有文件,以便触发器将提供FILESTREAM对象的更新大小。

UPDATE DemoFileStreamTable_1
SET [File] = (SELECT *
FROM OPENROWSET(
BULK 'C:\AdventureWorks2017.bak',
SINGLE_BLOB) AS Document)
WHERE fileid = '8662F1FA-8918-48D7-B752-AFBC0392902C'
GO

Once the update completes for the FILESTREAM table, we get the below message printed.

FILESTREAM表的更新完成后,我们将打印以下消息。

多个FILESTREAM文件组 (Multiple FILESTREAM filegroups)

We might be using the FILESTREAM for a large number of objects and might be big files such as videos. In the database, we usually create multiple secondary data files on different disks to split the load from a single disk or data store. The question may arise here whether SQL Server allows creating multiple FILESTREAM filegroups. We can create multiple FILESTREAM filegroups due to the disk space limitation. We can also create multiple filegroups to get the benefits of the multiple disks IO read and write latency and performance.

我们可能将FILESTREAM用于大量对象,并且可能是大文件,例如视频。 在数据库中,我们通常在不同的磁盘上创建多个辅助数据文件,以将负载从单个磁​​盘或数据存储区中分离出来。 这里可能会出现问题,即SQL Server是否允许创建多个FILESTREAM文件组。 由于磁盘空间的限制,我们可以创建多个FILESTREAM文件组。 我们还可以创建多个文件组,以获得多个磁盘IO读写延迟和性能的好处。

You can notice in the below that we created the multiple FILESTREAM filegroups.

您可以在下面注意到我们创建了多个FILESTREAM文件组。

CREATE DATABASE [FS_MultipleFG]ON  PRIMARY
( NAME = N'FS_MultipleFG', FILENAME = N'C:\MS\FS_MultipleFG.mdf' , SIZE = 8192KB , FILEGROWTH = 65536KB ), FILEGROUP [FILESTREAM_1] CONTAINS FILESTREAM DEFAULT
( NAME = N'FS_FG_1', FILENAME = N'C:\MS\FS_FG_1' ), FILEGROUP [FILESTREAM_2] CONTAINS FILESTREAM
( NAME = N'FS_FG_2', FILENAME = N'C:\NPE\FS_FG_2' )LOG ON
( NAME = N'FS_MultipleFG_log', FILENAME = N'C:\MS\FS_MultipleFG_log.ldf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
GO

Execute the above code and open the database properties from SSMS. In the ‘Filegroups’ page, you can see two FILESTREAM filegroups and defined ‘FILESTREAM_1’ as default FILESTREAM filegroup.

执行上面的代码,然后从SSMS中打开数据库属性。 在“文件组”页面中,您可以看到两个FILESTREAM文件组,并将“ FILESTREAM_1”定义为默认的FILESTREAM文件组。

In the database files, we created multiple FILESTREAM files in the different filegroups.

在数据库文件中,我们在不同的文件组中创建了多个FILESTREAM文件。

We have multiple FILESTREAM filegroups in this database. Therefore, we need to define the FILESTREAM filegroup while creating the table. If we do not specify any filegroup, it uses the default FILESTREAM filegroup (in this example FILESTREAM_1) to create the object.

在此数据库中,我们有多个FILESTREAM文件组。 因此,我们在创建表时需要定义FILESTREAM文件组。 如果我们未指定任何文件组,它将使用默认的FILESTREAM文件组(在本示例中为FILESTREAM_1)创建对象。

Let us create the tables to use the default FILESTREAM filegroup and specific FILESTREAM filegroup.

让我们创建表以使用默认的FILESTREAM文件组和特定的FILESTREAM文件组。

  • Default filegroup: in the below command, we did not specify any FILESTREAM filegroup.默认文件组:在以下命令中,我们未指定任何FILESTREAM文件组。

    CREATE TABLE [DemoFileStreamTable_1] (
    [FileId] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE,
    [FileName] VARCHAR (25),
    [File] VARBINARY (MAX) FILESTREAM);
    GO
    

    In the below image, we can see SQL Server created the table in the default FILESTREAM filegroup.

    在下图中,我们可以看到SQL Server在默认的FILESTREAM文件组中创建了表。

  • Object creation in specific FILESTREAM filegroup: In the below code, we instructed SQL Server to create the table in the’ FILESTREAM_2′ filegroup. 在特定的FILESTREAM文件组中创建对象 :在下面的代码中,我们指示SQL Server在“ FILESTREAM_2”文件组中创建表。
    CREATE TABLE [DemoFileStreamTable_2] ([FileId] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE,[FileName] VARCHAR (25),[File] VARBINARY (MAX) FILESTREAM)FILESTREAM_ON FILESTREAM_2GO
    

结论 (Conclusion)

In this article, we explored various aspects the SQL Server FILESTREAM feature available from SQL Server 2008 onwards. I will continue writing on this topic to complete this series. Stay tuned for the next article.

在本文中,我们从SQL Server 2008开始探讨了SQL Server FILESTREAM功能的各个方面。 我将继续就该主题进行写作以完成本系列。 请继续关注下一篇文章。

目录 (Table of contents)

FILESTREAM in SQL Server
Managing data with SQL Server FILESTREAM tables
SQL Server FILESTREAM Database backup overview
Restoring a SQL Server FILESTREAM enabled database
SQL Server FILESTREAM database recovery scenarios
Working with SQL Server FILESTREAM – Adding columns and moving databases
SQL Server FILESTREAM internals overview
Importing SQL Server FILESTREAM data with SSIS packages
SQL Server FILESTREAM queries and Filegroups
Viewing SQL Server FILESTREAM data with SSRS
SQL Server FILESTREAM Database Corruption and Remediation
Export SQL Server FILESTREAM Objects with PowerShell and SSIS
SQL FILESTREAM and SQL Server Full Text search
SQL Server FILESTREAM and Replication
SQL Server FILESTREAM with Change Data Capture
Transaction log backups in a SQL FILESTREAM database
SQL FILESTREAM Compatibility with Database Snapshot, Mirroring, TDE and Log Shipping
SQL Server FILETABLE – the next generation of SQL FILESTREAM
Managing Data in SQL Server FILETABLEs
SQL Server FILETABLE Use Cases
SQL Server中的文件流
使用SQL Server FILESTREAM表管理数据
SQL Server FILESTREAM数据库备份概述
还原启用了SQL Server FILESTREAM的数据库
SQL Server FILESTREAM数据库恢复方案
使用SQL Server FILESTREAM –添加列和移动数据库
SQL Server FILESTREAM内部概述
使用SSIS包导入SQL Server FILESTREAM数据
SQL Server FILESTREAM查询和文件组
使用SSRS查看SQL Server FILESTREAM数据
SQL Server FILESTREAM数据库损坏和修复
使用PowerShell和SSIS导出SQL Server FILESTREAM对象
SQL FILESTREAM和SQL Server全文搜索
SQL Server FILESTREAM和复制
具有更改数据捕获功能SQL Server FILESTREAM
SQL FILESTREAM数据库中的事务日志备份
SQL FILESTREAM与数据库快照,镜像,TDE和日志传送的兼容性
SQL Server FILETABLE –下一代SQL FILESTREAM
在SQL Server FILETABLEs中管理数据
SQL Server FILETABLE用例

翻译自: https://www.sqlshack.com/sql-server-filestream-queries-and-filegroups/

SQL Server FILESTREAM查询和文件组相关推荐

  1. SQL Server中的数据库文件组和零碎还原

    So far, we discussed many de-facto details about SQL Server database backup and restore. In this 15t ...

  2. SQL Server 数据库中添加文件组和数据文件

    SQL Server 现有数据库中添加文件组和数据文件use CURRENT_DB --进入当前操作数据库 go alter database CURRENT_DB add filegroup FG1 ...

  3. 使用SSIS包导入SQL Server FILESTREAM数据

    初始配置 (Initial configuration) We have been exploring the SQL Server FILESTREAM feature in this ongoin ...

  4. SQL Server FILESTREAM数据库损坏和修复

    This article will cover corruption and recovery scenarios in the context of SQL Server FILESTREAM in ...

  5. 如何使用 SQL Server FILESTREAM 存储非结构化数据?这篇文章告诉你!

    作者 | ALEN İBRIÇ 译者 | 火火酱,责编 | Carol 封图 | CSDN 付费下载于视觉中国 在本文中,我将解释如何使用SQL Server FILESTREAM来存储非结构化数据. ...

  6. 如何使用 SQL Server FILESTREAM 存储非结构化数据?

    作者 | ALEN İBRIÇ 译者 | 火火酱,责编 | Carol 封图 | CSDN 付费下载于视觉中国 在本文中,我将解释如何使用SQL Server FILESTREAM来存储非结构化数据. ...

  7. sql server只读_如何在SQL Server 2016中为可用性组配置只读路由

    sql server只读 The SQL Server Always On Availability Groups concept was introduced the first time in S ...

  8. SQL Server中的即时文件初始化概述

    This article gives an overview of Instant File Initialization and its benefits for SQL Server databa ...

  9. 数据库-SQL Server数据库查询速度慢(连接超时)原因及优化方法

    SQL Server数据库查询速度慢的原因有很多,常见的有以下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列 ...

最新文章

  1. 一些js代码,自己备用的。高手不要笑话我。。(跨浏览器基础事件,浏览器检测,判断浏览器的名称、版本号、操作系统)...
  2. Leangoo看板Jenkins配置指南
  3. Linux ENSP 搭建DHCP服务器并实现中继和Linux 下搭建DNS服务器(2个实验可跟做)
  4. access vba 常量数组赋值_聊聊 VBA 数组的那些坑
  5. php提示是否运行,php运行错误提示
  6. python123系统基本信息获取_Python运维-获取当前操作系统的各种信息
  7. 机器学习基础(四十四)—— 优化
  8. Calendar中add()和roll()函数的用法
  9. java 请求https post 接口 绕过证书验证
  10. Excel 2010 VBA 入门 010 VBE编辑器的工具栏
  11. latex normal是几号字_Latex之字体 | 学步园
  12. 云计算 原理与实践期末复习
  13. excel统计每个单元格内的单词及空格的个数
  14. Sql Server 存储过程 循环处理字符串的每个字符
  15. 计算机电源功率计算器,装机不用愁 航嘉功率计算器教你选电源
  16. 学猫叫歌词计算机,学猫叫歌词 小峰峰(陈峰)、小潘潘(潘柚彤)_晴格歌词网
  17. 洛谷2002 消息扩散
  18. LoRa开发7:PingPong系统
  19. SparkStreaming实时计算pv和uv,注意事项以及生产问题
  20. 数字图像处理(2)正交变换

热门文章

  1. 一个程序如何连接到外网_如何从头开始开发一个微信小程序
  2. 移动网络安全_新突破!青海移动在2020年中国移动网络安全运维技能竞赛大赛中荣获三等奖!...
  3. rms 公式 有效值_有效值是电流电压的均方根值吗?
  4. python程序语法元素的描述_Python入门——Python程序语法元素
  5. OO第二单元作业分析
  6. 13. 星际争霸之php设计模式--正面模式
  7. 字节跳动2019春招笔试——找零(JavaScript)
  8. 解决VScode在保存less文件时,自动生成对应的css文件以及安装Easy less之后,计算式子不显示结果的问题
  9. 作业帮电脑版在线使用_应届生应聘作业帮的在线辅导老师
  10. CentOS系统安装Java