在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性。在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性。SQL Server允许在某些文件损坏或离线的情况下,允许数据库依然保持部分在线,从而保证了高可用性。

 

文件和文件组

    有关文件和文件组的基本概念,有很多文章已经阐述过了。这里我只是提一下,文件组作为SQL Server访问文件的一个抽象层而存在。因此SQL Server上所做的操作不是直接针对文件,而是针对文件组。

    使用多个文件组和文件不仅仅是为了分散IO和提高性能,还有高可用性方面的原因。有关一个数据库应该包含几个文件或文件组,Paul Randal已经做过非常棒的阐述,请参阅:http://www.sqlskills.com/blogs/paul/files-and-filegroups-survey-results/

    数据库中使用多个文件或文件组在高可用性方面的好处包括:

  • 某文件的IO损坏,数据库还可以保证部分在线。
  • 将索引和表分开存放,假如索引文件不在线,数据依然可以被访问。
  • 历史数据和热数据分开,历史归档数据损坏,不影响热数据。
  • 分开数据文件使得在灾难恢复时仅仅恢复部分数据从而缩短了宕机时间
  • 数据库分为多个文件使得可以通过增加文件或移动文件的方式解决空间不足的问题

 

文件

    在SQL Server中,文件分为三类,分别为:

  • 主数据文件
  • 辅助数据文件
  • 日志文件

    其中,主数据文件默认以扩展名mdf结尾,辅助数据库文件默认以ndf结尾,日志文件以ldf结尾。虽然扩展名是可以修改的,但强烈建议不要去改扩展名。

    上面提到文件名值得是物理文件名,但是实际上在SQL Server中进行操作,操作的是逻辑文件名。

    任何时间,文件都会处于某一种状态,这些状态包括:

  • 在线
  • 离线
  • 恢复中
  • 还原挂起
  • 质疑

    可以通过sys.database_files这个DMV来查看数据库文件中包含状态在内的相关信息,如图1所示。

    SQL Server中的高可用性(2)----文件与文件组

图1.查看数据库中文件的状态等相关信息

 

   你甚至可以在数据库缺少NDF文件时附加数据库,具体细节,请参阅MCM黄大师的一篇文章:http://www.sqlnotes.info/2013/05/07/attach-database-with-missing-ndf-file/

 

文件组

    在SQL Server中,文件组中某个文件的状态决定了整个文件组的状态。但文件的状态独立于数据库的状态,比如说文件的状态是离线,但数据库依然能保证在线,这也就是所谓的数据库部分在线,举个例子,某个文件包含了名为selldata的表,如果该文件离线,但数据库在线,所有针对该selldata的表上的操作都会失败。

   如果需要数据库中的某个文件组在线,该文件组中的所有文件都应该处于在线状态。

 

表分区

    表分区是自SQL Server 2005之后出现的一个概念,我之前已经写过一篇关于表分区的文章。表分区的概念虽然很老了,但是很多地方对于表分区的使用依然处于非常初级的阶段。

    我见过大部分想到使用表分区的例子是出现性能问题,从而考虑分散大表的IO。但实际上,表分区还会提高可用性。使用表分区的好处还包括:

  • 轻松管理大表或分区
  • 提高并发性(产生分区锁,而不是表锁)
  • 以文件组级别就行备份和还原,从而仅仅只备份和还原表的一部分(比如说只备份表中的热数据),从而减少了还原时间
  • 可以仅仅在线重建某个分区

 

    值得注意的是,对表分区后,也要对表上的非聚集索引进行索引分区。否则有可能造成性能方面的例子。

 

DEMO

DEMO1 :仅重建某个分区

    下面例子是一个简单的分区表,并对索引进行分区后,仅仅重建某个分区,而不是整个索引。比如说表中按照数据冷热进行分区,可以仅仅对热数据进行重建,从而大大减少了重建索引所需的时间,如代码清单1所示。

--创建分区函数<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">CREATE</a> PARTITION <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FUNCTION&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FUNCTION</a> [t](<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=int&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">int</a>) <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">AS</a> RANGE <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=LEFT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">LEFT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FOR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FOR</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=VALUES&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">VALUES</a> (100, 500)<br/>
--分区架构<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">CREATE</a> PARTITION SCHEME [x] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">AS</a> PARTITION [t] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TO</a> ([<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=PRIMARY&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">PRIMARY</a>], [FileGroup1], [FileGroup1])

--创建表<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">CREATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TABLE</a> [Sales].[SalesOrderDetailPartition](<br/>
	[SalesOrderID] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=int&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">int</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[SalesOrderDetailID] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=int&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">int</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IDENTITY&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">IDENTITY</a>(1,1) <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[CarrierTrackingNumber] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=nvarchar&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">nvarchar</a>](25) <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[OrderQty] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=smallint&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">smallint</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[ProductID] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=int&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">int</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[SpecialOfferID] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=int&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">int</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[UnitPrice] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=money&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">money</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[UnitPriceDiscount] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=money&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">money</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[LineTotal]  <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=AS&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">AS</a> (isnull(([UnitPrice]*((1.0)-[UnitPriceDiscount]))*[OrderQty],(0.0))),<br/>
	[rowguid] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=uniqueidentifier&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">uniqueidentifier</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ROWGUIDCOL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ROWGUIDCOL</a>  <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
	[ModifiedDate] [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=datetime&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">datetime</a>] <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NOT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NOT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NULL</a>,<br/>
) <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ON</a> x(SalesOrderID)

<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INSERT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">INSERT</a> 	<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INTO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">INTO</a> [Sales].[SalesOrderDetailPartition]([SalesOrderID],<br/>
	[CarrierTrackingNumber],<br/>
	[OrderQty] ,<br/>
	[ProductID] ,<br/>
	[SpecialOfferID],<br/>
	[UnitPrice],<br/>
	[UnitPriceDiscount],ModifiedDate,rowguid)<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">SELECT</a> 	[SalesOrderID],<br/>
	[CarrierTrackingNumber],<br/>
	[OrderQty] ,<br/>
	[ProductID] ,<br/>
	[SpecialOfferID],<br/>
	[UnitPrice],<br/>
	[UnitPriceDiscount],ModifiedDate,rowguid <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FROM</a> [Sales].[SalesOrderDetail]

<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=go&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">go</a><br/>
--创建索引分区<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">CREATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=NONCLUSTERED&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">NONCLUSTERED</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INDEX&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">INDEX</a> test_partition_idx <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ON</a> [Sales].[SalesOrderDetailPartition](ProductID) <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=on&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">on</a> x(SalesOrderID)

--仅仅重建某个分区<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ALTER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ALTER</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INDEX&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">INDEX</a> test_partition_idx<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ON</a> [Sales].[SalesOrderDetailPartition]<br/>
REBUILD Partition = 1

   代码清单1.仅仅重建某个分区,而不是整个索引

 

    关于这里,更深入的文章可以参阅:http://www.mssqltips.com/sqlservertip/1621/sql-server-partitioned-tables-with-multiple-filegroups-for-high-availability/

 

DEMO2:数据库部分在线和文件还原

--创建测试数据库<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">CREATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test<br/>
 <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">GO</a><br/>
--改成完整恢复模式<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ALTER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ALTER</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SET&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">SET</a>  RECOVERY <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FULL&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FULL</a><br/>
--添加一个文件组<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ALTER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ALTER</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ADD&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ADD</a> FILEGROUP WW_GROUP<br/>
 <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">GO</a><br/>
 --向文件组中添加文件<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ALTER&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ALTER</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ADD&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ADD</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FILE</a><br/>
( NAME = ww,<br/>
FILENAME = 'D:\wwdat1.ndf',<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SIZE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">SIZE</a> = 5MB,<br/>
MAXSIZE = 100MB,<br/>
FILEGROWTH = 5MB)<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TO</a> FILEGROUP ww_Group

 --在不同文件组上分别创建两个表<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">CREATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TABLE</a> test..test ( id <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">INT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IDENTITY&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">IDENTITY</a> )<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ON</a>  [<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=primary&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">primary</a>]<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=CREATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">CREATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TABLE</a> test..test_GR ( id <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=INT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">INT</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=IDENTITY&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">IDENTITY</a> )<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=ON&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">ON</a>  ww_Group

--做完整备份<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BACKUP&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">BACKUP</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test<br/>
 <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TO</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DISK&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DISK</a>='D:\Test_backup.bak'WITH INIT

--做文件备份<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BACKUP&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">BACKUP</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FILE</a> = 'ww',<br/>
    FILEGROUP = 'ww_Group'<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TO</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DISK&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DISK</a>='D:\CROUPFILES.bak'WITH INIT<br/>
--备份日志<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BACKUP&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">BACKUP</a> LOG test<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TO</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DISK&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DISK</a>='D:\Test__log.ldf'WITH INIT

--删除文件组中的表内的数据<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TRUNCATE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TRUNCATE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TABLE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TABLE</a> test..test_GR

--还原备份,日志仅仅被应用于那个还原状态的文件<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=RESTORE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">RESTORE</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FILE</a> = 'ww',<br/>
    FILEGROUP = 'ww_Group'<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FROM</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DISK&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DISK</a> ='D:\CROUPFILES.bak'<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WITH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">WITH</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FILE</a> = 1,NORECOVERY<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=RESTORE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">RESTORE</a> LOG test<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FROM</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DISK&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DISK</a>='D:\Test__log.ldf'<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WITH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">WITH</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FILE</a> = 1, NORECOVERY

--备份尾端日志<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=BACKUP&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">BACKUP</a> LOG test<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=TO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">TO</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DISK&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DISK</a>='D:\Test__log.ldf' <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WITH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">WITH</a> NOINIT,NO_TRUNCATE<br/>
--还原尾端日志<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=RESTORE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">RESTORE</a> LOG test<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FROM</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DISK&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DISK</a>='D:\Test__log.ldf'<br/>
    <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WITH&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">WITH</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FILE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FILE</a> = 2, RECOVERY<br/>
 <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=GO&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">GO</a>

 --查看数据,删除数据的操作被成功恢复<br/>
 <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">SELECT</a>  *<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">FROM</a>    test..test_GR

--清除数据库<br/>
<a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DROP&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DROP</a> <a href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=DATABASE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99" rel="external nofollow noreferrer">DATABASE</a> test

   代码清单2.备份还原单个文件

 

    代码清单2很好的阐述了整个文件备份还原的过程,其中,在文件还原的过程中我们可以看到数据库本身是在线的,但数据库中有一个文件处于还原状态,如图2所示。

SQL Server中的高可用性(2)----文件与文件组

图2.ww文件处于还原中状态

 

   此时对于表test_GR做操作的话,会提示因文件组没有联机而失败,如图3所示。

SQL Server中的高可用性(2)----文件与文件组

图3.因为文件组没有联机而导致操作失败

 

    再对尾端日志进行备份还原之后,该文件组成功上线。

 

小结