Azure SQL & SQL Server 2016 JSON
-
Upload
davide-mauri -
Category
Data & Analytics
-
view
163 -
download
3
Transcript of Azure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server JSONDAVIDE [email protected] - @MAURIDB HTTP://SPEAKERSCORE.COM/WP5Z
About Me Microsoft Data Platform MVP since 2006 Works with SQL Server from 6.5, on BI from 2003 Specialized in Data Solution Architecture, Database Design, Performance Tuning, High-Performance Data Warehousing, BI, Big Data
Very strong developer background Loves community!
◦ President of UGISS (Italian SQL Server UG) for 11 Years◦ AppDev VG Leader since 2017
Regular Speaker @ SQL Server events Director SW & Cloud @ Sensoria: http://www.sensoriafitness.com/ E-mail: [email protected] -Twitter: @mauridb Blog: http://sqlblog.com/blogs/davide_mauri/default.aspx
Agenda JSON in life JSON and SQL Server
◦ JSON “Datatype” (not really) ◦ Navigation via “dot” notation◦ LAX and STRICT path modes
◦ JSON Functions◦ JSON_VALUE, JSON_QUERY, JSON_MODIFY, ISJSON
◦ JSON & SQL◦ OPENJSON◦ FOR JSON◦ Indexes
◦ The Dynamic Schema problem (or “the Relational Division”)
JSON in Life JSON is everyone… …so I’m sure you know JSON pretty well For who doesn’t already know it:
◦ JavaScript Object Notation◦ Simple, Human-Readable, Text data format◦ Based on Key-Value pair idea
◦ Values can be: Scalar, Arrays, Key-Value pairs◦ No strict schema (yet)◦ Supported in any language, cross platform◦ De-facto standard
◦ http://www.ietf.org/rfc/rfc4627.txt
Image taken from wikipedia
JSON and SQL Server No support until SQL Server 2016 Before SQL Server 2016:
◦ One option is to use SQLCLR◦ Solutions available surfing the web:◦ http://www.sqlservercentral.com/articles/SQLCLR/74160/ ◦ http://www.json4sql.com/examples.html
◦ Another (more limited) option is a pure T-SQL solution◦ https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/
JSON and SQL Server Why native JSON support it’s important?
◦ Simplify application development◦ Especially in non-MS environments, since JSON is *very* well supported (eg. Python / JS)
◦ Reduces the impedance mismatch between app and db◦ Make schema extensibility a breeze
◦ But without having *only* “liquid” schemas. Schema is a good thing in general. Having the option to decide when and when not use it is a key feature today:
JSON and SQL Server 2016 Native support to JSON No specific datatype format (like XML)
◦ relies on varchar(max)
Specific function to ◦ manipulate and create JSON◦ extract data from JSON◦ turn relational data into JSON and vice-versa
JSON Path The dollar sign ($) represents the context item. The property path is a set of path steps.
◦ Key names. ◦ Array elements. Arrays are zero-based.◦ The dot operator (.) indicates a member
of an object.
“LAX” or “STRICT” option set invalid pathhandling◦ LAX: null◦ STRICT: error
$.conference.speaker
$.conference.speaker.editions[0]
JSON Path Like XPATH but for JSON
◦ http://goessner.net/articles/JsonPath◦ https://jsonpath.curiousconcept.com◦ http://jsonpath.com
SQL Server 2016 doesn’t offer a full support yet. ◦ Is a subset of JSONPath
JSON Functions Scalar functions to manage JSON data
◦ ISJSON: test if string is JSON◦ JSON_VALUE: extract a scalar value◦ JSON_QUERY: extract a JSON value◦ JSON_MODIFY: modify a JSON property
DEMOSQL/JSON FIRST CONTACTS
JSON Functions Tabular functions
◦ OPENJSON: turn a JSON object into a table◦ FOR JSON: turn a table into a JSON object
OPENJSON Support both implicit and explicit schema:
IMPLIC
ITEXPLICIT
Type Meaning
0 null
1 string
2 int
3 true/false
4 array
5 object
OPENJSON Very useful with OPENROWSET to import JSON
FOR JSON Two modes: AUTO, PATH
AUTO: shape of JSON using the SELECT statement as guideline
PATH: column alias and JSON_QUERY function allows for total control of final JSON
DEMOJSON AND RELATIONS
JSON Indexes JSON data support indexes…even if it doesn’t seems so!
◦ Right, there is no special index like it happens with XML
How to index JSON Data◦ Create calculated columns for scalar JSON values you know if want to index◦ Create FULLTEXT index to index whole JSON document◦ Use the FULLTEXT index to perform “near” term search
◦ CONTAINS(JSON_DATA, ‘NEAR((“Key”, “Value”),1)’)◦ Uses a “Generalized Inverted Index” (like Postgres)
◦ https://msdn.microsoft.com/en-us/library/cc879306.aspx
Indexes to support JSON Path queries (like XML) are not yet there◦ You have to DIY here (Service Broker to async update EAV table)
DEMOJSON INDEXING
JSON & Stored Procedures Perfect to pass an object
◦ Dev happy because they can just pass an object◦ DBA happy because:
◦ No SQL-Injection problems◦ Can use the passed object as json or a relational object as they wish
◦ Everyone’s happy◦ Easy to pass arrays of objects◦ Easy to create UPSERT procedures (everyone really LOVE this)◦ But keep in mind that BULK INSERT is still the way to got to bulk load data (say when you have 10000
or more rows to insert)◦ Performances are great!
Dynamic Schema JSON sounds like a perfect solution of all the “Dynamic Schema” situations
Why “Dynamic Schema” and not Schemaless? ◦ Because schemaless does not *really* exists! In reality a schema always
exists, albeit implicit, otherwise it would be impossible to handle data
Implict Schema
Any data that doesn't fit this implicit schema will not be manipulated properly, leading to errors.
(Schemaless data structures, Martin Fowler)
Words of Wisdom
«Schemaless => implicit schema = bad. Prefer an explicit schema»(Schemaless data structures, Martin Fowler)
Dynamic Schema What if my use case is one that perfectly fits the need for a implicit schema?
The only possible solution are the so-called «No-SQL» databases◦ Document Database or Key-Value store?◦ How can I integrate it into already existing database?◦ Integration does not come for free!
Now with SQL Server 2016 we have an additional option! ◦ No integration problems, No added complexity◦ Great Performances◦ Works within a well-known platform
DEMODYNAMIC SCHEMA
Last things JSON and the new COMPRESS function works well together:
◦ Compress using GZIP algorithm (Natively supported by browsers)◦ Simplify app development and integration with rdbms◦ Sample usage with node.js◦ http://www.codeproject.com/Articles/1063475/Pushing-Compression-from-n
ode-js-to-SQL-Server
JSON is also supported on SQL Azure◦ Requires server version V12◦ Right now (2017) has *less* limitations than SQL Server 2016
Last things Works *incredibly well* with columnstore
◦ Up to 25x data compression◦ SQL Server vNext e SQL Azure only◦ https://blogs.msdn.microsoft.com/sqlserverstorageengine/2017/02/09/extre
me-25x-compression-of-json-data-using-clustered-columnstore-indexes/
Conclusions JSON does not substitute relational solutions
◦ Can be useful to extend them◦ Can be *very* useful to simplify app-to-db communications
◦ JSON as parameter value! That’s a dream
FAQ: ◦ https://msdn.microsoft.com/en-us/library/mt631706.aspx
Follow SQL Server JSON PM:◦ https://blogs.msdn.microsoft.com/sqlserverstorageengine/tag/json/
Help us to make JSON support better!
Don't restrict JSON_VALUE and JSON_QUERY to string literals only for the path◦ https://connect.microsoft.com/SQLServer/Feedback/Details/2235470
Support JSON in COLUMNSET for SPARSE columns◦ https://connect.microsoft.com/SQLServer/feedback/details/2533991
Help us to make JSON support better!
Allow support for JSON of simple arrays generation◦ https://connect.microsoft.com/SQLServer/Feedback/Details/1383569
JSON_VALUE: adding another optional parameter, which defines what type of value the function will return◦ https://connect.microsoft.com/SQLServer/Feedback/Details/2543881
Thanks!QUESTIONS?
Demos available on GitHubHTTPS://GITHUB.COM/YOREK/SQLSAT613