Tuesday 31 January 2017

Stretch Database SQL Server 2016


Introduction to Stretch Database:

What?

Stretch Database is one of the highly anticipated new feature in Microsoft SQL Server 2016.

This feature archives your historical data (Cold data) to Azure SQL database (PaaS Cloud) securely.Once you enable this feature, SQL Server silently and transparently moves/migrates data to Azure SQL Database.

Why?

Evergrowing databases with millions & Billions of rows.

Cost reduction

small latency
-------------------------------------------------------

Terminologies:

Cold Data/Historical data (> 10 years)
Warm Data (> 5 years)
Hot data/live data/on premise (< 5 years)
-----------------------------------------------------

How does StretchDB work?

Once you enable this feature for a table, SQL Server silently and transparently moves/migrates data to Azure SQL Database. When you query those tables, the query optimizer knows which rows are on your server and which rows are in Azure, and divides the workload accordingly. The query processing on the Azure rows takes place in Azure so the only latency is for the return of the rows over the network.

--------------------------------------------------------------------------------

Limitations for Stretch Database

  • Not all tables will be eligible for being Stretch-enabled. Certain table properties, data and column types, constraints, and indexes are not supported.
  • Memory-optimized and replicated tables.
  • Tables that contain FILESTREAM data, use Change Tracking or Change Data.
  • Data types such as timestamp, sql_variant, XML, geography or columns that are Always Encrypted.
  • Check and default constraints or foreign key constraints that reference the table.
  • You cannot run UPDATE or DELETE statements, or run CREATE INDEX or ALTER INDEX operations on a Stretch-enabled table.
-------------------------------------------------------------------------------
Backup and Restore of a Stretch Database
Currently in SQL Server 2016 when a database that is Stretch-enabled is backed up, a shallow backup is created which does not include the data that has been migrated to the Azure SQL database. It is expected that with the RTM release of SQL Server 2016 backing up a Stretch-enabled database will create a deep backup that will contain both local and stretched data. When restoring a database that is Stretch-enabled, you'll have to reconnect the local database to the remote Azure SQL Database. You do this by running the stored procedure sys.sp_reauthorize_remote_data_archive as a db_owner. If I now back up the Stretch-enabled AdventureWorks2014 database and restore it, I will no longer be able to query the StretchTest table until I reconnect to Azure SQL Database by running:
USE [AdventureWorks2014];
GO
EXEC sys.sp_reauthorize_remote_data_archive @azure_username,
@azure_password;
GO
----------------------------------------------------------------------------------
Un-migrate Data To migrate data back to local storage from an Azure SQL Database you will need to create a new local table with the same schema as the Stretch-enabled table. You then have to copy the data from the Stretch-enabled table into the new local table. Once the data is copied you drop the Stretch-enabled table and rename the new local table to the name of the Stretch-enabled table that was just dropped. You can only disable Stretch for a database once all Stretch-enabled tables have been dropped. If you drop a database enabled for Stretch, the local database is removed but the remote data is not; you will have to drop the remote database from the Azure management portal. --------------------------------------------------------------------------------- Some Queries used on StretchDB To enable Stretch Database on an instance run:
EXEC sys.sp_configure N'remote data archive', '1';
RECONFIGURE;
GO
-------------------
To monitor the migration of data to Azure you can query
sys.dm_db_rda_migration_status
---------------------

When restoring a database that is Stretch-enabled, you'll have to reconnect the local database to the remote Azure SQL Database. You do this by running the stored procedure 

USE [AdventureWorks2014];
GO
EXEC sys.sp_reauthorize_remote_data_archive @azure_username,
@azure_password;
GO
--------------------------
Check the status of data migration in a dynamic management view
sys.dm_db_rda_migration_status to see how many batches and rows of data have been migrated.
--------------------------- Tools StretchDB Migration advisor Stretch Database monitor.




Monday 30 January 2017

SQL Server In-Memory OLTP 2016



In-memory OLTP is a new feature introduced on SQL Server 2014.

Why In-Memory?

This feature is used to improve OLTP database application performance. Memory-optimized-tables are fully transactional, durable, and are accessed using Transact-SQL in the same way as disk-based tables.

  • ·         A query can reference both memory-optimized tables and disk-based tables.
  • ·         A transaction can update data in memory-optimized tables and disk-based tables.
  • ·         Stored procedures that only reference memory-optimized tables can be natively compiled into machine code for further performance improvements.


Benefits of this feature:

  • ·         In-Memory OLTP will improve performance best in OLTP with short-running transactions.
  • ·         Optimistic concurrency control that eliminates logical locks.
  • ·         Lock free objects that eliminate all physical locks and latches. Threads that perform transactional work don’t use locks or latches for concurrency control.
  • ·         Natively compiled stored procedures, which have significantly better performance than interpreted stored procedures, when accessing a memory-optimized table.

Deep dive into SQL Server in-memory OLTP

It mainly features two new data structures which are Memory-Optimized Tables, and Natively-Compiled Stored Procedures.

Memory-Optimized Tables

Memory-optimized tables are fully durable by default, and, like transactions on (traditional) disk-based tables, fully durable transactions on memory-optimized tables are fully atomic, consistent, isolated, and durable (ACID). Memory-optimized tables and natively compiled stored procedures support a subset of Transact-SQL.

Natively-compiled stored procedures
A natively-compiled stored procedure is a SQL Server object that can access only memory-optimized data structures such as memory-optimized tables, table variables, etc. 

Here comes SQL server 2016 In-Memory OLTP

SQL Server 2016 offers significant improvements in In-Memory feature.

Some of the important features with enhancements

In-Memory Performance

SQL Server 2016 in-memory technology provides improved query performance. For example, the same transactions that run against SQL Server 2014 memory-optimized tables will run 30 times faster when run against SQL Server 2016 memory-optimized tables.

Supported size for In-Memory Tables

In SQL Server 2014, size of memory optimized tables is maximum of 256 GB of memory.
In SQL Server 2016 the recommended maximum is 2 TB of memory for memory-optimized tables.

Schema and data changes

SQL Server 2014 in-memory OLTP does not allow you to modify memory-optimized tables once they are created. If you have to make changes to a memory-optimized table schema or rebuild its indexes, you must drop and re-create the memory-optimized table, using DROP TABLE and CREATE TABLE statements. 
From SQL Server 2016 in-memory OLTP, this is no longer a constraint; you can make changes to memory-optimized tables after creation, using the ALTER TABLE statement.

Transparent Data Encryption (TDE)

In SQL Server 2014, the TDE feature is not compatible with memory-optimized tables. That means data stored for memory-optimized tables on a MEMORY_OPTIMIZED_DATA file-group are not encrypted on disk

TDE function is fully compatible with SQL Server 2016 memory-optimized tables
  
AlwaysON

In SQL Server 2014, the visibility of data in memory-optimized tables on secondary replicas is delayed by few transactions.
SQL Server 2016 memory-optimized tables; like disk-based tables, all data is visible immediately to the user.
Below are the complete feature enhancements from 2014 to 2016
Feature/Limit
SQL Server 2014
SQL Server 2016
Maximum combined size of durable tables
256 GB
2 TB
LOB (varbinary(max), [n]varchar(max))
Not supported
Supported*
Transparent Data Encryption (TDE)
Not supported
Supported
Offline Checkpoint Threads
1
1 per container
ALTER PROCEDURE / sp_recompile
Not supported
Supported (fully online)
Nested native procedure calls
Not supported
Supported
Natively-compiled scalar UDFs
Not supported
Supported
ALTER TABLE
Not supported
(DROP / re-CREATE)
Partially supported
(offline – details below)
DML triggers
Not supported
Partially supported
(AFTER, natively compiled)
Indexes on NULLable columns
Not supported
Supported
Non-BIN2 collations in index key columns
Not supported
Supported
Non-Latin codepages for [var]char columns
Not supported
Supported
Non-BIN2 comparison / sorting in native modules
Not supported
Supported
Foreign Keys
Not supported
Supported
Check/Unique Constraints
Not supported
Supported
Parallelism
Not supported
Supported
OUTER JOIN, OR, NOT, UNION [ALL], DISTINCT, EXISTS, IN
Not supported
Supported
Multiple Active Result Sets (MARS)
(Means better Entity Framework support.)
Not supported
Supported
SSMS Table Designer
Not supported
Supported

Creating Memory optimized table

CREATE TABLE [Employee](
[EmployeeID] INT NOT NULL PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000000),
[Name] NVARCHAR(250) NOT NULL INDEX [IName] HASH WITH (BUCKET_COUNT = 1000000),
[EmployeeSince] DATETIME NULL
)
WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);

Below are keywords used in above query.
MEMORY_OPTIMIZED = ON
This clause triggers the database engine to consider this table as memory optimized table

NONCLUSTERED HASH
Memory optimized tables can have up to eight non-clustered indexes, they don’t support clustered indexes. These indexes don't duplicate data, but rather just point to the rows in the chain. In-memory OLTP maintains these indexes online and they get created each time during database recovery.

Only two types of Indexes can be created on In-Memory tables, i.e. Non Clustered Hash Index and Non Clustered Index (aka Range Index).

Hash index is an array of N buckets or slots, each one containing a pointer to a row.
Non Clustered Indexes are inherently Covering, and all columns are automatically INCLUDED in the Index.

BUCKET_COUNT
With index specification, you also need to specify the BUCKET COUNT.  The recommended value for this should be two times the expected number of unique values of that column.
The value of BUCKET_COUNT represents the number of buckets a Hash index has. We can think of a bucket as an index position in which a row address is stored.

DURABILITY = SCHEMA_AND_DATA
With memory optimized tables you can either specify DURABILITY = SCHEMA_AND_DATA or DURABILITY = SCHEMA_ONLY. 

DURABILITY = SCHEMA_AND_DATA is a default option, with this option changes to table data are logged in the SQL Server transaction log and data gets stored in a file-stream based file-group. So that data can be recovered after an unexpected crash or a server restart.

DURABILITY = SCHEMA_ONLY with this option changes to table data are not logged, only structure or schema of table is saved. After a server restart the table will be available, but it will be an empty table.
---------------------------------------------------------------------------------------------
In-Memory OLTP in Azure

In-Memory OLTP can be used to improve the performance of OLTP workload in Premium Azure SQL Databases without increasing the performance level. With In-Memory OLTP in SQL Server, customers achieve up to 30x transactional performance gains.
A memory-optimized table has one representation of itself in active memory, in addition to the standard representation on a hard drive. Business transactions against the table run faster because they directly interact with only the representation that is in active memory.
Premium databases created in November 2015 or later do support the In-Memory feature.
Check the table compatibility for In-Memory using Memory Optimization Advisor.

Bringing this technology to the cloud means customers will be able to take advantage of in-memory OLTP in a fully managed database-as-a-service with 99.99% SLA.




SQL Database Modes/States

A SQL database is always in one specific state. 

To verify the current state of a database, select the state_desc column in the sys.databases catalog view or the Status property in the DATABASEPROPERTYEX function.

The following table defines the database states.


State Definition
ONLINE                                                                                                                                         
Database is available for access. The primary filegroup is online, although the undo phase of recovery may not have been completed.
OFFLINE
Database is unavailable. A database becomes offline by explicit user action and remains offline until additional user action is taken. For example, the database may be taken offline in order to move a file to a new disk. The database is then brought back online after the move has been completed.
RESTORING
One or more files of the primary filegroup are being restored, or one or more secondary files are being restored offline. The database is unavailable.
RECOVERING
Database is being recovered. The recovering process is a transient state; the database will automatically become online if the recovery succeeds. If the recovery fails, the database will become suspect. The database is unavailable.
RECOVERY PENDING
SQL Server has encountered a resource-related error during recovery. The database is not damaged, but files may be missing or system resource limitations may be preventing it from starting. The database is unavailable. Additional action by the user is required to resolve the error and let the recovery process be completed.
SUSPECT
At least the primary filegroup is suspect and may be damaged. The database cannot be recovered during startup of SQL Server. The database is unavailable. Additional action by the user is required to resolve the problem.
EMERGENCY
User has changed the database and set the status to EMERGENCY. The database is in single-user mode and may be repaired or restored. The database is marked READ_ONLY, logging is disabled, and access is limited to members of the sysadmin fixed server role. EMERGENCY is primarily used for troubleshooting purposes. For example, a database marked as suspect can be set to the EMERGENCY state. This could permit the system administrator read-only access to the database. Only members of the sysadmin fixed server role can set a database to the EMERGENCY state.