Wednesday, February 8, 2017

Create memory Optimized table in SQL 2016


  • In memory OLTP Enhancement
Memory optimized tables speed up transnational workloads, high concurrency and too many latches by moving data from disk-based to memory optimized tables.

It helps to improve the performance of DW staging by using non-durable, memory-optimized tables as staging tables.

Features which Memory optimized tables support/not support

Support
Not Support
Check Constraints
Replication
Unique Constraints
Database Snapshots
Foreign Keys : As long as foreign key reference to a “primary key”
Remove “ON” clause from “create table statement”
Triggers: for insert/update/delete as long as it use “with native Compilation”
Datetimeoffset/ Hierachy id
Columns with large object types(LOB): Varchar(max), Nvarchar(max), Binary(max)
DBCC checkDB / DBCC checktable
Noncluster index: specify primary key nonclustered.
Clustered index
Unique/nullable index key columns
Create index: Must be specified with create statement or alter table statement.

Scaling Memory Optimized Tables
  • Can put more date into memory optimized tables.
  • Support bigger workloads.
  • Can store upto 2TB of data when you create a durable table by using "Schema_and_data" option in the create table statement.
  • Create memory optimized table with row size > 8060 bytes even without a LOB column
  • Support parallel scans of all indexes (nonclustered + hash). It is beatifically when you have queries which scan large data sets.
Create Memory Optimized table.

Before creating memory optimized table you should complete below 2 steps.
  • Create memory-optimized database file group 
  • Create filestream database file.
If you are unable to fulfill above steps before creating memory optimized tables, SQL Server engine will rise an exception "Cannot create memory optimized tables in a database that does not have an online and non-empty MEMORY_OPTIMIZED_DATA filegroup".

Note:
  • You can only create one memory-optimized file group per database.
  • You can create the file group when you create the database or you can add it later.
There are 2 ways to accomplish it.
  1. Using SQL Query statements
  2. Using DB interface.
Ok then lets start.

Assumption: Lets assum that you have alreay created the Database.

1.Using SQL Query statements


Create in memory optimized file group

ALTER DATABASE DB NAME ADD FILEGROUP FILEGROUP NAME CONTAINS MEMORY_OPTIMIZED_DATA  

Ex: ALTER DATABASE SQL2016 ADD FILEGROUP imoltp CONTAINS MEMORY_OPTIMIZED_DATA 

Create filestream database file

ALTER DATABASE DB NAME ADD FILE (NAME='NAME OF THE DB FILE', FILENAME='PATH WHERE FILE IS STORED ') TO FILEGROUP FILEGROUP NAME

Ex: ALTER DATABASE SQL2016 ADD FILE (name='imoltp_mod1', filename='C:\Program Files (x86)\SQL2016_IMOLTP') TO FILEGROUP imoltp

Now Crete in memory optimized table with durability to store more than 2tb of data

CREATE TABLE ProductReviewHistory (
ProductID INT IDENTITY PRIMARY KEY NONCLUSTERED,
ProductEnglishDescription NVARCHAR(4000),
ProductReviewDate DATETIME,
ProductSpanishReviewComments NVARCHAR(4000),
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);

2. Using DB Interface

Create in memory optimized file group



Create filestream database file


Now Crete in memory optimized table

CREATE TABLE ProductReviewHistory (
ProductID INT IDENTITY PRIMARY KEY NONCLUSTERED,
ProductEnglishDescription NVARCHAR(4000),
ProductReviewDate DATETIME,
ProductSpanishReviewComments NVARCHAR(4000),
) WITH (MEMORY_OPTIMIZED = ON);

Cheers :)
Thilini

Tuesday, February 7, 2017

Installing SQL Server 2016 and Business Intelligence features - Step by Step

SQL 2016 installation is totally different compared to previous version. You will be struggling when you try it first time due to unawareness and difficulty to find comprehensive details in one place.

One of the main difference is you should install SSMS as a separate installation. It is not included in SQL Server 2016 setup.

There are 3 main steps to fulfill SQL 2016 installation with Business Intelligence components.

01. Install SQL 2016 Server.
02. Install SQL Server Management Studio (SSMS).
03. Install SQL 2016 Data Tool (SSDT).

Here I tried installing SQL 2016 Developer edition. Because It is free :) . You can now download SQL Server 2016 Developer Edition by joining https://www.visualstudio.com/dev-essentials/

Lets start.

01. Install SQL 2016 Server

Once you have SQL Server 2016 Developer Edition, start the installation by executing setup.exe.

The SQL Server Installation Wizard provides a feature tree to install all SQL Server components:
  • Database Engine
  • Analysis Services
  • Reporting Services
  • Integration Services
  • Master Data Services
  • Data Quality Services
  • Connectivity components



02. Install SQL Server Management Studio (SSMS)

As per next setup fundamental, SQL Server Management Studio is a separate installer and should be installed separately.

Once the SQL Server 2016 installation completed, I clicked the “Install SQL Server Management Tools” link on the SQL Server Installation Center’s Installation page.



The link takes me to a page titled Download SQL Server Management Studio. It contains a link to install SSMS. msdn.microsoft.com/en-us/library/mt238290.aspx



03. Install SQL 2016 Data Tool (SSDT)

Note: SSDT works best with Visual Studio 2015.If you do have Visual Studio 2015 installed on your machine, installing SSDT will add the full set of SQL Server tools into your existing Visual Studio installation. The recommendation is to use Visual Studio 2015 to get the best experience when developing T-SQL.

Once SSMS is installed, click the “Install SQL Server Data Tools” link.


You can download SSDT directly from https://docs.microsoft.com/en-us/sql/ssdt/download-sql-server-data-tools-ssdt



SSDT 2016 can run on the following operating systems.
  • Windows 10 (x86 and x64)
  • Windows 8, 8.1 (x86 and x64)
  • Windows 7 SP1 (x86 and x64)
  • Windows Server 2012 (x64), R2 (x64)
  • Windows Server 2008 R2 SP1 (x64)
That is it. Hope it will help you to get the basic installation knowledge.

Thanks & Cheers :)
Thilini

Sunday, February 5, 2017

Data Driven Subscriptions


Step 1


Step 2


Step 3


Code - Output ( Daily Buffer Report as at 05-Feb-2017)
ReportName with DD-MON-YYYY

SELECT
'Daily Buffer Report as at ' +
              REPLACE
            (
                CONVERT(VARCHAR(11), GETDATE()-1, 106),
               ' ',
              '-'
           )

AS [EmailSubject]


Case study

Lets assume that you have a list of plants and each plant should get data specific to them only. Plant QLD should get a report having QLD plant related data.

Example:

PlantDDSEmails Table for Data Driven Subscription
Plant
Email
QLD qld@gmail.com
VIC
vic@gmail.com
NSW
nsw@gmail.com

Code

SELECT Email,Plant,

'QLD Debtors Dashboard as at '+
REPLACE
CONVERT(VARCHAR(11), GETDATE()-1, 106),
' ', 
'-' 
) as [EmailSubject] 
FROM dbo.PlantDDSEmails where Plant='QLD'

OR


SELECT 
 ( select plant +' Debtors Dashboard as at ' from dbo.PlantDDSEmails 
  where Plant ='QLD'
 ) +
REPLACE  
(
     CONVERT(VARCHAR(11), GETDATE()-1, 106),
     ' ', 
     '-' 
)AS [EmailSubject]

Step 4




Step 5




Step 6




Step 7



Step 8

Click Finish button.

Thanks & Cheers :)
Thilini

SQL Joins

There are mainly 3 types of joins.
  • INNER
  • OUTER
  • CROSS

·  INNER JOIN

The INNER JOIN keyword return rows when there is at least one match in both tables.

Example SQL statement:

SELECT * FROM Individual AS Ind

INNER JOIN Publisher AS Pub

ON Ind.IndividualId = Pub.IndividualId


LEFT= Individual             RIGHT- Publisher

Individual
ID First Name Last Name User Name
1
Kamal PereraK
2
Nimal Silve N
3
Saman Peries S
4
Amal Fernando A
5
Bimal Bandara B

Publisher
Individual ID
Acces Level
1
Admin
2
Contri
3
Contri
4
Contri
10
Admin









Result
Individual ID First Last User AID Access Level
1
Kamal Perera K
1
Admin
2
Nimal

Silve

N

2
Contri
3
Saman Peries S
3
Contri
4
Amal Fernando A
4
Contri


·       OUTER JOIN is of 3 types

1. LEFT OUTER JOIN

Use this when you only want to return rows that have matching data in the left table, even if there are no matching rows in the right table.

Example SQL statement:

LEFT= Individual       RIGHT- Publisher

SELECT * FROM Individual AS Ind

LEFT JOIN Publisher AS Pub

ON Ind.IndividualId = Pub.IndividualId

Individual
IDFirst NameLast NameUser Name
1
KamalPereraK
2
NimalSilveN
3
SamanPeriesS
4
AmalFernandoA
5
BimalBandaraB
  
Publisher
Individual ID
Acces Level
1
Admin
2
Contri
3
Contri
4
Contri
10
Admin







Result
Individual ID First Last User AID Access Level
1
Kamal Perera K
1
Admin
2
Nimal

Silve

N

2
Contri
3
Saman Peries S
3
Contri
4
Amal Fernando A
4
Contri
5
Bimal Bandara B NULL NULL


2. RIGHT OUTER JOIN



Use this when you only want to return rows that have matching data in the right table, even if there's no matching rows in the left table.

Example SQL statement:

LEFT= Individual        RIGHT- Publisher

SELECT * FROM Individual AS Ind

RIGHT JOIN Publisher AS Pub

ON Ind.IndividualId = Pub.IndividualId

Individual
IDFirst NameLast NameUser Name
1
KamalPereraK
2
NimalSilveN
3
SamanPeriesS
4
AmalFernandoA
5
BimalBandaraB
  
Publisher
Individual ID
Acces Level
1
Admin
2
Contri
3
Contri
4
Contri
10
Admin









Result
Individual ID First Last User AID Access Level
1
Kamal Perera K
1
Admin
2
Nimal

Silve

N

2
Contri
3
Saman Peries S
3
Contri
4
Amal Fernando A
4
Contri
NULL NULL NULL NULL
10
Admin











3.FULL OUTER JOIN

Use this when you want to all rows, even if there are no matching rows in the right table.

Example SQL statement:

LEFT= Individual        RIGHT- Publisher

SELECT * FROM Individual AS Ind

FULL JOIN Publisher AS Pub

ON Ind.IndividualId = Pub.IndividualId

Individual
IDFirst NameLast NameUser Name
1
KamalPereraK
2
NimalSilveN
3
SamanPeriesS
4
AmalFernandoA
5
BimalBandaraB
  
Publisher
Individual ID
Acces Level
1
Admin
2
Contri
3
Contri
4
Contri
10
Admin









Result
Individual ID First Last User AID Access Level
1
Kamal Perera K
1
Admin
2
Nimal

Silve

N

2
Contri
3
Saman Peries S
3
Contri
4
Amal Fernando A
4
Contri
5
Bimal Bandara B NULL NULL
NULL NULL NULL NULL 10 Admin











Note
  • A OUTER join has to be LEFT | RIGHT | FULL you cannot simply say OUTER JOIN 
  • You can drop OUTER keyword and just say LEFT JOIN or RIGHT JOIN or FULL JOIN
  • If you just mention JOIN then by default it is a INNER JOIN 

 ·       CROSS JOIN
  • The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table.
  • If ‘WHERE’ clause is not used along with CROSS JOIN, This kind of result is called as Cartesian product.
  • If, WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN.

Example SQL statement:

Select * from Individual

Cross JOIN Publisher



Thanks & Cheers :) 
Thilini