SlideShare a Scribd company logo
Use JSON to Slash Your Database Performance.pdf
WELCOME
Into the Box 2025: The Future is Dynamic!
Code Necromancer — Raising Legacy Code from the Dead
Bill Reese
• 25+ ye
a
rs of experience in full-st
a
ck
a
pplic
a
tion
a
nd web development
• Using ColdFusion since version 4
a
nd ColdBox
since version 2
• B
a
sed ne
a
r Orl
a
ndo Florid
a
• M
a
rried with three teen
a
gers
• Love Movies, Music
a
nd Photogr
a
phy
• Excited to be cre
a
ting BoxL
a
ng content for
CFC
a
sts
• This is my
f
irst time spe
a
king @ ITB
BReese@OrtusSolutions.com
Senior Developer - Ortus Solutions
@Studio7718
Bill Reese - Into The Box 2025
No hockey mask needed!
Use JSON to Slash Your
Database Performance
Collection of ways to deliver data directly from the database using JSON
• We will be using
a
simple demo d
a
t
a
b
a
se of movies
a
nd TV shows with rel
a
ted t
a
bles for
a
ctors, c
a
tegories
a
nd v
a
rious met
a
d
a
t
a
.
• We will be getting movie
a
nd TV show inform
a
tion from the d
a
t
a
b
a
se using di
ff
erent methods
a
nd delivering th
a
t d
a
t
a
to
a
nd from REST API endpoints.
• JSON D
a
t
a
Types = string, number, boole
a
n,
a
rr
a
y, object
1. Cl
a
ssic Method - Builds JSON document on
a
pplic
a
tion server using seri
a
lizing
a
nd
deseri
a
lizing function
a
lity.
vs
2. JSON Method - Builds JSON document on d
a
t
a
b
a
se server using built-in JSON function
a
lity.
What will we be doing?
Why would I want to use JSON in the Database?
Think di
ff
erently about your data. Does this get updated frequently?
• D
a
t
a
type conversion issues
• Less d
a
t
a
b
a
se tr
a
ns
a
ctions, connections, queries
• F
a
ster response time (
a
lmost
a
ll of the time)
• Less
a
pplic
a
tion server overhe
a
d
• Quick sub-object cre
a
tion
• Utilize In-Memory stor
a
ge of JSON documents
• Utilize Query C
a
ching of JSON documents
SQL Functionality Covered
Some of the things we will be using in this demo
Microsoft SQL Server Function
a
lity Covered
• FOR JSON PATH
• STRING_AGG
• STUFF & XML PATH
• JSON_QUERY
• OPENJSON
• OPENROWSET (Bulk)
• ISJSON
• STRING_ESCAPE
JSON Document Form
a
tting Topics
• Using l
a
bels to build node p
a
ths
• Adding
a
root node(s)
• D
a
t
a
type conversion
• When to esc
a
pe in strings
• Returning
a
n
a
rr
a
y or single object
Data Type Conversion
SQL use the following rules when converting your data to JSON
SQL Server data type JSON data type
char, nchar, varchar, nvarchar string
int, bigint,
fl
oat, decimal, numeric number
bit Boolean (true or false)
date, datetime, datetime2, time, datetimeo
ff
set string
varbinary, binary, image, timestamp/rowversion BASE64-encoded string
geometry, geography, other CLR types Not supported. These types return an error.
In the SELECT statement, use CAST or CONVERT, or use a CLR property or
method, to convert the source data to a SQL Server data type that can be
converted successfully to a JSON type. For example, use STAsText() for
the geometry type, or use ToString() for any CLR type. The type of the
JSON output value is then derived from the return type of the conversion that
you apply in the SELECT statement.
uniqueidenti
fi
er, money string
Testing
We will compare both versions and see how they stack up against each other.
Wh
a
t
a
re we testing?
• Request Response Time
• D
a
t
a
b
a
se Response Time
• D
a
t
a
b
a
se connections
• Memory Us
a
ge
Testing Output
• 1000 Records
Getting up and running
Application Setup
D
a
t
a
b
a
se Requirements
Microsoft SQL Server Version 2016+
COMPATIBILITY_LEVEL >= 130
CLOB en
a
bled
BULK permission required for Insert
a
nd upd
a
te method used
Demo App Inst
a
ll (Comm
a
ndBox)
Applic
a
tion Setup
1. Setup D
a
t
a
source
2. Cre
a
te Endpoints
3. Cre
a
te Models
4. Server start
Applic
a
tion Server Options
• BoxL
a
ng
• Lucee
• Adobe ColdFusion
NOTE: MySQL
a
nd PostgreSQL h
a
ve simil
a
r
function
a
lity
• Query output should be
a
rr
a
y form
a
t.
• Some documents m
a
y be too big
a
nd get split into sep
a
r
a
te rows in the
recordset. All rows of the
a
rr
a
y need to be combined to form the single JSON
String.
• JsonSqlRestHandler will h
a
ndle the SQL output
a
nd prep
a
re it for returning.
• returnStringJSON = Enc
a
psul
a
tes d
a
t
a
m
a
rsh
a
ling
• decodeSQLJSON = Combines recordset output into single string
• jsonMeta = Appends st
a
nd
a
rd REST nodes if desired
* This function
a
lity c
a
n be incorpor
a
ted using DI or through inherit
a
nce
Di
ff
erent Output Methods
Handling the JSON string from the database and how it is returned
JsonSqlRestHandler - returnStringJSON
JsonSqlRestHandler - decodeSQLJSON
JsonSqlRestHandler - jsonMeta
Only Needed if trying to mimic
CB st
a
nd
a
rd REST returns.
Output Code Demo
Read (Single Table)
Single Table
• FOR JSON PATH
{
"data": [
{
"showId": 70104894,
"title": "Friday the 13th",
"type": "Movie",
"releaseYear": 2009,
"rating": "R",
"durationMinutes": 97,
"durationSeasons": 0,
"dateAdded": "2020-01-01"
},
{
"showId": 70140450,
"title": "The IT Crowd",
"type": "TV Show",
"releaseYear": 2013,
"rating": "TV-MA",
"durationMinutes": 0,
"durationSeasons": 5,
"dateAdded": "2017-08-01"
}
],
"error": false,
"pagination": {
"totalPages": 1,
"maxRows": 0,
"offset": 0,
"page": 1,
"totalRecords": 0
},
"messages": []
}
Read (Lists)
Related Data as Displayable List
Output directly to UI
a
s
a
list of v
a
lues with no
need to inter
a
ct with or loop through the d
a
t
a
.
• FOR JSON PATH
• STRING_AGG
• XML PATH
{
"data": [
{
"showId": 70104894,
"title": "Friday the 13th",
"type": "Movie",
"releaseYear": 2009,
"rating": "R",
"durationMinutes": 97,
"durationSeasons": 0,
"dateAdded": "2020-01-01",
"categories": "Horror Movies",
"cast": "Aaron Yoo, Jonathan Sadowski, Derek Mears, Arlen Escarpeta, Danielle
Panabaker, Amanda Righetti, Ryan Hansen, Travis Van Winkle, Julianna Guill, Ben Feldman,
Jared Padalecki",
"directors": "Marcus Nispel"
},
{
"showId": 70140450,
"title": "The IT Crowd",
"type": "TV Show",
"releaseYear": 2013,
"rating": "TV-MA",
"durationMinutes": 0,
"durationSeasons": 5,
"dateAdded": "2017-08-01",
"categories": "British TV Shows, Classic & Cult TV, International TV Shows",
"cast": "Richard Ayoade, Katherine Parkinson, Matt Berry, Chris O'Dowd"
}
],
"error": false,
"pagination": {
"totalPages": 1,
"maxRows": 0,
"offset": 0,
"page": 1,
"totalRecords": 0
},
"messages": []
}
Read (Objects)
Related Data as Array of Object with Properties
Ability to loop though
a
n
a
rr
a
y of objects
with properties.FOR JSON PATH
• FOR JSON PATH on Sub-queries
{
"data": [
{
"showId": 70104894,
"title": "Friday the 13th",
"type": "Movie",
"releaseYear": 2009,
"rating": "R",
"durationMinutes": 97,
"durationSeasons": 0,
"dateAdded": "2020-01-01",
"categories": [
{
"category": "Horror Movies"
}
],
"cast": [
{
"name": "Aaron Yoo"
},
{
"name": "Amanda Righetti"
},
{
"name": "Arlen Escarpeta"
},
{
"name": "Ben Feldman"
},
{
"name": "Danielle Panabaker"
},
{
"name": "Derek Mears"
},
{
"name": "Jared Padalecki"
},
…
Read (Strings)
Related Data as Array of Strings
Ability to loop through
a
n
a
rr
a
y of strings
or other simple d
a
t
a
type.
• FOR JSON PATH
• STRING_AGG
• JSON_QUERY
{
"data": [
{
"showId": 70104894,
"title": "Friday the 13th",
"type": "Movie",
"releaseYear": 2009,
"rating": "R",
"durationMinutes": 97,
"durationSeasons": 0,
"dateAdded": "2020-01-01",
"categories": [
"Horror Movies"
],
"cast": [
"Aaron Yoo",
"Amanda Righetti",
"Arlen Escarpeta",
"Ben Feldman",
"Danielle Panabaker",
"Derek Mears",
"Jared Padalecki",
"Jonathan Sadowski",
"Julianna Guill",
"Ryan Hansen",
"Travis Van Winkle"
],
"directors": [
"Marcus Nispel"
]
},
…
Read (Custom)
Custom formatted and Related Data as Array of Object with Properties
• FOR JSON PATH
• STRING_AGG
• JSON_QUERY
• ROOT Node
• PATH L
a
bel Nodes
• D
a
t
a
Type Conversion
{
"data": [
{
"showId": 70104894,
"title": "Friday the 13th",
"type": "Movie",
"meta": {
"Release Year": 2009,
"rating": "R",
"duration": {
"minutes": 97,
"seasons": 0
},
"dateAdded": "01/01/2020"
},
"search": {
"categories": [
{
"categoryID": 1,
"Category": "Horror Movies",
"slug": "horror-movies"
}
]
},
"people": {
"cast": [
"Aaron Yoo",
"Amanda Righetti",
"Arlen Escarpeta",
"Ben Feldman",
"Danielle Panabaker",
"Derek Mears",
"Jared Padalecki",
"Jonathan Sadowski",
"Julianna Guill",
"Ryan Hansen",
"Travis Van Winkle"
],
"directors": [
"Marcus Nispel"
]
}
}
…
Read (Single Custom)
Single record custom formatted object
• FOR JSON PATH
• WITHOUT_ARRAY_WRAPPER
{
"showId": 70104894,
"title": "Friday the 13th",
"type": "Movie",
"meta": {
"Release Year": 2009,
"rating": "R",
"duration": {
"minutes": 97,
"seasons": 0
},
"dateAdded": "01/01/2020"
},
"search": {
"categories": [
{
"categoryID": 1,
"Category": "Horror Movies",
"slug": "horror-movies"
}
]
},
"people": {
"cast": [
"Aaron Yoo",
"Amanda Righetti",
"Arlen Escarpeta",
"Ben Feldman",
"Danielle Panabaker",
"Derek Mears",
"Jared Padalecki",
"Jonathan Sadowski",
"Julianna Guill",
"Ryan Hansen",
"Travis Van Winkle"
],
"directors": [
"Marcus Nispel"
]
}
}
Read (Flat File)
Read a JSON text
fi
le from SQL into a query
• OPENROWSET
• BULK Insert
Read (JSON Variable)
Pass a JSON string into a query
This will be used in the next ex
a
mples.
• OPENJSON
[ { show }, { show } ]
Writing to the database using JSON
• OPENJSON
• BULK Insert
[ { show }, { show } ]
• Create Record(s) Example
Insert one or more records from a JSON string into a table
• Update Record(s) Example
Update one or more records in the database from JSON string nodes
• Delete Record(s) Example
Delete one or more records using nodes in a JSON string
CRUD Code Demos
Putting It All Together
Lets use everything we learned and see what we can do!
• Get custom form
a
tted JSON for
a
ll records
• Insert into memory optimized SQL t
a
ble
• Return
a
ll rows from memory optimized t
a
ble
• Query c
a
che results
How long will the query t
a
ke to get the JSON b
a
ck?
Cache Code Demo
Things to look out for
Some things that may trip you up
• D
a
t
a
Type conversion f
a
ilures - i.e. Use
a
string for
a
number. Utilize CAST or CONVERT.
• An error occurred during the current comm
a
nd (Done st
a
tus 0). JSON text is not properly
form
a
tted. Unexpected ch
a
r
a
cter ‘P’
Find the column(s)
a
nd wr
a
p it with STRING_ESCAPE
• NULLS - Node will not show, if expected use ISNULL
• C
a
se Sensitivity - Write the w
a
y it should be returned, w
a
tch t
a
ble
a
li
a
s with
a
uto
• Keep rel
a
ted nodes together - D
a
t
a
will dis
a
ppe
a
r
• Document Size vs Ch
a
r
a
cter Limits - JSON_VALUE will trunc
a
te @ 4000 ch
a
r
a
cters, use
JSONQUERY
• B
a
d / Duplic
a
te L
a
bels - G
a
rb
a
ge in g
a
rb
a
ge out
Qb Example
Lets use qb to get similar results
• Cl
a
ssic QB
• JSON QB
• Using Custom Gr
a
mm
a
r File
• Custom Edits to h
a
ndle JSON function
a
lity
QB Code Demo
Q & A
Thank You
www.linkedin.com/in/studio7718/
GitHub.com/Studio7718/SQLJSONDEMO/

More Related Content

Similar to Use JSON to Slash Your Database Performance.pdf (20)

PDF
There is Javascript in my SQL
PGConf APAC
 
PPTX
SQL Server 2016 JSON
Davide Mauri
 
PPTX
Power JSON with PostgreSQL
EDB
 
PPTX
Sql Server 2016 and JSON
Greg McMurray
 
PPTX
MySQL Rises with JSON Support
Okcan Yasin Saygılı
 
PPTX
JSON and the Oracle Database
Maria Colgan
 
PDF
How to Use JSON in MySQL Wrong
Karwin Software Solutions LLC
 
PDF
Practical JSON in MySQL 5.7 and Beyond
Ike Walker
 
PPTX
BGOUG15: JSON support in MySQL 5.7
Georgi Kodinov
 
PDF
PostgreSQL 9.3 and JSON - talk at PgOpen 2013
Andrew Dunstan
 
PDF
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013
PostgresOpen
 
PPTX
What's new in DB2 for i - IBM i 7.3 TR1 and IBM i 7.2 TR5 - final.pptx
CesarDuarteMoreno
 
PPTX
A Tour of PostgREST
begriffs
 
PDF
NoSQL on ACID - Meet Unstructured Postgres
EDB
 
PPTX
NoSQL on ACID: Meet Unstructured Postgres
EDB
 
PPSX
Json in 18c and 19c
stewashton
 
PDF
Json in Postgres - the Roadmap
EDB
 
PDF
UKOUG Tech14 - Getting Started With JSON in the Database
Marco Gralike
 
PPTX
Data Con LA 2022 - MySQL, JSON & You: Perfect Together
Data Con LA
 
PPSX
JSON in 18c and 19c
stewashton
 
There is Javascript in my SQL
PGConf APAC
 
SQL Server 2016 JSON
Davide Mauri
 
Power JSON with PostgreSQL
EDB
 
Sql Server 2016 and JSON
Greg McMurray
 
MySQL Rises with JSON Support
Okcan Yasin Saygılı
 
JSON and the Oracle Database
Maria Colgan
 
How to Use JSON in MySQL Wrong
Karwin Software Solutions LLC
 
Practical JSON in MySQL 5.7 and Beyond
Ike Walker
 
BGOUG15: JSON support in MySQL 5.7
Georgi Kodinov
 
PostgreSQL 9.3 and JSON - talk at PgOpen 2013
Andrew Dunstan
 
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013
PostgresOpen
 
What's new in DB2 for i - IBM i 7.3 TR1 and IBM i 7.2 TR5 - final.pptx
CesarDuarteMoreno
 
A Tour of PostgREST
begriffs
 
NoSQL on ACID - Meet Unstructured Postgres
EDB
 
NoSQL on ACID: Meet Unstructured Postgres
EDB
 
Json in 18c and 19c
stewashton
 
Json in Postgres - the Roadmap
EDB
 
UKOUG Tech14 - Getting Started With JSON in the Database
Marco Gralike
 
Data Con LA 2022 - MySQL, JSON & You: Perfect Together
Data Con LA
 
JSON in 18c and 19c
stewashton
 

More from Ortus Solutions, Corp (20)

PDF
June Webinar: BoxLang-Dynamic-AWS-Lambda
Ortus Solutions, Corp
 
PDF
BoxLang-Dynamic-AWS-Lambda by Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
What's-New-with-BoxLang-Brad Wood.pptx.pdf
Ortus Solutions, Corp
 
PDF
Getting Started with BoxLang - CFCamp 2025.pdf
Ortus Solutions, Corp
 
PDF
CFCamp2025 - Keynote Day 1 led by Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
What's New with BoxLang Led by Brad Wood.pdf
Ortus Solutions, Corp
 
PDF
Vector Databases and the BoxLangCFML Developer.pdf
Ortus Solutions, Corp
 
PDF
Using cbSSO in a ColdBox App Led by Jacob Beers.pdf
Ortus Solutions, Corp
 
PDF
Portable CI wGitLab and Github led by Gavin Pickin.pdf
Ortus Solutions, Corp
 
PDF
Tame the Mesh An intro to cross-platform tracing and troubleshooting.pdf
Ortus Solutions, Corp
 
PDF
Supercharging CommandBox with Let's Encrypt.pdf
Ortus Solutions, Corp
 
PDF
Spice up your site with cool animations using GSAP..pdf
Ortus Solutions, Corp
 
PDF
Passkeys and cbSecurity Led by Eric Peterson.pdf
Ortus Solutions, Corp
 
PDF
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Ortus Solutions, Corp
 
PDF
Integrating the OpenAI API in Your Coldfusion Apps.pdf
Ortus Solutions, Corp
 
PDF
Hidden Gems in FusionReactor for BoxLang, ACF, and Lucee Users.pdf
Ortus Solutions, Corp
 
PDF
Geting-started with BoxLang Led By Raymon Camden.pdf
Ortus Solutions, Corp
 
PDF
From Zero to CRUD with ORM - Led by Annette Liskey.pdf
Ortus Solutions, Corp
 
PDF
Customize your Runtime Creating your first BoxLang Module.pdf
Ortus Solutions, Corp
 
PDF
CommandBox WebSockets - and SocketBox.pdf
Ortus Solutions, Corp
 
June Webinar: BoxLang-Dynamic-AWS-Lambda
Ortus Solutions, Corp
 
BoxLang-Dynamic-AWS-Lambda by Luis Majano.pdf
Ortus Solutions, Corp
 
What's-New-with-BoxLang-Brad Wood.pptx.pdf
Ortus Solutions, Corp
 
Getting Started with BoxLang - CFCamp 2025.pdf
Ortus Solutions, Corp
 
CFCamp2025 - Keynote Day 1 led by Luis Majano.pdf
Ortus Solutions, Corp
 
What's New with BoxLang Led by Brad Wood.pdf
Ortus Solutions, Corp
 
Vector Databases and the BoxLangCFML Developer.pdf
Ortus Solutions, Corp
 
Using cbSSO in a ColdBox App Led by Jacob Beers.pdf
Ortus Solutions, Corp
 
Portable CI wGitLab and Github led by Gavin Pickin.pdf
Ortus Solutions, Corp
 
Tame the Mesh An intro to cross-platform tracing and troubleshooting.pdf
Ortus Solutions, Corp
 
Supercharging CommandBox with Let's Encrypt.pdf
Ortus Solutions, Corp
 
Spice up your site with cool animations using GSAP..pdf
Ortus Solutions, Corp
 
Passkeys and cbSecurity Led by Eric Peterson.pdf
Ortus Solutions, Corp
 
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Ortus Solutions, Corp
 
Integrating the OpenAI API in Your Coldfusion Apps.pdf
Ortus Solutions, Corp
 
Hidden Gems in FusionReactor for BoxLang, ACF, and Lucee Users.pdf
Ortus Solutions, Corp
 
Geting-started with BoxLang Led By Raymon Camden.pdf
Ortus Solutions, Corp
 
From Zero to CRUD with ORM - Led by Annette Liskey.pdf
Ortus Solutions, Corp
 
Customize your Runtime Creating your first BoxLang Module.pdf
Ortus Solutions, Corp
 
CommandBox WebSockets - and SocketBox.pdf
Ortus Solutions, Corp
 
Ad

Recently uploaded (20)

PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Ad

Use JSON to Slash Your Database Performance.pdf

  • 2. WELCOME Into the Box 2025: The Future is Dynamic!
  • 3. Code Necromancer — Raising Legacy Code from the Dead Bill Reese • 25+ ye a rs of experience in full-st a ck a pplic a tion a nd web development • Using ColdFusion since version 4 a nd ColdBox since version 2 • B a sed ne a r Orl a ndo Florid a • M a rried with three teen a gers • Love Movies, Music a nd Photogr a phy • Excited to be cre a ting BoxL a ng content for CFC a sts • This is my f irst time spe a king @ ITB [email protected] Senior Developer - Ortus Solutions @Studio7718
  • 4. Bill Reese - Into The Box 2025 No hockey mask needed! Use JSON to Slash Your Database Performance
  • 5. Collection of ways to deliver data directly from the database using JSON • We will be using a simple demo d a t a b a se of movies a nd TV shows with rel a ted t a bles for a ctors, c a tegories a nd v a rious met a d a t a . • We will be getting movie a nd TV show inform a tion from the d a t a b a se using di ff erent methods a nd delivering th a t d a t a to a nd from REST API endpoints. • JSON D a t a Types = string, number, boole a n, a rr a y, object 1. Cl a ssic Method - Builds JSON document on a pplic a tion server using seri a lizing a nd deseri a lizing function a lity. vs 2. JSON Method - Builds JSON document on d a t a b a se server using built-in JSON function a lity. What will we be doing?
  • 6. Why would I want to use JSON in the Database? Think di ff erently about your data. Does this get updated frequently? • D a t a type conversion issues • Less d a t a b a se tr a ns a ctions, connections, queries • F a ster response time ( a lmost a ll of the time) • Less a pplic a tion server overhe a d • Quick sub-object cre a tion • Utilize In-Memory stor a ge of JSON documents • Utilize Query C a ching of JSON documents
  • 7. SQL Functionality Covered Some of the things we will be using in this demo Microsoft SQL Server Function a lity Covered • FOR JSON PATH • STRING_AGG • STUFF & XML PATH • JSON_QUERY • OPENJSON • OPENROWSET (Bulk) • ISJSON • STRING_ESCAPE JSON Document Form a tting Topics • Using l a bels to build node p a ths • Adding a root node(s) • D a t a type conversion • When to esc a pe in strings • Returning a n a rr a y or single object
  • 8. Data Type Conversion SQL use the following rules when converting your data to JSON SQL Server data type JSON data type char, nchar, varchar, nvarchar string int, bigint, fl oat, decimal, numeric number bit Boolean (true or false) date, datetime, datetime2, time, datetimeo ff set string varbinary, binary, image, timestamp/rowversion BASE64-encoded string geometry, geography, other CLR types Not supported. These types return an error. In the SELECT statement, use CAST or CONVERT, or use a CLR property or method, to convert the source data to a SQL Server data type that can be converted successfully to a JSON type. For example, use STAsText() for the geometry type, or use ToString() for any CLR type. The type of the JSON output value is then derived from the return type of the conversion that you apply in the SELECT statement. uniqueidenti fi er, money string
  • 9. Testing We will compare both versions and see how they stack up against each other. Wh a t a re we testing? • Request Response Time • D a t a b a se Response Time • D a t a b a se connections • Memory Us a ge Testing Output • 1000 Records
  • 10. Getting up and running Application Setup D a t a b a se Requirements Microsoft SQL Server Version 2016+ COMPATIBILITY_LEVEL >= 130 CLOB en a bled BULK permission required for Insert a nd upd a te method used Demo App Inst a ll (Comm a ndBox) Applic a tion Setup 1. Setup D a t a source 2. Cre a te Endpoints 3. Cre a te Models 4. Server start Applic a tion Server Options • BoxL a ng • Lucee • Adobe ColdFusion NOTE: MySQL a nd PostgreSQL h a ve simil a r function a lity
  • 11. • Query output should be a rr a y form a t. • Some documents m a y be too big a nd get split into sep a r a te rows in the recordset. All rows of the a rr a y need to be combined to form the single JSON String. • JsonSqlRestHandler will h a ndle the SQL output a nd prep a re it for returning. • returnStringJSON = Enc a psul a tes d a t a m a rsh a ling • decodeSQLJSON = Combines recordset output into single string • jsonMeta = Appends st a nd a rd REST nodes if desired * This function a lity c a n be incorpor a ted using DI or through inherit a nce Di ff erent Output Methods Handling the JSON string from the database and how it is returned
  • 14. JsonSqlRestHandler - jsonMeta Only Needed if trying to mimic CB st a nd a rd REST returns.
  • 16. Read (Single Table) Single Table • FOR JSON PATH { "data": [ { "showId": 70104894, "title": "Friday the 13th", "type": "Movie", "releaseYear": 2009, "rating": "R", "durationMinutes": 97, "durationSeasons": 0, "dateAdded": "2020-01-01" }, { "showId": 70140450, "title": "The IT Crowd", "type": "TV Show", "releaseYear": 2013, "rating": "TV-MA", "durationMinutes": 0, "durationSeasons": 5, "dateAdded": "2017-08-01" } ], "error": false, "pagination": { "totalPages": 1, "maxRows": 0, "offset": 0, "page": 1, "totalRecords": 0 }, "messages": [] }
  • 17. Read (Lists) Related Data as Displayable List Output directly to UI a s a list of v a lues with no need to inter a ct with or loop through the d a t a . • FOR JSON PATH • STRING_AGG • XML PATH { "data": [ { "showId": 70104894, "title": "Friday the 13th", "type": "Movie", "releaseYear": 2009, "rating": "R", "durationMinutes": 97, "durationSeasons": 0, "dateAdded": "2020-01-01", "categories": "Horror Movies", "cast": "Aaron Yoo, Jonathan Sadowski, Derek Mears, Arlen Escarpeta, Danielle Panabaker, Amanda Righetti, Ryan Hansen, Travis Van Winkle, Julianna Guill, Ben Feldman, Jared Padalecki", "directors": "Marcus Nispel" }, { "showId": 70140450, "title": "The IT Crowd", "type": "TV Show", "releaseYear": 2013, "rating": "TV-MA", "durationMinutes": 0, "durationSeasons": 5, "dateAdded": "2017-08-01", "categories": "British TV Shows, Classic & Cult TV, International TV Shows", "cast": "Richard Ayoade, Katherine Parkinson, Matt Berry, Chris O'Dowd" } ], "error": false, "pagination": { "totalPages": 1, "maxRows": 0, "offset": 0, "page": 1, "totalRecords": 0 }, "messages": [] }
  • 18. Read (Objects) Related Data as Array of Object with Properties Ability to loop though a n a rr a y of objects with properties.FOR JSON PATH • FOR JSON PATH on Sub-queries { "data": [ { "showId": 70104894, "title": "Friday the 13th", "type": "Movie", "releaseYear": 2009, "rating": "R", "durationMinutes": 97, "durationSeasons": 0, "dateAdded": "2020-01-01", "categories": [ { "category": "Horror Movies" } ], "cast": [ { "name": "Aaron Yoo" }, { "name": "Amanda Righetti" }, { "name": "Arlen Escarpeta" }, { "name": "Ben Feldman" }, { "name": "Danielle Panabaker" }, { "name": "Derek Mears" }, { "name": "Jared Padalecki" }, …
  • 19. Read (Strings) Related Data as Array of Strings Ability to loop through a n a rr a y of strings or other simple d a t a type. • FOR JSON PATH • STRING_AGG • JSON_QUERY { "data": [ { "showId": 70104894, "title": "Friday the 13th", "type": "Movie", "releaseYear": 2009, "rating": "R", "durationMinutes": 97, "durationSeasons": 0, "dateAdded": "2020-01-01", "categories": [ "Horror Movies" ], "cast": [ "Aaron Yoo", "Amanda Righetti", "Arlen Escarpeta", "Ben Feldman", "Danielle Panabaker", "Derek Mears", "Jared Padalecki", "Jonathan Sadowski", "Julianna Guill", "Ryan Hansen", "Travis Van Winkle" ], "directors": [ "Marcus Nispel" ] }, …
  • 20. Read (Custom) Custom formatted and Related Data as Array of Object with Properties • FOR JSON PATH • STRING_AGG • JSON_QUERY • ROOT Node • PATH L a bel Nodes • D a t a Type Conversion { "data": [ { "showId": 70104894, "title": "Friday the 13th", "type": "Movie", "meta": { "Release Year": 2009, "rating": "R", "duration": { "minutes": 97, "seasons": 0 }, "dateAdded": "01/01/2020" }, "search": { "categories": [ { "categoryID": 1, "Category": "Horror Movies", "slug": "horror-movies" } ] }, "people": { "cast": [ "Aaron Yoo", "Amanda Righetti", "Arlen Escarpeta", "Ben Feldman", "Danielle Panabaker", "Derek Mears", "Jared Padalecki", "Jonathan Sadowski", "Julianna Guill", "Ryan Hansen", "Travis Van Winkle" ], "directors": [ "Marcus Nispel" ] } } …
  • 21. Read (Single Custom) Single record custom formatted object • FOR JSON PATH • WITHOUT_ARRAY_WRAPPER { "showId": 70104894, "title": "Friday the 13th", "type": "Movie", "meta": { "Release Year": 2009, "rating": "R", "duration": { "minutes": 97, "seasons": 0 }, "dateAdded": "01/01/2020" }, "search": { "categories": [ { "categoryID": 1, "Category": "Horror Movies", "slug": "horror-movies" } ] }, "people": { "cast": [ "Aaron Yoo", "Amanda Righetti", "Arlen Escarpeta", "Ben Feldman", "Danielle Panabaker", "Derek Mears", "Jared Padalecki", "Jonathan Sadowski", "Julianna Guill", "Ryan Hansen", "Travis Van Winkle" ], "directors": [ "Marcus Nispel" ] } }
  • 22. Read (Flat File) Read a JSON text fi le from SQL into a query • OPENROWSET • BULK Insert
  • 23. Read (JSON Variable) Pass a JSON string into a query This will be used in the next ex a mples. • OPENJSON [ { show }, { show } ]
  • 24. Writing to the database using JSON • OPENJSON • BULK Insert [ { show }, { show } ] • Create Record(s) Example Insert one or more records from a JSON string into a table • Update Record(s) Example Update one or more records in the database from JSON string nodes • Delete Record(s) Example Delete one or more records using nodes in a JSON string
  • 26. Putting It All Together Lets use everything we learned and see what we can do! • Get custom form a tted JSON for a ll records • Insert into memory optimized SQL t a ble • Return a ll rows from memory optimized t a ble • Query c a che results How long will the query t a ke to get the JSON b a ck?
  • 28. Things to look out for Some things that may trip you up • D a t a Type conversion f a ilures - i.e. Use a string for a number. Utilize CAST or CONVERT. • An error occurred during the current comm a nd (Done st a tus 0). JSON text is not properly form a tted. Unexpected ch a r a cter ‘P’ Find the column(s) a nd wr a p it with STRING_ESCAPE • NULLS - Node will not show, if expected use ISNULL • C a se Sensitivity - Write the w a y it should be returned, w a tch t a ble a li a s with a uto • Keep rel a ted nodes together - D a t a will dis a ppe a r • Document Size vs Ch a r a cter Limits - JSON_VALUE will trunc a te @ 4000 ch a r a cters, use JSONQUERY • B a d / Duplic a te L a bels - G a rb a ge in g a rb a ge out
  • 29. Qb Example Lets use qb to get similar results • Cl a ssic QB • JSON QB • Using Custom Gr a mm a r File • Custom Edits to h a ndle JSON function a lity
  • 31. Q & A