Вы находитесь на странице: 1из 53

Table of Contents

Overview
Limitations and blocking issues
Get Started
Identify databases and tables
Run the wizard
Configure compatible features
How To
Enable
Enable a database
Enable a table
Select rows to migrate
Manage
Monitor migration
Pause and resume migration
Manage and troubleshoot
Back up
Restore
Disable and bring back data
Extended events
Resources
Pricing
Blog
Service updates
Videos
Stretch Database
4/12/2017 • 4 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
Stretch Database migrates your cold data transparently and securely to the Microsoft Azure cloud.
If you just want to get started with Stretch Database right away, see Get started by running the Enable Database for
Stretch Wizard.

What are the benefits of Stretch Database?


Stretch Database provides the following benefits:
Provides cost-effective availability for cold data
Stretch warm and cold transactional data dynamically from SQL Server to Microsoft Azure with SQL Server Stretch
Database. Unlike typical cold data storage, your data is always online and available to query. You can provide
longer data retention timelines without breaking the bank for large tables like Customer Order History. Benefit
from the low cost of Azure rather than scaling expensive, on-premises storage. You choose the pricing tier and
configure settings in the Azure Portal to maintain control over price and costs. Scale up or down as needed. Visit
SQL Server Stretch Database Pricing for details.
Doesn’t require changes to queries or applications
Access your SQL Server data seamlessly regardless of whether it’s on-premises or stretched to the cloud. You set
the policy that determines where data is stored, and SQL Server handles the data movement in the background.
The entire table is always online and queryable. And, Stretch Database doesn’t require any changes to existing
queries or applications – the location of the data is completely transparent to the application.
Streamlines on-premises data maintenance
Reduce on-premises maintenance and storage for your data. Backups for your on-premises data run faster and
finish within the maintenance window. Backups for the cloud portion of your data run automatically. Your on-
premises storage needs are greatly reduced. Azure storage can be 80% less expensive than adding to on-premises
SSD.
Keeps your data secure even during migration
Enjoy peace of mind as you stretch your most important applications securely to the cloud. SQL Server’s Always
Encrypted provides encryption for your data in motion. Row Level Security (RLS) and other advanced SQL Server
security features also work with Stretch Database to protect your data.

What does Stretch Database do?


After you enable Stretch Database for a SQL Server instance, a database, and at least one table, Stretch Database
silently begins to migrate your cold data to Azure.
If you store cold data in a separate table, you can migrate the entire table.
If your table contains both hot and cold data, you can specify a filter function to select the rows to migrate.
You don't have to change existing queries and client apps. You continue to have seamless access to both
local and remote data, even during data migration. There is a small amount of latency for remote queries, but you
only encounter this latency when you query the cold data.
Stretch Database ensures that no data is lost if a failure occurs during migration. It also has retry logic to
handle connection issues that may occur during migration. A dynamic management view provides the status of
migration.
You can pause data migration to troubleshoot problems on the local server or to maximize the available
network bandwidth.

Is Stretch Database for you?


If you can make the following statements, Stretch Database may help to meet your requirements and solve your
problems.

IF YOU'RE A DECISION MAKER IF YOU'RE A DBA

I have to keep transactional data for a long time. The size of my tables is getting out of control.

Sometimes I have to query the cold data. My users say that they want access to cold data, but they only
rarely use it.

I have apps, including older apps, that I don’t want to update. I have to keep buying and adding more storage.

I want to find a way to save money on storage. I can’t backup or restore such large tables within the SLA.

What kind of databases and tables are candidates for Stretch Database?
Stretch Database targets transactional databases with large amounts of cold data, typically stored in a small
number of tables. These tables may contain more than a billion rows.
If you use the temporal table feature of SQL Server, use Stretch Database to migrate all or part of the associated
history table to cost-effective storage in Azure. For more info, see Manage Retention of Historical Data in System-
Versioned Temporal Tables.
Use Stretch Database Advisor, a feature of SQL Server 2016 Upgrade Advisor, to identify databases and tables for
Stretch Database. For more info, see Identify databases and tables for Stretch Database by running Stretch
Database Advisor. To learn more about potential blocking issues, see Limitations for Stretch Database.

Test drive Stretch Database


Test drive Stretch Database with the AdventureWorks sample database. To get the AdventureWorks sample
database, download at least the database file and the samples and scripts file from here. After you restore the
sample database to an instance of SQL Server 2016, unzip the samples file and open the Stretch DB Samples file
from the Stretch DB folder. Run the scripts in this file to check the space used by your data before and after you
enable Stretch Database, to track the progress of data migration, and to confirm that you can continue to query
existing data and insert new data both during and after data migration.

Next step
Identify databases and tables that are candidates for Stretch Database. Download SQL Server 2016
Upgrade Advisor and run the Stretch Database Advisor to identify databases and tables that are candidates for
Stretch Database. Stretch Database Advisor also identifies blocking issues. For more info, see Identify databases
and tables for Stretch Database by running Stretch Database Advisor.
Limitations for Stretch Database
4/12/2017 • 1 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
Learn about limitations for Stretch-enabled tables, and about limitations that currently prevent you from enabling
Stretch for a table.

Limitations for Stretch-enabled tables


Stretch-enabled tables have the following limitations.
Constraints
Uniqueness is not enforced for UNIQUE constraints and PRIMARY KEY constraints in the Azure table that
contains the migrated data.
DML operations
You can't UPDATE or DELETE rows that have been migrated, or rows that are eligible for migration, in a
Stretch-enabled table or in a view that includes Stretch-enabled tables.
You can't INSERT rows into a Stretch-enabled table on a linked server.
Indexes
You can't create an index for a view that includes Stretch-enabled tables.
Filters on SQL Server indexes are not propagated to the remote table.

Limitations that currently prevent you from enabling Stretch for a table
The following items currently prevent you from enabling Stretch for a table.
Table properties
Tables that have more than 1,023 columns or more than 998 indexes
FileTables or tables that contain FILESTREAM data
Tables that are replicated, or that are actively using Change Tracking or Change Data Capture
Memory-optimized tables
Data types
text, ntext and image
timestamp
sql_variant
XML
CLR data types including geometry, geography, hierarchyid, and CLR user-defined types
Column types
COLUMN_SET
Computed columns
Constraints
Default constraints and check constraints
Foreign key constraints that reference the table. In a parent-child relationship (for example, Order and
Order_Detail), you can enable Stretch for the child table (Order_Detail) but not for the parent table (Order).
Indexes
Full text indexes
XML indexes
Spatial indexes
Indexed views that reference the table

See Also
Identify databases and tables for Stretch Database by running Stretch Database Advisor
Enable Stretch Database for a database
Enable Stretch Database for a table
Stretch Database databases and tables - Stretch
Database Advisor
4/12/2017 • 1 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To identify databases and tables that are candidates for Stretch Database, download SQL Server 2016 Upgrade
Advisor and run the Stretch Database Advisor. Stretch Database Advisor also identifies blocking issues.

Download and install Upgrade Advisor


Download and install Upgrade Advisor from here. This tool is not included on the SQL Server 2016 installation
media.

Run the Stretch Database Advisor


1. Run Upgrade Advisor.
2. Select Scenarios, and then select RUN STRETCH DATABASE ADVISOR.
3. On the Run Stretch Database Advisor blade, click SELECT DATABASES TO ANALYZE.
4. On the Select databases blade, enter or select the server name and the authentication info. Click Connect.
5. A list of databases on the selected server appears. Select the databases that you want to analyze. Click
Select.
6. On the Run Stretch Database Advisor blade, click Run. The analysis runs.

Review the results


1. When the analysis is finished, on the Analyzed databases blade, select one of the databases that you
analyzed to display the Analysis results blade.
The Analysis results blade lists recommended tables in the selected database that match the default
recommendation criteria.
2. In the list of tables on the Analysis results blade, select one of the recommended tables to display the
Table results blade.
If there are blocking issues, the Table results blade lists the blocking issues for the selected table. For
information about blocking issues detected by Stretch Database Advisor, see Limitations for Stretch
Database.
3. In the list of blocking issues on the Table results blade, select one of the issues to display more info about
the selected issue and proposes mitigation steps. Implement the suggested mitigation steps if you want to
configure the selected table for Stretch Database.

Next step
Enable Stretch Database.
To enable Stretch Database on a database, see Enable Stretch Database for a database.
To enable Stretch Database on another table, when Stretch is already enabled on the database, see Enable
Stretch Database for a table.

See Also
Limitations for Stretch Database
Enable Stretch Database for a database
Enable Stretch Database for a table
Get started by running the Enable Database for
Stretch Wizard
4/12/2017 • 6 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To configure a database for Stretch Database, run the Enable Database for Stretch Wizard. This topic describes the
info that you have to enter and the choices that you have to make in the wizard.
To learn more about Stretch Database, see Stretch Database.

NOTE
Later, if you disable Stretch Database, remember that disabling Stretch Database for a table or for a database does not
delete the remote object. If you want to delete the remote table or the remote database, you have to drop it by using the
Azure management portal. The remote objects continue to incur Azure costs until you delete them manually.

Launch the wizard


1. In SQL Server Management Studio, in Object Explorer, select the database on which you want to enable
Stretch.
2. Right-click and select Tasks, and then select Stretch, and then select Enable to launch the wizard.

Introduction
Review the purpose of the wizard and the prerequisites.
The important prerequisites include the following.
You have to be an administrator to change database settings.
You have to have a Microsoft Azure subscription.
Your SQL Server has to be able to communicate with the remote Azure server.
Select tables
Select the tables that you want to enable for Stretch.
Tables with lots of rows appear at the top of the sorted list. Before the Wizard displays the list of tables, it analyzes
them for data types that are not currently supported by Stretch Database.

COLUMN DESCRIPTION

(no title) Check the check box in this column to enable the selected
table for Stretch.

Name Specifies the name of the table in the database.

(no title) A symbol in this column may represent a warning that doesn't
prevent you from enabling the selected table for Stretch. It
may also represent a blocking issue that prevents you from
enabling the selected table for Stretch - for example, because
the table uses an unsupported data type. Hover over the
symbol to display more info in a tooltip. For more info, see
Limitations for Stretch Database.
COLUMN DESCRIPTION

Stretched Indicates whether the table is already enabled for Stretch.

Migrate You can migrate an entire table (Entire Table) or you can
specify a filter on an existing column in the table. If you want
to use a different filter function to select rows to migrate, run
the ALTER TABLE statement to specify the filter function after
you exit the wizard. For more info about the filter function, see
Select rows to migrate by using a filter function. For more info
about how to apply the function, see Enable Stretch Database
for a table or ALTER TABLE (Transact-SQL).

Rows Specifies the number of rows in the table.

Size (KB) Specifies the size of the table in KB.

Optionally provide a row filter


If you want to provide a filter function to select rows to migrate, do the following things on the Select tables page.
1. In the Select the tables you want to stretch list, click Entire Table in the row for the table. The Select
rows to stretch dialog box opens.

2. In the Select rows to stretch dialog box, select Choose Rows.


3. In the Name field, provide a name for the filter function.
4. For the Where clause, pick a column from the table, pick an operator, and provide a value.
5. Click Check to test the function. If the function returns results from the table - that is, if there are rows to
migrate that satisfy the condition - the test reports Success.

NOTE
The textbox that displays the filter query is read-only. You can't edit the query in the textbox.
1. Click Done to return to the Select tables page.
The filter function is created in SQL Server only when you finish the wizard. Until then, you can return to the Select
tables page to change or rename the filter function.

If you want to use a different type of filter function to select rows to migrate, do one of the following things.
Exit the wizard and run the ALTER TABLE statement to enable Stretch for the table and to specify a filter
function. For more info, see Enable Stretch Database for a table.
Run the ALTER TABLE statement to specify a filter function after you exit the wizard. For the required steps,
see Add a filter function after running the Wizard.

Configure Azure
1. Sign in to Microsoft Azure with a Microsoft account.

2. Select the existing Azure subscription to use for Stretch Database.

NOTE
To enable Stretch on a database you must have administrator rights to the subscription you are using. Stretch Database
wizard will only show subscriptions where the user has administrator rights.

1. Select the Azure region to use for Stretch Database.


If you create a new server, the server is created in this region.
If you have existing servers in the selected region, the wizard lists them when you choose Existing
server.
To minimize latency, pick the Azure region in which your SQL Server is located. For more info about
regions, see Azure Regions.
2. Specify whether you want to use an existing server or create a new Azure server.
If the Active Directory on your SQL Server is federated with Azure Active Directory, you can optionally use a
federated service account for SQL Server to communicate with the remote Azure server. For more info
about the requirements for this option, see ALTER DATABASE SET Options (Transact-SQL).
Create new server
a. Create a login and password for the server administrator.
b. Optionally, use a federated service account for SQL Server to communicate with the remote
Azure server.

Existing server
a. Select the existing Azure server.
b. Select the authentication method.
If you select SQL Server Authentication, provide the administrator login and
password.
Select Active Directory Integrated Authentication to use a federated service
account for SQL Server to communicate with the remote Azure server. If the selected
server is not integrated with Azure Active Directory, this option doesn't appear.
Secure credentials
You have to have a database master key to secure the credentials that Stretch Database uses to connect to the
remote database.
If a database master key already exists, enter the password for it.

If the database does not have an existing master key, enter a strong password to create a database master key.
For more info about the database master key, see CREATE MASTER KEY (Transact-SQL) and Create a Database
Master Key. For more info about the credential that the wizard creates, see CREATE DATABASE SCOPED
CREDENTIAL (Transact-SQL).

Select IP address
Use the subnet IP address range (recommended), or the public IP address of your SQL Server, to create a firewall
rule on Azure that lets SQL Server communicate with the remote Azure server.
The IP address or addresses that you provide on this page tell the Azure server to allow incoming data, queries,
and management operations initiated by SQL Server to pass through the Azure firewall. The wizard doesn't change
anything in the firewall settings on the SQL Server.

Summary
Review the values that you entered and the options that you selected in the wizard and the estimated costs on
Azure. Then select Finish to enable Stretch.
Results
Review the results.
To monitor the status of data migration, see Monitor and troubleshoot data migration (Stretch Database).
Troubleshooting the wizard
The Stretch Database wizard failed.
If Stretch Database is not yet enabled at the server level, and you run the wizard without the system administrator
permissions to enable it, the wizard fails. Ask the system administrator to enable Stretch Database on the local
server instance, and then run the wizard again. For more info, see Prerequisite: Permission to enable Stretch
Database on the server.

Next steps
Enable additional tables for Stretch Database. Monitor data migration and manage Stretch-enabled databases and
tables.
Enable Stretch Database for a table to enable additional tables.
Monitor and troubleshoot data migration (Stretch Database) to see the status of data migration.
Pause and resume data migration (Stretch Database)
Manage and troubleshoot Stretch Database
Backup Stretch-enabled databases
Restore Stretch-enabled databases

See Also
Enable Stretch Database for a database
Enable Stretch Database for a table
Configure compatible SQL Server features with
Stretch Database
4/12/2017 • 1 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
Take simple steps to configure the following SQL Server features to work with Stretch Database.
Always On
Always Encrypted
Transparent Data Encryption (TDE)
Temporal tables

Configure Always On with Stretch Database


If you're using Always On with Stretch Database, you have to make sure that the database master key is available
on the secondary replicas. Stretch Database uses the database master key to secure the credentials that it uses to
connect to the remote Azure database.
After you set up the Always On availability group, run the stored procedure sp_control_dbmasterkey_password
on each secondary replica and provide the password for the Stretch-enabled database. For more info and examples,
see sp_control_dbmasterkey_password.

Configure Always Encrypted with Stretch Database


If you want to use Always Encrypted and Stretch Database together, you have to configure encryption on the
selected columns before you enable Stretch Database on the table.
If you have already enabled Stretch Database on the table, and you want to use Always Encrypted columns, you
have to do the following things.
1. Disable Stretch Database on the table and bring the remote data back from Azure. For more info, see Disable
Stretch Database and bring back remote data.
2. Configure Always Encrypted on the selected columns.
3. Re-enable Stretch Database on the table. For more info, see Enable Stretch Database for a database.

Configure Transparent Data Encryption (TDE) with Stretch Database


If TDE is enabled on your local database, it will not be automatically enabled on the Stretch database remote
endpoint. You must remember to enable TDE on the remote endpoint after enabling Stretch on your database.

Configure temporal tables with Stretch Database


If you're using temporal tables, you can enable Stretch Database on the history table, but not on the current table.
For guidance about using temporal tables with Stretch Database, see Manage Retention of Historical Data in
System-Versioned Temporal Tables.
To filter rows to migrate from the history table by using a sliding window, see Select rows to migrate by using a
filter function.
Enable Stretch Database for a database
4/12/2017 • 5 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To configure an existing database for Stretch Database, select Tasks | Stretch | Enable for a database in SQL
Server Management Studio to open the Enable Database for Stretch wizard. You can also use Transact-SQL to
enable Stretch Database for a database.
If you select Tasks | Stretch | Enable for an individual table, and you have not yet enabled the database for
Stretch Database, the wizard configures the database for Stretch Database and lets you select tables as part of the
process. Follow the steps in this topic instead of the steps in Enable Stretch Database for a table.
Enabling Stretch Database on a database or a table requires db_owner permissions. Enabling Stretch Database on
a database also requires CONTROL DATABASE permissions.

NOTE
Later, if you disable Stretch Database, remember that disabling Stretch Database for a table or for a database does not
delete the remote object. If you want to delete the remote table or the remote database, you have to drop it by using the
Azure management portal. The remote objects continue to incur Azure costs until you delete them manually.

Before you get started


Before you configure a database for Stretch, we recommend that you run the Stretch Database Advisor to
identify databases and tables that are eligible for Stretch. The Stretch Database Advisor also identifies
blocking issues. For more info, see Identify databases and tables for Stretch Database by running Stretch
Database Advisor.
Review Limitations for Stretch Database.
Stretch Database migrates data to Azure . Therefore you have to have an Azure account and a subscription
for billing. To get an Azure account, click here.
Have the connection and login info you need to create a new Azure server or to select an existing Azure
server.

Prerequisite: Enable Stretch Database on the server


Before you can enable Stretch Database on a database or a table, you have to enable it on the local server. This
operation requires sysadmin or serveradmin permissions.
If you have the required administrative permissions, the Enable Database for Stretch wizard configures
the server for Stretch .
If you don't have the required permissions, an administrator has to enable the option manually by running
sp_configure before you run the wizard, or an administrator has to run the wizard.
To enable Stretch Database on the server manually, run sp_configure and turn on the remote data
archive option. The following example enables the remote data archive option by setting its value to 1.
EXEC sp_configure 'remote data archive' , '1';
GO

RECONFIGURE;
GO

For more info, see Configure the remote data archive Server Configuration Option and sp_configure (Transact-
SQL).

Use the wizard to enable Stretch Database on a database


For info about the Enable Database for Stretch Wizard, including the info that you have to enter and the choices
that you have to make, see Get started by running the Enable Database for Stretch Wizard.

Use Transact-SQL to enable Stretch Database on a database


Before you can enable Stretch Database on individual tables, you have to enable it on the database.
Enabling Stretch Database on a database or a table requires db_owner permissions. Enabling Stretch Database on
a database also requires CONTROL DATABASE permissions.
1. Before you begin, choose an existing Azure server for the data that Stretch Database migrates, or create a
new Azure server.
2. On the Azure server, create a firewall rule with the IP address range of the SQL Server that lets SQL Server
communicate with the remote server.
You can easily find the values you need and create the firewall rule by attempting to connect to the Azure
server from Object Explorer in SQL Server Management Studio (SSMS). SSMS helps you to create the rule
by opening the following dialog box which already includes the required IP address values.

3. To configure a SQL Server database for Stretch Database, the database has to have a database master key.
The database master key secures the credentials that Stretch Database uses to connect to the remote
database. Here's an example that creates a new database master key.

USE <database>;
GO

CREATE MASTER KEY ENCRYPTION BY PASSWORD='<password>';


GO

For more info about the database master key, see CREATE MASTER KEY (Transact-SQL) and Create a
Database Master Key.
4. When you configure a database for Stretch Database, you have to provide a credential for Stretch Database
to use for communication between the on premises SQL Server and the remote Azure server. You have two
options.
You can provide an administrator credential.
If you enable Stretch Database by running the wizard, you can create the credential at that
time.
If you plan to enable Stretch Database by running ALTER DATABASE, you have to create the
credential manually before you run ALTER DATABASE to enable Stretch Database.
Here's an example that creates a new credential.

CREATE DATABASE SCOPED CREDENTIAL <db_scoped_credential_name>


WITH IDENTITY = '<identity>' , SECRET = '<secret>' ;
GO

For more info about the credential, see CREATE DATABASE SCOPED CREDENTIAL (Transact-SQL).
Creating the credential requires ALTER ANY CREDENTIAL permissions.
You can use a federated service account for the SQL Server to communicate with the remote Azure
server when the following conditions are all true.
The service account under which the instance of SQL Server is running is a domain account.
The domain account belongs to a domain whose Active Directory is federated with Azure
Active Directory.
The remote Azure server is configured to support Azure Active Directory authentication.
The service account under which the instance of SQL Server is running must be configured as
a dbmanager or sysadmin account on the remote Azure server.
5. To configure a database for Stretch Database, run the ALTER DATABASE command.
a. For the SERVER argument, provide the name of an existing Azure server, including the
.database.windows.net portion of the name - for example,
MyStretchDatabaseServer.database.windows.net .

b. Provide an existing administrator credential with the CREDENTIAL argument, or specify


FEDERATED_SERVICE_ACCOUNT = ON. The following example provides an existing credential.

ALTER DATABASE <database name>


SET REMOTE_DATA_ARCHIVE = ON
(
SERVER = '<server_name>' ,
CREDENTIAL = <db_scoped_credential_name>
) ;
GO

Next steps
Enable Stretch Database for a table to enable additional tables.
Monitor and troubleshoot data migration (Stretch Database) to see the status of data migration.
Pause and resume data migration (Stretch Database)
Manage and troubleshoot Stretch Database
Backup Stretch-enabled databases
Restore Stretch-enabled databases

See Also
Identify databases and tables for Stretch Database by running Stretch Database Advisor
ALTER DATABASE SET Options (Transact-SQL)
Enable Stretch Database for a table
4/12/2017 • 4 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To configure a table for Stretch Database, select Stretch | Enable for a table in SQL Server Management Studio
to open the Enable Table for Stretch wizard. You can also use Transact-SQL to enable Stretch Database on an
existing table, or to create a new table with Stretch Database enabled.
If you store cold data in a separate table, you can migrate the entire table.
If your table contains both hot and cold data, you can specify a filter function to select the rows to migrate.
Prerequisites. If you select Stretch | Enable for a table, and you have not yet enabled Stretch Database
for the database, the wizard first configures the database for Stretch Database. Follow the steps in Get
started by running the Enable Database for Stretch Wizard instead of the steps in this topic.
Permissions. Enabling Stretch Database on a database or a table requires db_owner permissions.
Enabling Stretch Database on a table also requires ALTER permissions on the table.

NOTE
Later, if you disable Stretch Database, remember that disabling Stretch Database for a table or for a database does
not delete the remote object. If you want to delete the remote table or the remote database, you have to drop it
by using the Azure management portal. The remote objects continue to incur Azure costs until you delete them
manually.

Use the wizard to enable Stretch Database on a table


Launch the wizard
1. In SQL Server Management Studio, in Object Explorer, select the table on which you want to enable Stretch.
1. Right-click and select Stretch, and then select Enable to launch the wizard.
Introduction
Review the purpose of the wizard and the prerequisites.
Select database tables
Confirm that the table you want to enable is displayed and selected.
You can migrate an entire table or you can specify a simple filter function in the wizard. If you want to use
a different type of filter function to select rows to migrate, do one of the following things.
Exit the wizard and run the ALTER TABLE statement to enable Stretch for the table and to specify a filter
function.
Run the ALTER TABLE statement to specify a filter function after you exit the wizard. For the required
steps, see Add a filter function after running the Wizard.
The ALTER TABLE syntax is described later in this topic.
Summary
Review the values that you entered and the options that you selected in the wizard. Then select Finish to
enable Stretch.
Results
Review the results.

Use Transact-SQL to enable Stretch Database on a table


You can enable Stretch Database for an existing table or create a new table with Stretch Database enabled by
using Transact-SQL.
Options
Use the following options when you run CREATE TABLE or ALTER TABLE to enable Stretch Database on a table.
Optionally, use the FILTER_PREDICATE = <function> clause to specify a function to select rows to migrate if
the table contains both hot and cold data. The predicate must call an inline table-valued function. For
more info, see Select rows to migrate by using a filter function. If you don't specify a filter function, the
entire table is migrated.

IMPORTANT
If you provide a filter function that performs poorly, data migration also performs poorly. Stretch Database applies
the filter function to the table by using the CROSS APPLY operator.

Specify MIGRATION_STATE = OUTBOUND to start data migration immediately or MIGRATION_STATE = PAUSED to


postpone the start of data migration.
Enable Stretch Database for an existing table
To configure an existing table for Stretch Database, run the ALTER TABLE command.
Here's an example that migrates the entire table and begins data migration immediately.

USE <Stretch-enabled database name>;


GO
ALTER TABLE <table name>
SET ( REMOTE_DATA_ARCHIVE = ON ( MIGRATION_STATE = OUTBOUND ) ) ;
GO

Here's an example that migrates only the rows identified by the dbo.fn_stretchpredicate inline table-valued
function and postpones data migration. For more info about the filter function, see Select rows to migrate by
using a filter function.

USE <Stretch-enabled database name>;


GO
ALTER TABLE <table name>
SET ( REMOTE_DATA_ARCHIVE = ON (
FILTER_PREDICATE = dbo.fn_stretchpredicate(),
MIGRATION_STATE = PAUSED ) ) ;
GO

For more info, see ALTER TABLE (Transact-SQL).


Create a new table with Stretch Database enabled
To create a new table with Stretch Database enabled, run the CREATE TABLE command.
Here's an example that migrates the entire table and begins data migration immediately.
USE <Stretch-enabled database name>;
GO
CREATE TABLE <table name>
( ... )
WITH ( REMOTE_DATA_ARCHIVE = ON ( MIGRATION_STATE = OUTBOUND ) ) ;
GO

Here's an example that migrates only the rows identified by the dbo.fn_stretchpredicate inline table-valued
function and postpones data migration. For more info about the filter function, see Select rows to migrate by
using a filter function.

USE <Stretch-enabled database name>;


GO
CREATE TABLE <table name>
( ... )
WITH ( REMOTE_DATA_ARCHIVE = ON (
FILTER_PREDICATE = dbo.fn_stretchpredicate(),
MIGRATION_STATE = PAUSED ) ) ;
GO

For more info, see CREATE TABLE (Transact-SQL).

See Also
ALTER TABLE (Transact-SQL)
CREATE TABLE (Transact-SQL)
Select rows to migrate by using a filter function
(Stretch Database)
4/12/2017 • 13 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
If you store cold data in a separate table, you can configure Stretch Database to migrate the entire table. If your
table contains both hot and cold data, on the other hand, you can specify a filter predicate to select the rows to
migrate. The filter predicate is an inline table-valued function. This topic describes how to write an inline table-
valued function to select rows to migrate.

IMPORTANT
If you provide a filter function that performs poorly, data migration also performs poorly. Stretch Database applies the filter
function to the table by using the CROSS APPLY operator.

If you don't specify a filter function, the entire table is migrated.


When you run the Enable Database for Stretch Wizard, you can migrate an entire table or you can specify a simple
filter function in the wizard. If you want to use a different type of filter function to select rows to migrate, do one
of the following things.
Exit the wizard and run the ALTER TABLE statement to enable Stretch for the table and to specify a filter
function.
Run the ALTER TABLE statement to specify a filter function after you exit the wizard.
The ALTER TABLE syntax for adding a function is described later in this topic.

Basic requirements for the filter function


The inline table-valued function required for a Stretch Database filter predicate looks like the following example.

CREATE FUNCTION dbo.fn_stretchpredicate(@column1 datatype1, @column2 datatype2 [, ...n])


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE <predicate>

The parameters for the function have to be identifiers for columns from the table.
Schema binding is required to prevent columns that are used by the filter function from being dropped or altered.
Return value
If the function returns a non-empty result, the row is eligible to be migrated. Otherwise - that is, if the function
doesn't return a result - the row is not eligible to be migrated.
Conditions
The <predicate> can consist of one condition, or of multiple conditions joined with the AND logical operator.
<predicate> ::= <condition> [ AND <condition> ] [ ...n ]

Each condition in turn can consist of one primitive condition, or of multiple primitive conditions joined with the
OR logical operator.

<condition> ::= <primitive_condition> [ OR <primitive_condition> ] [ ...n ]

Primitive conditions
A primitive condition can do one of the following comparisons.

<primitive_condition> ::=
{
<function_parameter> <comparison_operator> constant
| <function_parameter> { IS NULL | IS NOT NULL }
| <function_parameter> IN ( constant [ ,...n ] )
}

Compare a function parameter to a constant expression. For example, @column1 < 1000 .
Here's an example that checks whether the value of a date column is < 1/1/2016.

CREATE FUNCTION dbo.fn_stretchpredicate(@column1 datetime)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '1/1/2016', 101)
GO

ALTER TABLE stretch_table_name SET ( REMOTE_DATA_ARCHIVE = ON (


FILTER_PREDICATE = dbo.fn_stretchpredicate(date),
MIGRATION_STATE = OUTBOUND
) )

Apply the IS NULL or IS NOT NULL operator to a function parameter.


Use the IN operator to compare a function parameter to a list of constant values.
Here's an example that checks whether the value of a shipment_status column is
IN (N'Completed', N'Returned', N'Cancelled') .

CREATE FUNCTION dbo.fn_stretchpredicate(@column1 nvarchar(15))


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 IN (N'Completed', N'Returned', N'Cancelled')
GO

ALTER TABLE table1 SET ( REMOTE_DATA_ARCHIVE = ON (


FILTER_PREDICATE = dbo.fn_stretchpredicate(shipment_status),
MIGRATION_STATE = OUTBOUND
) )

Comparison operators
The following comparison operators are supported.
<, <=, >, >=, =, <>, !=, !<, !>

<comparison_operator> ::= { < | <= | > | >= | = | <> | != | !< | !> }

Constant expressions
The constants that you use in a filter function can be any deterministic expression that can be evaluated when you
define the function. Constant expressions can contain the following things.
Literals. For example, N’abc’, 123 .
Algebraic expressions. For example, 123 + 456 .
Deterministic functions. For example, SQRT(900) .
Deterministic conversions that use CAST or CONVERT. For example, CONVERT(datetime, '1/1/2016', 101) .
Other expressions
You can use the BETWEEN and NOT BETWEEN operators if the resulting function conforms to the rules described
here after you replace the BETWEEN and NOT BETWEEN operators with the equivalent AND and OR expressions.
You can't use subqueries or non-deterministic functions such as RAND() or GETDATE().

Add a filter function to a table


Add a filter function to a table by running the ALTER TABLE statement and specifying an existing inline table-
valued function as the value of the FILTER_PREDICATE parameter. For example:

ALTER TABLE stretch_table_name SET ( REMOTE_DATA_ARCHIVE = ON (


FILTER_PREDICATE = dbo.fn_stretchpredicate(column1, column2),
MIGRATION_STATE = <desired_migration_state>
) )

After you bind the function to the table as a predicate, the following things are true.
The next time data migration occurs, only the rows for which the function returns a non-empty value are
migrated.
The columns used by the function are schema bound. You can't alter these columns as long as a table is
using the function as its filter predicate.
You can't drop the inline table-valued function as long as a table is using the function as its filter predicate.

TIP
To improve the performance of the filter function, create an index on the columns used by the function.

Passing column names to the filter function


When you assign a filter function to a table, specify the column names passed to the filter function with a one-part
name. If you specify a three-part name when you pass the column names, subsequent queries against the Stretch-
enabled table will fail.
For example, if you specify a three-part column name as shown in the following example, the statement will run
successfully, but subsequent queries against the table will fail.
ALTER TABLE SensorTelemetry
SET ( REMOTE_DATA_ARCHIVE = ON (
FILTER_PREDICATE=dbo.fn_stretchpredicate(dbo.SensorTelemetry.ScanDate),
MIGRATION_STATE = OUTBOUND )
)

Instead, specify the filter function with a one-part column name as shown in the following example.

ALTER TABLE SensorTelemetry


SET ( REMOTE_DATA_ARCHIVE = ON (
FILTER_PREDICATE=dbo.fn_stretchpredicate(ScanDate),
MIGRATION_STATE = OUTBOUND )
)

Add a filter function after running the Wizard


If you want use a function that you can't create in the Enable Database for Stretch Wizard, you can run the
ALTER TABLE statement to specify a function after you exit the wizard. Before you can apply a function, however,
you have to stop the data migration that's already in progress and bring back migrated data. (For more info about
why this is necessary, see Replace an existing filter function.)
1. Reverse the direction of migration and bring back the data already migrated. You can't cancel this
operation after it starts. You also incur costs on Azure for outbound data transfers (egress). For more info,
see How Azure pricing works.

ALTER TABLE <table name>


SET ( REMOTE_DATA_ARCHIVE ( MIGRATION_STATE = INBOUND ) ) ;

2. Wait for migration to finish. You can check the status in Stretch Database Monitor from SQL Server
Management Studio, or you can query the sys.dm_db_rda_migration_status view. For more info, see
Monitor and troubleshoot data migration or sys.dm_db_rda_migration_status.
3. Create the filter function that you want to apply to the table.
4. Add the function to the table and restart data migration to Azure.

ALTER TABLE <table name>


SET ( REMOTE_DATA_ARCHIVE
(
FILTER_PREDICATE = <predicate>,
MIGRATION_STATE = OUTBOUND
)
);

Filter rows by date


The following example migrates rows where the date column contains a value earlier than January 1, 2016.
-- Filter by date
--
CREATE FUNCTION dbo.fn_stretch_by_date(@date datetime2)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible WHERE @date < CONVERT(datetime2, '1/1/2016', 101)
GO

Filter rows by the value in a status column


The following example migrates rows where the status column contains one of the specified values.

-- Filter by status column


--
CREATE FUNCTION dbo.fn_stretch_by_status(@status nvarchar(128))
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible WHERE @status IN (N'Completed', N'Returned', N'Cancelled')
GO

Filter rows by using a sliding window


To filter rows by using a sliding window, keep in mind the following requirements for the filter function.
The function has to be deterministic. Therefore you can't create a function that automatically recalculates
the sliding window as time passes.
The function uses schema binding. Therefore you can't simply update the function "in place" every day by
calling ALTER FUNCTION to move the sliding window.
Start with a filter function like the following example, which migrates rows where the systemEndTime
column contains a value earlier than January 1, 2016.

CREATE FUNCTION dbo.fn_StretchBySystemEndTime20160101(@systemEndTime datetime2)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @systemEndTime < CONVERT(datetime2, '2016-01-01T00:00:00', 101) ;

Apply the filter function to the table.

ALTER TABLE <table name>


SET (
REMOTE_DATA_ARCHIVE = ON
(
FILTER_PREDICATE = dbo.fn_StretchBySystemEndTime20160101(SysEndTime)
, MIGRATION_STATE = OUTBOUND
)
)
;

When you want to update the sliding window, do the following things.
1. Create a new function that specifies the new sliding window. The following example selects dates earlier
than January 2, 2016, instead of January 1, 2016.
2. Replace the previous filter function with the new one by calling ALTER TABLE, as shown in the following
example.
3. Optionally, drop the previous filter function that you're no longer using by calling DROP FUNCTION. (This
step is not shown in the example.)

BEGIN TRAN
GO
/*(1) Create new predicate function definition */
CREATE FUNCTION dbo.fn_StretchBySystemEndTime20160102(@systemEndTime datetime2)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @systemEndTime < CONVERT(datetime2,'2016-01-02T00:00:00', 101)
GO

/*(2) Set the new function as the filter predicate */


ALTER TABLE <table name>
SET
(
REMOTE_DATA_ARCHIVE = ON
(
FILTER_PREDICATE = dbo.fn_StretchBySystemEndTime20160102(SysEndTime),
MIGRATION_STATE = OUTBOUND
)
)
COMMIT ;

More examples of valid filter functions


The following example combines two primitive conditions by using the AND logical operator.

CREATE FUNCTION dbo.fn_stretchpredicate((@column1 datetime, @column2 nvarchar(15))


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < N'20150101' AND @column2 IN (N'Completed', N'Returned', N'Cancelled')
GO

ALTER TABLE table1 SET ( REMOTE_DATA_ARCHIVE = ON (


FILTER_PREDICATE = dbo.fn_stretchpredicate(date, shipment_status),
MIGRATION_STATE = OUTBOUND
) )

The following example uses several conditions and a deterministic conversion with CONVERT.

CREATE FUNCTION dbo.fn_stretchpredicate_example1(@column1 datetime, @column2 int, @column3 nvarchar)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '1/1/2015', 101) AND (@column2 < -100 OR @column2 > 100 OR
@column2 IS NULL) AND @column3 IN (N'Completed', N'Returned', N'Cancelled')
GO

The following example uses mathematical operators and functions.


CREATE FUNCTION dbo.fn_stretchpredicate_example2(@column1 float)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < SQRT(400) + 10
GO

The following example uses the BETWEEN and NOT BETWEEN operators. This usage is valid because the
resulting function conforms to the rules described here after you replace the BETWEEN and NOT BETWEEN
operators with the equivalent AND and OR expressions.

CREATE FUNCTION dbo.fn_stretchpredicate_example3(@column1 int, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 BETWEEN 0 AND 100
AND (@column2 NOT BETWEEN 200 AND 300 OR @column1 = 50)
GO

The preceding function is equivalent to the following function after you replace the BETWEEN and NOT
BETWEEN operators with the equivalent AND and OR expressions.

CREATE FUNCTION dbo.fn_stretchpredicate_example4(@column1 int, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 >= 0 AND @column1 <= 100 AND (@column2 < 200 OR @column2 > 300 OR @column1 = 50)
GO

Examples of filter functions that aren't valid


The following function isn't valid because it contains a non-deterministic conversion.

CREATE FUNCTION dbo.fn_example5(@column1 datetime)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '1/1/2016')
GO

The following function isn't valid because it contains a non-deterministic function call.

CREATE FUNCTION dbo.fn_example6(@column1 datetime)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < DATEADD(day, -60, GETDATE())
GO

The following function isn't valid because it contains a subquery.


CREATE FUNCTION dbo.fn_example7(@column1 int)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 IN (SELECT SupplierID FROM Supplier WHERE Status = 'Defunct')
GO

The following functions aren't valid because expressions that use algebraic operators or built-in functions
must evaluate to a constant when you define the function. You can't include column references in algebraic
expressions or function calls.

CREATE FUNCTION dbo.fn_example8(@column1 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 % 2 = 0
GO

CREATE FUNCTION dbo.fn_example9(@column1 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE SQRT(@column1) = 30
GO

The following function isn't valid because it violates the rules described here after you replace the
BETWEEN operator with the equivalent AND expression.

CREATE FUNCTION dbo.fn_example10(@column1 int, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE (@column1 BETWEEN 1 AND 200 OR @column1 = 300) AND @column2 > 1000
GO

The preceding function is equivalent to the following function after you replace the BETWEEN operator with
the equivalent AND expression. This function isn't valid because primitive conditions can only use the OR
logical operator.

CREATE FUNCTION dbo.fn_example11(@column1 int, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE (@column1 >= 1 AND @column1 <= 200 OR @column1 = 300) AND @column2 > 1000
GO

How Stretch Database applies the filter function


Stretch Database applies the filter function to the table and determines eligible rows by using the CROSS APPLY
operator. For example:
SELECT * FROM stretch_table_name CROSS APPLY fn_stretchpredicate(column1, column2)

If the function returns a non-empty result for the row, the row is eligible to be migrated.

Replace an existing filter function


You can replace a previously specified filter function by running the ALTER TABLE statement again and specifying
a new value for the FILTER_PREDICATE parameter. For example:

ALTER TABLE stretch_table_name SET ( REMOTE_DATA_ARCHIVE = ON (


FILTER_PREDICATE = dbo.fn_stretchpredicate2(column1, column2),
MIGRATION_STATE = <desired_migration_state>

The new inline table-valued function has the following requirements.


The new function has to be less restrictive than the previous function.
All the operators that existed in the old function must exist in the new function.
The new function can't contain operators that don’t exist in the old function.
The order of operator arguments can't change.
Only constant values that are part of a <, <=, >, >= comparison can be changed in a way that makes the
function less restrictive.
Example of a valid replacement
Assume that the following function is the current filter function.

CREATE FUNCTION dbo.fn_stretchpredicate_old(@column1 datetime, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '1/1/2016', 101)
AND (@column2 < -100 OR @column2 > 100)
GO

The following function is a valid replacement because the new date constant (which specifies a later cutoff date)
makes the function less restrictive.

CREATE FUNCTION dbo.fn_stretchpredicate_new(@column1 datetime, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '2/1/2016', 101)
AND (@column2 < -50 OR @column2 > 50)
GO

Examples of replacements that aren't valid


The following function isn't a valid replacement because the new date constant (which specifies an earlier cutoff
date) doesn't make the function less restrictive.
CREATE FUNCTION dbo.fn_notvalidreplacement_1(@column1 datetime, @column2 int)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '1/1/2015', 101)
AND (@column2 < -100 OR @column2 > 100)
GO

The following function isn't a valid replacement because one of the comparison operators has been removed.

CREATE FUNCTION dbo.fn_notvalidreplacement_2(@column1 datetime, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '1/1/2016', 101)
AND (@column2 < -50)
GO

The following function isn't a valid replacement because a new condition has been added with the AND logical
operator.

CREATE FUNCTION dbo.fn_notvalidreplacement_3(@column1 datetime, @column2 int)


RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS is_eligible
WHERE @column1 < CONVERT(datetime, '1/1/2016', 101)
AND (@column2 < -100 OR @column2 > 100)
AND (@column2 <> 0)
GO

Remove a filter function from a table


To migrate the entire table instead of selected rows, remove the existing function by setting FILTER_PREDICATE
to null. For example:

ALTER TABLE stretch_table_name SET ( REMOTE_DATA_ARCHIVE = ON (


FILTER_PREDICATE = NULL,
MIGRATION_STATE = <desired_migration_state>
) )

After you remove the filter function, all rows in the table are eligible for migration. As a result, you cannot specify
a filter function for the same table later unless you bring back all the remote data for the table from Azure first.
This restriction exists to avoid the situation where rows that are not eligible for migration when you provide a new
filter function have already been migrated to Azure.

Check the filter function applied to a table


To check the filter function applied to a table, open the catalog view sys.remote_data_archive_tables and check
the value of the filter_predicate column. If the value is null, the entire table is eligible for archiving. For more
info, see sys.remote_data_archive_tables (Transact-SQL).

Security notes for filter functions


A compromised account with db_owner privileges can do the following things.
Create and apply a table-valued function that consumes large amounts of server resources or waits for an
extended period resulting in a denial of service.
Create and apply a table-valued function that makes it possible to infer the content of a table for which the
user has been explicitly denied read access.

See Also
ALTER TABLE (Transact-SQL)
Monitor and troubleshoot data migration (Stretch
Database)
4/12/2017 • 1 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To monitor data migration in Stretch Database Monitor, select Tasks | Stretch | Monitor for a database in SQL
Server Management Studio .

Check the status of data migration in the Stretch Database Monitor


Select Tasks | Stretch | Monitor for a database in SQL Server Management Studio to open Stretch Database
Monitor and monitor data migration.
The top portion of the monitor displays general information about both the Stretch-enabled SQL Server
database and the remote Azure database.
The bottom portion of the monitor displays the status of data migration for each Stretch-enabled table in
the database.

Check the status of data migration in a dynamic management view


Open the dynamic management view sys.dm_db_rda_migration_status to see how many batches and rows of
data have been migrated. For more info, see sys.dm_db_rda_migration_status (Transact-SQL).

Troubleshoot data migration


Rows from my Stretch-enabled table are not being migrated to Azure. What’s the problem?
There are several problems that can affect migration. Check the following things.
Check network connectivity for the SQL Server computer.
Check that the Azure firewall is not blocking your SQL Server from connecting to the remote endpoint.
Check the dynamic management view sys.dm_db_rda_migration_status for the status of the latest batch.
If an error has occurred, check the error_number, error_state, and error_severity values for the batch.
For more info about the view, see sys.dm_db_rda_migration_status (Transact-SQL).
For more info about the content of a SQL Server error message, see sys.messages (Transact-SQL).
The Azure firewall is blocking connections from my local server.
You may have to add a rule in the Azure firewall settings of the Azure server to let SQL Server
communicate with the remote Azure server.

See Also
Manage and troubleshoot Stretch Database
Pause and resume data migration (Stretch Database)
4/12/2017 • 1 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To pause or resume data migration to Azure, select Stretch for a table in SQL Server Management Studio, and
then select Pause to pause data migration or Resume to resume data migration. You can also use Transact-SQL
to pause or resume data migration.
Pause data migration on individual tables when you want to troubleshoot problems on the local server or to
maximize the available network bandwidth.

Pause data migration


Use SQL Server Management Studio to pause data migration
1. In SQL Server Management Studio, in Object Explorer, select the Stretch-enabled table for which you want
to pause data migration.
2. Right-click and select Stretch, and then select Pause.
Use Transact-SQL to pause data migration
Run the following command.

USE <Stretch-enabled database name>;


GO
ALTER TABLE <Stretch-enabled table name>
SET ( REMOTE_DATA_ARCHIVE ( MIGRATION_STATE = PAUSED ) ) ;
GO

Resume data migration


Use SQL Server Management Studio to resume data migration
1. In SQL Server Management Studio, in Object Explorer, select the Stretch-enabled table for which you want
to resume data migration.
2. Right-click and select Stretch, and then select Resume.
Use Transact-SQL to resume data migration
Run the following command.

USE <Stretch-enabled database name>;


GO
ALTER TABLE <Stretch-enabled table name>
SET ( REMOTE_DATA_ARCHIVE ( MIGRATION_STATE = OUTBOUND ) ) ;
GO

Check whether migration is active or paused


Use SQL Server Management Studio to check whether migration is active or paused
In SQL Server Management Studio, open Stretch Database Monitor and check the value of the Migration State
column. For more info, see Monitor and troubleshoot data migration.
Use Transact-SQL to check whether migration is active or paused
Query the catalog view sys.remote_data_archive_tables and check the value of the is_migration_paused
column. For more info, see sys.remote_data_archive_tables.

See Also
ALTER TABLE (Transact-SQL)
Monitor and troubleshoot data migration
Manage and troubleshoot Stretch Database
4/12/2017 • 5 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To manage and troubleshoot Stretch Database, use the tools and methods described in this topic .

Manage local data


Get info about local databases and tables enabled for Stretch Database
Open the catalog views sys.databases and sys.tables to see info about Stretch-enabled SQL Server databases
and tables. For more info, see sys.databases (Transact-SQL) and sys.tables (Transact-SQL).
To see how much space a Stretch-enabled table is using in SQL Server, run the following statement.

USE <Stretch-enabled database name>;


GO
EXEC sp_spaceused '<Stretch-enabled table name>', 'true', 'LOCAL_ONLY';
GO

Manage data migration


Check the filter function applied to a table
Open the catalog view sys.remote_data_archive_tables and check the value of the filter_predicate column to
identify the function that Stretch Database is using to select rows to migrate. If the value is null, the entire table is
eligible to be migrated. For more info, see sys.remote_data_archive_tables (Transact-SQL) and Select rows to
migrate by using a filter function.
Check the status of data migration
Select Tasks | Stretch | Monitor for a database in SQL Server Management Studio to monitor data migration in
Stretch Database Monitor. For more info, see Monitor and troubleshoot data migration (Stretch Database).
Or, open the dynamic management view sys.dm_db_rda_migration_status to see how many batches and rows
of data have been migrated.
Troubleshoot data migration
For troubleshooting suggestions, see Monitor and troubleshoot data migration (Stretch Database).

Manage remote data


Get info about remote databases and tables used by Stretch Database
Open the catalog views sys.remote_data_archive_databases and sys.remote_data_archive_tables to see info
about the remote databases and tables in which migrated data is stored. For more info, see
sys.remote_data_archive_databases (Transact-SQL) and sys.remote_data_archive_tables (Transact-SQL).
To see how much space a Stretch-enabled table is using in Azure, run the following statement.
USE <Stretch-enabled database name>;
GO
EXEC sp_spaceused '<Stretch-enabled table name>', 'true', 'REMOTE_ONLY';
GO

Delete migrated data


If you want to delete data that has already been migrated to Azure, follow the steps described in
sys.sp_rda_reconcile_batch.

Manage table schema


Don't change the schema of the remote table
Don't change the schema of a remote Azure table that's associated with a SQL Server table configured for Stretch
Database. In particular, don't modify the name or the data type of a column. The Stretch Database feature makes
various assumptions about the schema of the remote table in relation to the schema of the SQL Server table. If
you change the remote schema, Stretch Database stops working for the changed table.
Reconcile table columns
If you have accidentally deleted columns from the remote table, run sp_rda_reconcile_columns to add columns
to the remote table that exist in the Stretch-enabled SQL Server table but not in the remote table. For more info,
see sys.sp_rda_reconcile_columns.

IMPORTANT
When sp_rda_reconcile_columns recreates columns that you accidentally deleted from the remote table, it does not
restore the data that was previously in the deleted columns.

sp_rda_reconcile_columns does not delete columns from the remote table that exist in the remote table but not
in the Stretch-enabled SQL Server table. If there are columns in the remote Azure table that no longer exist in the
Stretch-enabled SQL Server table, these extra columns do not prevent Stretch Database from operating normally.
You can optionally remove the extra columns manually.

Manage performance and costs


Troubleshoot query performance
Queries that include Stretch-enabled tables are expected to perform more slowly than they did before the tables
were enabled for Stretch. If query performance degrades significantly, review the following possible problems.
Is your Azure server in a different geographical region than your SQL Server? Configure your Azure server
to be in the same geographical region as your SQL Server to reduce network latency.
Your network conditions may have degraded. Contact your network administrator for info about recent
issues or outages.
Increase Azure performance level for resource -intensive operations such as indexing
When you build, rebuild, or reorganize an index on a large table that's configured for Stretch Database, and you
anticipate heavy querying of the migrated data in Azure during this time, consider increasing the performance
level of the corresponding remote Azure database for the duration of the operation. For more info about
performance levels and pricing, see SQL Server Stretch Database Pricing.
You can't pause the SQL Server Stretch Database service on Azure
Make sure that you select the appropriate performance and pricing level. If you increase the performance level
temporarily for a resource-intensive operation, restore it to the previous level after the operation completes. For
more info about performance levels and pricing, see SQL Server Stretch Database Pricing.

Change the scope of queries


Queries against Stretch-enabled tables return both local and remote data by default. You can change the scope of
queries for all queries by all users, or only for a single query by an administrator.
Change the scope of queries for all queries by all users
To change the scope of all queries by all users, run the stored procedure sys.sp_rda_set_query_mode. You can
reduce the scope to query local data only, disable all queries, or restore the default setting. For more info, see
sys.sp_rda_set_query_mode.
Change the scope of queries for a single query by an administrator
To change the scope of a single query by a member of the db_owner role, add the WITH (
REMOTE_DATA_ARCHIVE_OVERRIDE = value ) query hint to the SELECT statement. The
REMOTE_DATA_ARCHIVE_OVERRIDE query hint can have the following values.
LOCAL_ONLY. Query local data only.
REMOTE_ONLY. Query remote data only.
STAGE_ONLY. Query only the data in the table where Stretch Database stages rows eligible for migration
and retains migrated rows for the specified period after migration. This query hint is the only way to query
the staging table.
For example, the following query returns local results only.

USE <Stretch-enabled database name>;


GO
SELECT * FROM <Stretch_enabled table name> WITH (REMOTE_DATA_ARCHIVE_OVERRIDE = LOCAL_ONLY) WHERE ... ;
GO

Make administrative updates and deletes


By default you can't UPDATE or DELETE rows that are eligible for migration, or rows that have already been
migrated, in a Stretch-enabled table. When you have to fix a problem, a member of the db_owner role can run an
UPDATE or DELETE operation by adding the WITH ( REMOTE_DATA_ARCHIVE_OVERRIDE = value ) query hint
to the statement. The REMOTE_DATA_ARCHIVE_OVERRIDE query hint can have the following values.
LOCAL_ONLY. Update or delete local data only.
REMOTE_ONLY. Update or delete remote data only.
STAGE_ONLY. Update or delete only the data in the table where Stretch Database stages rows eligible for
migration and retains migrated rows for the specified period after migration.

See Also
Monitor and troubleshoot data migration (Stretch Database)
Backup Stretch-enabled databases (Stretch Database)
Restore Stretch-enabled databases (Stretch Database)
Backup Stretch-enabled databases (Stretch
Database)
4/12/2017 • 3 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
Database backups help you to recover from many types of failures, errors, and disasters.
You have to back up your Stretch-enabled SQL Server databases.
Microsoft Azure automatically backs up the remote data that Stretch Database has migrated from SQL
Server to Azure.

TIP
Backup is only one part of a complete high availability and business continuity solution. For more info about high availability,
see High Availability Solutions.

Back up your SQL Server data


To back up your Stretch-enabled SQL Server databases, you can continue to use the SQL Server backup methods
that you currently use. For more info, see Back Up and Restore of SQL Server Databases.
Backups of a Stretch-enabled SQL Server database contain only local data and data eligible for migration at the
point in time when the backup runs. (Eligible data is data that has not yet been migrated, but will be migrated to
Azure based on the migration settings of the tables.) This is known as a shallow backup and does not include the
data already migrated to Azure.

Back up your remote Azure data


Microsoft Azure automatically backs up the remote data that Stretch Database has migrated from SQL Server to
Azure.
Azure reduces the risk of data loss with automatic backup
The SQL Server Stretch Database service on Azure protects your remote databases with automatic storage
snapshots at least every 8 hours. It retains each snapshot for 7 days to provide you with a range of possible
restore points.
Azure reduces the risk of data loss with geo -redundancy
Azure database backups are stored on geo-redundant Azure Storage (RA-GRS) and are therefore geo-redundant
by default. Geo-redundant storage replicates your data to a secondary region that is hundreds of miles away from
the primary region. In both primary and secondary regions, your data is replicated three times each, across
separate fault domains and upgrade domains. This ensures that your data is durable even in the case of a
complete regional outage or disaster that renders one of the Azure regions unavailable.
Stretch Database reduces the risk of data loss for your Azure data by retaining migrated rows temporarily
After Stretch Database migrates eligible rows from SQL Server to Azure, it retains those rows in the staging table
for a minimum of 8 hours. If you restore a backup of your Azure database, Stretch Database uses the rows saved
in the staging table to reconcile the SQL Server and the Azure databases.
After you restore a backup of your Azure data, you have to run the stored procedure sys.sp_rda_reauthorize_db to
reconnect the Stretch-enabled SQL Server database to the remote Azure database. When you run
sys.sp_rda_reauthorize_db, Stretch Database automatically reconciles the SQL Server and the Azure databases.
To increase the number of hours of migrated data that Stretch Database retains temporarily in the staging table,
run the stored procedure sys.sp_rda_set_rpo_duration and specify a number of hours greater than 8. To decide
how much data to retain, consider the following factors:
The frequency of automatic Azure backups (at least every 8 hours).
The time required after a problem to recognize the problem and to decide to restore a backup.
The duration of the Azure restore operation.

NOTE
Increasing the amount of data that Stretch Database retains temporarily in the staging table increases the amount of space
required on the SQL Server.

To check the number of hours of data that Stretch Database currently retains temporarily in the staging table, run
the stored procedure sys.sp_rda_get_rpo_duration.

See Also
Restore Stretch-enabled databases
Manage and troubleshoot Stretch Database
Restore Stretch-enabled databases (Stretch
Database)
4/12/2017 • 2 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
Restore a backed up database when necessary to recover from many types of failures, errors, and disasters.
For more info about backup, see Backup Stretch-enabled databases.

TIP
Backup is only one part of a complete high availability and business continuity solution. For more info about high availability,
see High Availability Solutions.

Restore your SQL Server data


To recover from hardware failure or corruption, restore the Stretch-enabled SQL Server database from a backup.
You can continue to use the SQL Server restore methods that you currently use. For more info, see Restore and
Recovery Overview.
After you restore the SQL Server database, you have to run the stored procedure sys.sp_rda_reauthorize_db to
re-establish the connection between the Stretch-enabled SQL Server database and the remote Azure database. For
more info, see Restore the connection between the SQL Server database and the remote Azure database.

Restore your remote Azure data


Recover a live Azure database
The SQL Server Stretch Database service on Azure snapshots all live data at least every 8 hours using Azure
Storage Snapshots. These snapshots are maintained for 7 days. This allows you to restore the data to one of at
least 21 points in time within the past 7 days up to the time when the last snapshot was taken.
To restore a live Azure database to an earlier point in time by using the Azure portal, do the following things.
1. Log in to the Azure portal.
2. On the left side of the screen select BROWSE and then select SQL Databases.
3. Navigate to your database and select it.
4. At the top of the database blade, click Restore.
5. Specify a new Database name, select a Restore Point and then click Create.
6. The database restore process will begin and can be monitored using NOTIFICATIONS.
Recover a deleted Azure database
The SQL Server Stretch Database service on Azure takes a database snapshot before a database is dropped and
retains it for 7 days. After this occurs, it no longer retains snapshots from the live database. This lets you restore a
deleted database to the point when it was deleted.
To restore a deleted Azure database to the point when it was deletedby using the Azure portal, do the following
things.
1. Log in to the Azure portal.
2. On the left side of the screen select BROWSE and then select SQL Servers.
3. Navigate to your server and select it.
4. Scroll down to Operations on your server's blade, click the Deleted Databases tile.
5. Select the deleted database you want to restore.
6. Specify a new Database name and click Create.
7. The database restore process will begin and can be monitored using NOTIFICATIONS.

Restore the connection between the SQL Server database and the
remote Azure database
1. If you're going to connect to a restored Azure database with a different name or in a different region, run
the stored procedure sys.sp_rda_deauthorize_db to disconnect from the previous Azure database.
2. Run the stored procedure sys.sp_rda_reauthorize_db to reconnect the local Stretch-enabled database to the
Azure database.
Provide the existing database scoped credential as a sysname or a varchar(128) value. (Don't use
varchar(max).) You can look up the credential name in the view sys.database_scoped_credentials.
Specify whether to make a copy of the remote data and connect to the copy (recommended).

USE <Stretch-enabled database name>;


GO
EXEC sp_rda_reauthorize_db
@credential = N'<existing_database_scoped_credential_name>',
@with_copy = 1 ;
GO

See Also
Backup Stretch-enabled databases
Manage and troubleshoot Stretch Database
sys.sp_rda_reauthorize_db sys.sp_rda_deauthorize_db
Back Up and Restore of SQL Server Databases
Disable Stretch Database and bring back remote data
4/12/2017 • 4 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
To disable Stretch Database for a table, select Stretch for a table in SQL Server Management Studio. Then select
one of the following options.
Disable | Bring data back from Azure. Copy the remote data for the table from Azure back to SQL Server,
then disable Stretch Database for the table. This operation incurs data transfer costs, and it can't be canceled.
Disable | Leave data in Azure. Disable Stretch Database for the table. Abandon the remote data for the
table in Azure.
You can also use Transact-SQL to disable Stretch Database for a table or for a database.
After you disable Stretch Database for a table, data migration stops and query results no longer include
results from the remote table.
If you simply want to pause data migration, see Pause and resume data migration (Stretch Database).

NOTE
Disabling Stretch Database for a table or for a database does not delete the remote object. If you want to delete the remote
table or the remote database, you have to drop it by using the Azure management portal. The remote objects continue to
incur Azure costs until you delete them. For more info, see SQL Server Stretch Database Pricing.

Disable Stretch Database for a table


Use SQL Server Management Studio to disable Stretch Database for a table
1. In SQL Server Management Studio, in Object Explorer, select the table for which you want to disable Stretch
Database.
2. Right-click and select Stretch, and then select one of the following options.
Disable | Bring data back from Azure. Copy the remote data for the table from Azure back to SQL
Server, then disable Stretch Database for the table. This command can't be canceled.

NOTE
Copying the remote data for the table from Azure back to SQL Server incurs data transfer costs. For more
info, see Data Transfers Pricing Details.

After all the remote data has been copied from Azure back to SQL Server, Stretch is disabled for the
table.
Disable | Leave data in Azure. Disable Stretch Database for the table. Abandon the remote data for
the table in Azure.
NOTE
Disabling Stretch Database for a table does not delete the remote data or the remote table. If you want to delete the
remote table, you have to drop it by using the Azure management portal. The remote table continues to incur Azure
costs until you delete it. For more info, see SQL Server Stretch Database Pricing.

Use Transact-SQL to disable Stretch Database for a table


To disable Stretch for a table and copy the remote data for the table from Azure back to SQL Server, run the
following command.After all the remote data has been copied from Azure back to SQL Server, Stretch is
disabled for the table.
This command can't be canceled.

USE <Stretch-enabled database name>;


GO
ALTER TABLE <Stretch-enabled table name>
SET ( REMOTE_DATA_ARCHIVE ( MIGRATION_STATE = INBOUND ) ) ;
GO

NOTE
Copying the remote data for the table from Azure back to SQL Server incurs data transfer costs. For more info, see
Data Transfers Pricing Details.

To disable Stretch for a table and abandon the remote data, run the following command.

USE <Stretch-enabled database name>;


GO
ALTER TABLE <Stretch-enabled table name>
SET ( REMOTE_DATA_ARCHIVE = OFF_WITHOUT_DATA_RECOVERY ( MIGRATION_STATE = PAUSED ) ) ;
GO

NOTE
Disabling Stretch Database for a table does not delete the remote data or the remote table. If you want to delete the remote
table, you have to drop it by using the Azure management portal. The remote table continues to incur Azure costs until you
delete it. For more info, see SQL Server Stretch Database Pricing.

Disable Stretch Database for a database


Before you can disable Stretch Database for a database, you have to disable Stretch Database on the individual
Stretch-enabled tables in the database.
Use SQL Server Management Studio to disable Stretch Database for a database
1. In SQL Server Management Studio, in Object Explorer, select the database for which you want to disable
Stretch Database.
2. Right-click and select Tasks, and then select Stretch, and then select Disable.
NOTE
Disabling Stretch Database for a database does not delete the remote database. If you want to delete the remote database,
you have to drop it by using the Azure management portal. The remote database continues to incur Azure costs until you
delete it. For more info, see SQL Server Stretch Database Pricing.

Use Transact-SQL to disable Stretch Database for a database


Run the following command.

ALTER DATABASE <Stretch-enabled database name>


SET REMOTE_DATA_ARCHIVE = OFF ;
GO

NOTE
Disabling Stretch Database for a database does not delete the remote database. If you want to delete the remote database,
you have to drop it by using the Azure management portal. The remote database continues to incur Azure costs until you
delete it. For more info, see SQL Server Stretch Database Pricing.

See Also
ALTER DATABASE SET Options (Transact-SQL)
Pause and resume data migration (Stretch Database)
Extended Events for Stretch Database
3/24/2017 • 3 min to read • Edit Online

THIS TOPIC APPLIES TO: SQL Server (starting with 2016) Azure SQL Database Azure SQL Data
Warehouse Parallel Data Warehouse
Stretch Database provides a set of extended events for troubleshooting.
For more info, see Extended Events. For info about how to start an extended events session for troubleshooting, see
Create an Extended Events Session

List of extended events for Stretch Database


EVENT NAME EVENT DESCRIPTION

remote_data_archive_db_ddl Occurs when the database T-SQL ddl for stretching data is
processed.

remote_data_archive_provision_operation Occurs when a provisioning operation starts or ends.

remote_data_archive_query_rewrite Occurs when RelOp_Get is replaced during query rewrite for


Stretch.

remote_data_archive_table_ddl Occurs when the table T-SQL ddl for stretching data is
processed.

remote_data_archive_telemetry Occurs whenever an on premise system transmits a telemetry


event to Azure DB.

remote_data_archive_telemetry_rejected Occurs whenever an AzureDB Stretch telemetry event is


rejected

repopulate_stretch_schema_task_queue_complete Reports the completion of repopulating stretch schema task


queue.

repopulate_stretch_schema_task_queue_start Reports the start of repopulating stretch schema task queue.

stretch_codegen_errorlog Reports the output from the code generator

stretch_codegen_start Reports the start of stretch code generation

stretch_create_remote_table_start Reports the start of remote table creation

stretch_database_disable_completed Reports the completion of a ALTER DATABASE SET


REMOTE_DATA_ARCHIVE OFF command

stretch_database_enable_completed Reports the completion of a ALTER DATABASE SET


REMOTE_DATA_ARCHIVE ON command

stretch_database_reauthorize_completed Reports the completion of a sp_rda_reauthorize_db spec proc


EVENT NAME EVENT DESCRIPTION

stretch_index_reconciliation_codegen_completed Reports the completion of code generation for stretch remote


index operation

stretch_index_update_step_completed Reports the duration of a stretched index update operation

stretch_migration_debug_trace Debug trace of stretch migration actions.

stretch_migration_dequeue_migration Event raised when a stretch migration task is dequeued for a


database.

stretch_migration_queue_migration Queue a packet for starting migration of the database and


object.

stretch_migration_requeue_migration Event raised when a stretch migration task packet is requeued.

stretch_migration_start_migration Start migration of the database and object.

stretch_migration_start_unmigration Start unmigration of the database and object.

stretch_remote_column_execution_completed Reports the completion of remote execution for the generated


code for a stretched column

stretch_remote_column_reconciliation_codegen_completed Reports the completion of code generation for stretch remote


column reconciliation

stretch_remote_index_execution_completed Reports the completion of remote execution for the generated


code for a stretched index

stretch_schema_queue_task Reports when a packet is about to be queued for processing a


schema task for the database and object.

stretch_schema_script_execution_completed Reports the completion of stretch script execution during


processing stretch schema task.

stretch_schema_script_execution_skipped Reports the skipping of stretch script execution during


processing stretch schema task.

stretch_schema_script_execution_start Reports the start of stretch script execution during processing


stretch schema task.

stretch_schema_task_failed Reports the failure of a stretch schema function during the


stretch schema task.

stretch_schema_task_skipped Reports the stretch schema task is skipped during the stretch
schema function.

stretch_schema_task_start Reports the start of stretch schema function during the stretch
schema task.

stretch_schema_task_succeeded Reports the successful completion of stretch schema function


during the stretch schema task.

stretch_sp_migration_get_batch_id Call sp_stretch_get_batch_id


EVENT NAME EVENT DESCRIPTION

stretch_sync_metadata_start Reports the start of metadata checks during the migration


task.

stretch_table_codegen_completed Reports the completion of code generation for a stretched


table

stretch_table_complete_data_reconciliation Complete data reconciliation of the database and object.

stretch_table_data_reconciliation_event Reports the completion of the data reconciliation of a batch of


rows

stretch_table_data_reconciliation_results_event Reports an error or completion of a successful data


reconciliation of a number of batches of rows

stretch_table_hinted_admin_delete_event Reports the execution of a Stretch delete DML operation that


uses an admin hint

stretch_table_hinted_admin_update_event Reports the execution of a Stretch update DML operation that


uses an admin hint

stretch_table_provisioning_step_completed Reports the duration of a stretched table provisioning


operation

stretch_table_query_error Reports an error thrown during Stretch query rewrite

stretch_table_remote_creation_completed Reports the completion of remote execution for the generated


code for a stretched table

stretch_table_row_migration_event Reports the completion of the migration of a batch of rows

stretch_table_row_migration_results_event Reports an error or completion of a successful migration of a


number of batches of rows

stretch_table_row_unmigration_event Reports the completion of the unmigration of a batch of rows

stretch_table_row_unmigration_results_event Reports an error or completion of a successful unmigration of


a number of batches of rows

stretch_table_start_data_reconciliation Start data reconciliation of the database and object.

stretch_table_unprovision_completed Reports the completion removal of local resources for a table


that was unstretched

stretch_table_validation_error Reports the completion of validation for a table when the user
enables stretch

stretch_unprovision_table_start Reports the start of stretch table un-provisioning

See Also
Manage and troubleshoot Stretch Database

Вам также может понравиться