Skip to content

Instantly share code, notes, and snippets.

@bertwagner
bertwagner / SQL Json Performance.sql
Last active February 19, 2017 13:41
Test data for SQL performance comparison
This file has been truncated, but you can view the full file.
-- Car data source: https://github.com/arthurkao/vehicle-make-model-data
IF OBJECT_ID('dbo.Cars') IS NOT NULL
BEGIN
DROP TABLE dbo.Cars;
END
CREATE TABLE dbo.Cars
(
Id INT IDENTITY(1,1),
CarDetails NVARCHAR(MAX)
);
@bertwagner
bertwagner / SQL Json Performance Truncated.sql
Created February 19, 2017 13:43
Test data for SQL performance comparison (truncated)
-- Car data source: https://github.com/arthurkao/vehicle-make-model-data
IF OBJECT_ID('dbo.Cars') IS NOT NULL
BEGIN
DROP TABLE dbo.Cars;
END
CREATE TABLE dbo.Cars
(
Id INT IDENTITY(1,1),
CarDetails NVARCHAR(MAX)
);
@bertwagner
bertwagner / SQL JSON Computed column.sql
Created February 19, 2017 14:00
Adding a computed column to a table storing JSON
-- Remember to turn on "Include Actual Execution Plan" for all of these examples
-- Before we add any computed columns/indexes, let's see our execution plan for our SQL statement with a JSON predicate
SELECT * FROM dbo.Cars WHERE JSON_VALUE(CarDetails, '$.model') = 'Golf'
/*
Output:
Id CarDetails
----------- --------------------------------------------------
1113 {"year":2001,"make":"VOLKSWAGEN","model":"GOLF"}
2410 {"year":2002,"make":"VOLKSWAGEN","model":"GOLF"}
@bertwagner
bertwagner / SQL pre2016 JSON performance.sql
Created February 19, 2017 14:29
Parsing JSON in pre-2016 SQL Server
-- Let's compare how quick Phil Factor's JSON parsing function does against the new SQL 2016 functions
-- Phil's parseJSON function can be downloaded from https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/
SELECT years.StringValue AS Year, makes.StringValue AS Make, models.StringValue AS Model FROM dbo.parseJSON(@cars) models
INNER JOIN dbo.parseJSON(@cars) years ON models.parent_ID = years.parent_ID
INNER JOIN dbo.parseJSON(@cars) makes ON models.parent_ID = makes.parent_ID
WHERE models.NAME = 'model' AND models.StringValue = 'Golf' AND years.NAME = 'year' AND makes.NAME = 'make'
@bertwagner
bertwagner / Json.NET performance test script.cs
Created February 20, 2017 00:45
Comparing performance between SQL Server 2016 and Json.Net JSON parsing
static void Main(string[] args)
{
string cars = @"[ {""year"":2001,""make"":""ACURA"",""model"":""CL""}, ... ]";
Stopwatch stopwatch = new Stopwatch();
// Test #1
stopwatch.Start();
var deserializedCars = JsonConvert.DeserializeObject<IEnumerable<Car>>(cars);
stopwatch.Stop();
long elapsedMillisecondsDeserialize = stopwatch.ElapsedMilliseconds;
@bertwagner
bertwagner / JSON SQL Index performance.sql
Last active February 20, 2017 17:10
Adding a nonclustered index to our computed column
-- Add an index onto our computed column
CREATE CLUSTERED INDEX CL_CarModel ON dbo.Cars (CarModel)
-- Check the execution plans again
SELECT DISTINCT * FROM dbo.Cars WHERE JSON_VALUE(CarDetails, '$.model') = 'Golf'
SELECT DISTINCT * FROM dbo.Cars WHERE CarModel = 'Golf'
-- We now get index seeks!
@bertwagner
bertwagner / SQL JSON performance comparison with Json.Net.sql
Last active February 20, 2017 17:21
Comparing performance of SQL Server 2016 vs Json.Net
-- Turn on stats and see how long it takes to parse the ~20k JSON array elements
SET STATISTICS TIME ON
-- Test #1
-- Test how long it takes to parse each property from all ~20k elements from the JSON array
-- SQL returns this query in ~546ms
SELECT JSON_VALUE(value, '$.year') AS [Year], JSON_VALUE(value, '$.make') AS Make, JSON_VALUE(value, '$.model') AS Model FROM OPENJSON(@cars, '$')
-- Test #2
-- Time to deserialize and query just Golfs without computed column + index
@bertwagner
bertwagner / SQL 2016 indexed computed performance.sql
Last active February 20, 2017 17:24
SQL Server 2016 reutrns data in 1ms for indexed computed column
-- Indexed computed column returns results in ~1ms
SELECT * FROM dbo.Cars WHERE CarModel = 'Golf'
@bertwagner
bertwagner / SQL to JSON datetime queries.sql
Last active March 23, 2017 22:02
SQL to JSON datetime conversion queries
DECLARE @sqlData datetime2 = '2017-03-28 12:45:00.1234567'
-- Let's first try the simplest SQL to JSON conversion first using FOR JSON PATH
SELECT @sqlData as SQLDateTime2 FOR JSON PATH
-- Output: [{"SQLDateTime2":"2017-03-28T12:45:00"}]
-- Honestly that's not too bad!
-- The datetime gets created in the YYYY-MM-DDTHH:MM:SS.fffffff format
-- Although this is pretty much what we need, what if we want to be explicit and specify that we are in UTC?
-- Just add the AT TIME ZONE modifier and we will get our JSON "Z" indicating UTC
@bertwagner
bertwagner / SQL to JSON datetime modifications.sql
Last active March 29, 2017 10:35
SQL to JSON datetime modifications
DECLARE @sqlDate datetime2 = '2017-03-28 12:45:00.1234567'
DECLARE @jsonData nvarchar(max) = N'{ "createDate" : "2017-03-28T12:45:00.000Z" }'
,@newDate datetime2(3) = '2017-03-28T12:48:00.123Z'
-- Let's start out modifying our data by replacing the value completely
SELECT JSON_VALUE(@jsonData, '$.createDate')