Upload
ivo-andreev
View
66
Download
0
Embed Size (px)
Citation preview
Native JSON support
in SQL 2016
Sponsors
Gold sponsors:
Silver sponsors: Bronze sponsors:
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
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
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
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
“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
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
JSON Support Overview
FOR JSON [PATH|AUTO]
OPENJSON()
ISJSON()
JSON_QUERY()
JSON_VALUE()
JSON_MODIFY()
STRING_ESCAPE(text, type)
Import – Export JSON
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 = …
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
Demo
FOR JSON
OPENJSON
JSON JOINS
Query JSON Data
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
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
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
…may not be what it sounds like!
Native …
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
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
Solving real-life problems
“I couldn’t repair your brakes,
so I made your horn louder!”
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,…)
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
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
Demo
In-Memory OLTP
JSON_MODIFY
JSON Indexing
How can you benefit
from JSON in SQL
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
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
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
Sponsors
Gold sponsors:
Silver sponsors: Bronze sponsors: