Top Banner
Native JSON support in SQL 2016
30

Native JSON Support in SQL2016

Feb 13, 2017

Download

Software

Ivo Andreev
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Native JSON Support in SQL2016

Native JSON support

in SQL 2016

Page 2: Native JSON Support in SQL2016

Sponsors

Gold sponsors:

Silver sponsors: Bronze sponsors:

Page 3: Native JSON Support in SQL2016

About me

3 |

Project Manager @ 14 years professional experience

.NET Web Development MCPD

External Expert Horizon 2020

External Expert Eurostars

Business Interests

Web Development, SOA, Integration

Security & Performance Optimization

IoT, Computer Intelligence

Contact [email protected]

www.linkedin.com/in/ivelin

www.slideshare.net/ivoandreev

Page 4: Native JSON Support in SQL2016

Agenda

4 |

Beginner Why JSON

SQL 2016 and Competitors

Intermediate Native JSON support in SQL 2016

JSON functions and Data Type?

Advanced Modelling

Indexing & Optimizations

Page 5: Native JSON Support in SQL2016

5 |

About JSON

Do you know JSON? Textual data format

Lightweight

Language independent

JSON is VIP Data interchange

Document DBs, log files, config files

REST service endpoints

AJAX-enabled web pages

Page 6: Native JSON Support in SQL2016

JSON vs XML

Common data interchange formats

JSON is data format, XML is markup

JSON is lighter that XML

• network transfer, memory & storage

JSON parsing is faster than XML

Can be validated against schema

JSON stores in arrays, XML in trees

XML requires transformation

JSON is natively parsed in JavaScript

Page 7: Native JSON Support in SQL2016

“The biggest leap forward in MS data platform history”

SQL 2016 Engine – What’s New

Live query statistics Query optimizer improvements

Query Store Column store indexes on in-memory

tables

Temporal Tables (history) Schema changes on in-memory

tables

Stretch DB to Azure TSQL changes for in-memory tables

Row level security Replication to Azure

Always encrypted JSON support

Dynamic data masking

Page 8: Native JSON Support in SQL2016

8 |

MongoDB BSON documents (binary)

PostgreSql JSON – exact copy of JSON text, UTF8 required

JSONB – binary (no spaces, order and duplicates) DataType validation

Supports indexing

SQL Server (and Oracle 12c) JSON as NVARCHAR

T-SQL statements for processing

Azure DocumentDB

SQL Server and Competitors

Page 9: Native JSON Support in SQL2016

JSON Support Overview

FOR JSON [PATH|AUTO]

OPENJSON()

ISJSON()

JSON_QUERY()

JSON_VALUE()

JSON_MODIFY()

STRING_ESCAPE(text, type)

Page 10: Native JSON Support in SQL2016

Import – Export JSON

Page 11: Native JSON Support in SQL2016

OPENJSON

Converts JSON to relational format

Returns table with one row f/e element

Output Schema

DEFAULT (Key-Value-Type schema) JSON Type (Null=0, String=1, Int=2, Bool=3, Array=4, Obj=5)

EXPLICIT (user defined schema, WITH clause) Specify path of JSON array (i.e. $.Order)

JOIN parent - child JSON entitiesSELECT … FROM [TABLE] CROSS APPLY OPENJSON (jsonText) JT

WHERE JT.ID = …

Page 12: Native JSON Support in SQL2016

FOR JSON

Converts relational data to JSON

Each row converted to a JSON object

Nested arrays generated when JOIN used

Formatting

FOR JSON AUTO

FOR JSON PATH

ROOT keyword

Type mapping

TSQL Type JSON Type

NVarChar, VarChar String

Bit Boolean

DateTime, DateTime2, Date, Time String

Binary types BASE64

GUID, Money String

Int, BigInt, Decimal, Numeric Number

Page 13: Native JSON Support in SQL2016

Demo

FOR JSON

OPENJSON

JSON JOINS

Page 14: Native JSON Support in SQL2016

Query JSON Data

Page 15: Native JSON Support in SQL2016

JSON_VALUE

Query JSON data, returns scalar valueSELECT JSON_VALUE(jsonText, ‘[lax|strict] path’)

PATH $ - entire JSON object

'$[2]' –3-rd element in JSON array

'$.property1' – property1 in JSON object

'$.info.“First Name”' – property of “info” object

Does not return values with length > 4000

Modes LAX(default) and STRICT

Page 16: Native JSON Support in SQL2016

JSON_QUERY

Query JSON data, returns object or arraySELECT JSON_QUERY(jsonText, ‘[lax|strinct] path’);

Similar to JSON_VALUE

PATH

Modes

LAX(default) and STRICT

Page 17: Native JSON Support in SQL2016

Updating (Patching)

Updates property value, returns updated JSONUPDATE … SET jsonCol = JSON_MODIFY (jsonCol, path, value)

Usage

Update value

Insert new key:value

Delete key

Limitations

Requires parsing of JSON properties in type

Update only one property at a time

Page 18: Native JSON Support in SQL2016

…may not be what it sounds like!

Native …

Page 19: Native JSON Support in SQL2016

What’s good?

Dev. tools update not necessary (ADO.NET, EF)

No conversion to string on export (as Binary JSON)

No migration necessary (pre-existing solutions)

Feature compatibility

Full-text search

In-memory OLTP

Column-store indexes

Row-level security

JSON in UDF

Page 20: Native JSON Support in SQL2016

What’s bad ?

https://connect.microsoft.com/SQLServer/feedback/details/673824/add-native-support-for-json-to-sql-server-a-la-xml-as-in-for-json-or-from-openjson

https://blogs.msdn.microsoft.com/jocapc/2015/05/16/json-support-in-sql-server-2016/ (Read the comments below)

No native data type (like XML)

No ability to: Update property w/o reinserting

Index (fast search) JSON data

Search on property value (efficiently)

Microsoft say: Focus is on functionality rather than storage

If necessary, performance will be improved

You can still use CLR to parse JSON

JSONB is not necessarily better than CLR

Page 21: Native JSON Support in SQL2016

Solving real-life problems

“I couldn’t repair your brakes,

so I made your horn louder!”

Page 22: Native JSON Support in SQL2016

Modelling

Hybrid RDBMS-JSON model in the same DB

Performance In-memory OLTP

Storage Unicode compression (up to 50% ratio)

COMPRESS/DECOMPRESS statements

ValidationALTER TABLE … ADD CONSTRAINT … CHECK (ISJSON([InfoJson])>0)

Usage Read/write regular string (C#, JAVA, Node.js,…)

Page 23: Native JSON Support in SQL2016

Indexing

Non-clustered Index (NVARCHAR(n))

FULLTEXT Index (in single language)

Computed column (JSON_VALUE & standard index)

Query optimizer uses the index if JSON_VALUE(col, PATH) used

Structured search for PATH

No dynamic search

No hierarchy indexing

External catalog index (EAV pattern)

Elasticsearch

Page 24: Native JSON Support in SQL2016

Integration

SSIS (SQL Server Integration Services)

REST service data retrieval

Required .NET for CLR processing

Elasticsearch (or documentDB)

11th DB by rating, +30% popularity (since Oct’2015)

http://db-engines.com/en/ranking_trend/system/Elasticsearch

ES import/export from SQL

Pre-2016 required manual Tabular-JSON conversion

Page 25: Native JSON Support in SQL2016

Demo

In-Memory OLTP

JSON_MODIFY

JSON Indexing

Page 26: Native JSON Support in SQL2016

How can you benefit

from JSON in SQL

Page 27: Native JSON Support in SQL2016

Usage Scenarios

Extend existing records

Add JSON column

Data Warehouse and ETL

Service integration

Cross-platform support and communication

Don’ts

Making SQL a Key-Value store

Too large JSON documents

Adding FOR JSON to all queries

Page 28: Native JSON Support in SQL2016

Tools

JSON Formatters https://jsonformatter.curiousconcept.com/

http://jsonformatter.org/

JSON Parser Newtonsoft JSON.NET (http://www.newtonsoft.com/json)

FastJson (2x faster) (https://fastjson.codeplex.com/)

JSON.SQL For pre-2016 SQL CLR JSON serializer/deserializer http://www.sqlservercentral.com/articles/SQLCLR/74160/

JSON4SQL Paid PostgreSQL JSONB ported to SQL Server

Page 29: Native JSON Support in SQL2016

References

JSON in SQL Server 2016https://blogs.technet.microsoft.com/dataplatforminsider/2016/01/05/json-in-sql-server-2016-part-1-of-4/

https://blogs.technet.microsoft.com/dataplatforminsider/2016/01/06/json-in-sql-server-2016-part-2-of-4/

https://blogs.technet.microsoft.com/dataplatforminsider/2016/01/07/json-in-sql-server-2016-part-3-of-4/

https://blogs.technet.microsoft.com/dataplatforminsider/2016/01/08/json-in-sql-server-2016-part-4-of-4/

Microsoft SQL Server Sampleshttps://github.com/microsoft/sql-server-samples

Jovan Popovic (Program Manager, Microsoft)https://blogs.msdn.microsoft.com/jocapc/2015/05/16/json-support-in-sql-server-2016/

https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/09/30/indexing-json-documents-in-sql-server-2016/

AdventureWorks DB and Samples (SQL 2016)https://www.microsoft.com/en-us/download/details.aspx?id=49502

Page 30: Native JSON Support in SQL2016

Sponsors

Gold sponsors:

Silver sponsors: Bronze sponsors: