SlideShare a Scribd company logo
1 of 32
NoSQL as Not Only SQL
Backend Berlin
Dezember 2015
Stefanie Janine Stölting
@sjstoelting
JSON Datatypes
JSON
Available since 9.2
BSON
Available as extension on GitHub since 2013
JSONB
Available since 9.4
Crompessed JSON
ACID
Atomicity, Consistency, Isolation, Durability is
a set of properties that guarantee that
database transactions are processed reliably. 1
1 See https://en.wikipedia.org/wiki/ACID
Performance
Test done by
EnterpriseDB,
see the articleby
Marc Linster
JSON Functions
row_to_json({row})
Returns the row as JSON
array_to_json({array})
Returns the array as JSON
JSON Opertators
Array element
->{int}
Array element by name
->{text}
Object element
->> {text}
Value at path
#> {text}
Index on JSON
Index JSONB content for faster access
GIN index overall
CREATE INDEX idx_1 ON jsonb.actor USING
GIN (jsondata);
Even unique B-Tree indexes are possible
CREATE UNIQUE INDEX actor_id_2 ON
jsonb.actor((CAST(jsondata→'actor_id'::int)));
New JSON functions
With the next version, 9.5, there will be new functions
available, for example:
jsonb_pretty
jsonb_set
If you can't wait using them, you might be interested
in the jsonbx extentsion available at PGXN
Data sources
The Chinook database is available
at chinookdatabase.codeplex.com
Amazon book reviews of 1998 are
available at
examples.citusdata.com/customer_review
CTE
Common Table Expressions will be used in examples
Example:
WITH RECURSIVE t(n) AS (
VALUES (1)
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT sum(n), min(n), max(n) FROM t;
Result:
Live Examples
Let's see, how it does work.
Live with Chinook data
-- Step 1: Albums with tracks as JSON
WITH albums AS
(
SELECT a."ArtistId" AS artist_id
, a."Title" AS album_title
, array_agg(t."Name") AS album_tracks
FROM "Album" AS a
INNER JOIN "Track" AS t
ON a."AlbumId" = t."AlbumId"
GROUP BY a."ArtistId"
, a."Title"
)
SELECT row_to_json(albums) AS album_tracks
FROM albums
;
Live with Chinook data
-- Step 2 Abums including tracks with aritsts
WITH albums AS
(
SELECT a."ArtistId" AS artist_id
, a."Title" AS album_title
, array_agg(t."Name") AS album_tracks
FROM "Album" AS a
INNER JOIN "Track" AS t
ON a."AlbumId" = t."AlbumId"
GROUP BY a."ArtistId"
, a."Title"
)
, js_albums AS
(
SELECT row_to_json(albums) AS album_tracks
FROM albums
)
SELECT a."Name" AS artist
, al.album_tracks AS albums_tracks
FROM sqlite_artist AS a
INNER JOIN js_albums AS al
ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT)
;
Live with Chinook data
-- Step 3 Return one row for an artist with all albums as VIEW
CREATE OR REPLACE VIEW v_artist_data AS
WITH albums AS
(
SELECT a."ArtistId" AS artist_id
, a."Title" AS album_title
, array_agg(t."Name") AS album_tracks
FROM "Album" AS a
INNER JOIN "Track" AS t
ON a."AlbumId" = t."AlbumId"
GROUP BY a."ArtistId"
, a."Title"
)
, js_albums AS
(
SELECT row_to_json(albums) AS album_tracks
FROM albums
)
, artist_albums AS
(
SELECT a."Name" AS artist
, array_agg(al.album_tracks) AS albums_tracks
FROM "Artist" AS a
INNER JOIN js_albums AS al
ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT)
GROUP BY a."Name"
)
SELECT CAST(row_to_json(artist_albums) AS JSONB) AS artist_data
FROM artist_albums
;
Live with Chinook data
-- Select data from the view
SELECT *
FROM v_artist_data
;
Live with Chinook data
-- SELECT data from that VIEW, that does querying
SELECT jsonb_pretty(artist_data) pretty_artistdata
FROM v_artist_data
WHERE artist_data->>'artist' IN ('Miles Davis', 'AC/DC')
;
Live with Chinook data
-- SELECT some data that VIEW using JSON methods
SELECT jsonb_pretty(artist_data#>'{albums_tracks}') AS all_albums
, jsonb_pretty(artist_data#>'{albums_tracks, 0}') AS tracks_0
, artist_data#>'{albums_tracks, 0, album_title}' AS title
, artist_data#>'{albums_tracks, 0, artist_id}' AS artist_id
, artist_data->>'artist' AS artist
FROM v_artist_data
WHERE artist_data->'albums_tracks' @> '[{"album_title":"Miles Ahead"}]'
;
Live with Chinook data
-- Array to records
SELECT jsonb_array_elements(artist_data#>'{albums_tracks}')->>'artist_id' AS artist_id
, artist_data->>'artist' AS artist
, jsonb_array_elements(artist_data#>'{albums_tracks}')->>'album_title' AS ablum_title
, jsonb_array_elements(jsonb_array_elements(artist_data#>'{albums_tracks}')#>'{album_tracks}') AS song_titles
FROM v_artist_data
WHERE artist_data->'albums_tracks' @> '[{"artist_id":139}]'
ORDER BY 3, 4;
Live with Chinook data
-- Create a function, which will be used for UPDATE on the view v_artrist_data
CREATE OR REPLACE FUNCTION trigger_v_artist_data_update()
RETURNS trigger AS
$BODY$
-- Data variables
DECLARE rec RECORD;
-- Error variables
DECLARE v_state TEXT;
DECLARE v_msg TEXT;
DECLARE v_detail TEXT;
DECLARE v_hint TEXT;
DECLARE v_context TEXT;
BEGIN
-- Update table Artist
IF OLD.artist_data->>'artist' <> NEW.artist_data->>'artist' THEN
UPDATE "Artist"
SET "Name" = NEW.artist_data->>'artist'
WHERE "ArtistId" = artist_data#>'{albums_tracks, 0, artist_id}';
END IF;
-- Update table Album in a foreach
-- Update table Track in a foreach
RETURN NEW;
EXCEPTION WHEN unique_violation THEN
RAISE NOTICE 'Sorry, but the something went wrong while trying to update artist data';
RETURN OLD;
WHEN others THEN
GET STACKED DIAGNOSTICS
v_state = RETURNED_SQLSTATE,
v_msg = MESSAGE_TEXT,
v_detail = PG_EXCEPTION_DETAIL,
v_hint = PG_EXCEPTION_HINT,
v_context = PG_EXCEPTION_CONTEXT;
RAISE NOTICE '%', v_msg;
RETURN OLD;
END;
$BODY$
LANGUAGE plpgsql;
Live with Chinook data
-- The trigger will be fired instead of an UPDATE statemen to save data
CREATE TRIGGER v_artist_data_instead_update INSTEAD OF UPDATE
ON v_artist_data
FOR EACH ROW
EXECUTE PROCEDURE trigger_v_artist_data_update()
;
Live Amazon reviews
-- Create a table for JSON data with 1998 Amazon reviews
CREATE TABLE reviews(review_jsonb jsonb);
Live Amazon reviews
-- Import customer reviews from a file
COPY reviews FROM '/var/tmp/customer_reviews_nested_1998.json'
-- Maintenance the filled table
VACUUM ANALYZE reviews;
Live Amazon reviews
-- There should be 589.859 records imported into the table
SELECT count(*) FROM reviews;
Live Amazon reviews
-- Select data with JSON
SELECT
review_jsonb#>> '{product,title}' AS title
, avg((review_jsonb#>> '{review,rating}')::int) AS average_rating
FROM reviews
WHERE review_jsonb@>'{"product": {"category": "Sheet Music & Scores"}}'
GROUP BY 1
ORDER BY 2 DESC
;
Without an Index: 248ms
Live Amazon reviews
-- Create a GIN index
CREATE INDEX review_review_jsonb ON reviews USING GIN (review_jsonb);
Live Amazon reviews
-- Select data with JSON
SELECT
review_jsonb#>> '{product,title}' AS title
, avg((review_jsonb#>> '{review,rating}')::int) AS average_rating
FROM reviews
WHERE review_jsonb@>'{"product": {"category": "Sheet Music & Scores"}}'
GROUP BY 1
ORDER BY 2 DESC
;
The same query as before with the previously created GIN Index: 7ms
Live Amazon reviews
-- SELECT some statistics from the JSON data
SELECT review_jsonb#>>'{product,category}' AS category
, avg((review_jsonb#>>'{review,rating}')::int) AS average_rating
, count((review_jsonb#>>'{review,rating}')::int) AS count_rating
FROM reviews
GROUP BY 1
;
Without an Index: 9747ms
Live Amazon reviews
-- Create a B-Tree index on a JSON expression
CREATE INDEX reviews_product_category ON reviews ((review_jsonb#>>'{product,category}'));
Live Amazon reviews
-- SELECT some statistics from the JSON data
SELECT review_jsonb#>>'{product,category}' AS category
, avg((review_jsonb#>>'{review,rating}')::int) AS average_rating
, count((review_jsonb#>>'{review,rating}')::int) AS count_rating
FROM reviews
GROUP BY 1
;
The same query as before with the previously created BTREE Index: 1605ms
User Group / Conferences
PostgreSQL User Group Berlin
PGDay FOSDEM
Brussels, January 29 2016
FOSDEM Dev Room
Brussels, January 31 2016
Link List
Extended JSONB functions for 9.5
Extended JSONB functions as extension for 9.4
from PGXN (jsonbx)
Slide and source on Github:
https://github.com/sjstoelting/talks
JSON by the other
Elephant
This document by Stefanie Janine Stölting is covered by the
Creative Commons Attribution 4.0 International

More Related Content

What's hot

Data Munging in R - Chicago R User Group
Data Munging in R - Chicago R User GroupData Munging in R - Chicago R User Group
Data Munging in R - Chicago R User Groupdesignandanalytics
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Hiroshi Shibamura
 
R- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webminingR- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webminingGabriela Plantie
 
Postgre(No)SQL - A JSON journey
Postgre(No)SQL - A JSON journeyPostgre(No)SQL - A JSON journey
Postgre(No)SQL - A JSON journeyNicola Moretto
 
supporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tablesupporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tableMahabubur Rahaman
 
Visualizing Postgres
Visualizing PostgresVisualizing Postgres
Visualizing Postgreselliando dias
 
Collectors in the Wild
Collectors in the WildCollectors in the Wild
Collectors in the WildJosé Paumard
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011Mandi Walls
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseMarco Gralike
 
Golang slidesaudrey
Golang slidesaudreyGolang slidesaudrey
Golang slidesaudreyAudrey Lim
 

What's hot (11)

Data Munging in R - Chicago R User Group
Data Munging in R - Chicago R User GroupData Munging in R - Chicago R User Group
Data Munging in R - Chicago R User Group
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
 
R- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webminingR- create a table from a list of files.... before webmining
R- create a table from a list of files.... before webmining
 
Postgre(No)SQL - A JSON journey
Postgre(No)SQL - A JSON journeyPostgre(No)SQL - A JSON journey
Postgre(No)SQL - A JSON journey
 
supporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tablesupporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered table
 
Visualizing Postgres
Visualizing PostgresVisualizing Postgres
Visualizing Postgres
 
Collectors in the Wild
Collectors in the WildCollectors in the Wild
Collectors in the Wild
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory Database
 
Golang slidesaudrey
Golang slidesaudreyGolang slidesaudrey
Golang slidesaudrey
 

Viewers also liked

Retail store system(bsit16)prototyping
Retail store system(bsit16)prototypingRetail store system(bsit16)prototyping
Retail store system(bsit16)prototypingrameses francia
 
BBR F-22 EDITED
BBR F-22 EDITEDBBR F-22 EDITED
BBR F-22 EDITEDBrian Bott
 
제9장교육평가과제전망 김주찬
제9장교육평가과제전망 김주찬제9장교육평가과제전망 김주찬
제9장교육평가과제전망 김주찬Ju Chan Kim
 
Prateek chauhan cv senior business analyst_csc
Prateek chauhan cv senior business analyst_cscPrateek chauhan cv senior business analyst_csc
Prateek chauhan cv senior business analyst_cscPrateek Chauhan
 
aprendizaje y pensamiento critico
aprendizaje y pensamiento criticoaprendizaje y pensamiento critico
aprendizaje y pensamiento criticoDiana Nieto
 
Phân biệt giảm cân an toàn và giảm cân không an toàn
Phân biệt giảm cân an toàn và giảm cân không an toànPhân biệt giảm cân an toàn và giảm cân không an toàn
Phân biệt giảm cân an toàn và giảm cân không an toàndale590
 
Seminario 2 de investigación penal
Seminario 2 de investigación penalSeminario 2 de investigación penal
Seminario 2 de investigación penalmvillafranca
 

Viewers also liked (15)

Retail store system(bsit16)prototyping
Retail store system(bsit16)prototypingRetail store system(bsit16)prototyping
Retail store system(bsit16)prototyping
 
Cloud Foundry Foundation Overview
Cloud Foundry Foundation OverviewCloud Foundry Foundation Overview
Cloud Foundry Foundation Overview
 
MU
MUMU
MU
 
BBR F-22 EDITED
BBR F-22 EDITEDBBR F-22 EDITED
BBR F-22 EDITED
 
Cloud
CloudCloud
Cloud
 
Savignano sul Rubicone: tutte le misure 2016 per il centro storico
Savignano sul Rubicone: tutte le misure 2016 per il centro storicoSavignano sul Rubicone: tutte le misure 2016 per il centro storico
Savignano sul Rubicone: tutte le misure 2016 per il centro storico
 
Study on the effect of pollution on some vegetable crops
Study on the effect of pollution on some vegetable  cropsStudy on the effect of pollution on some vegetable  crops
Study on the effect of pollution on some vegetable crops
 
Gestational diabetes
Gestational diabetesGestational diabetes
Gestational diabetes
 
제9장교육평가과제전망 김주찬
제9장교육평가과제전망 김주찬제9장교육평가과제전망 김주찬
제9장교육평가과제전망 김주찬
 
Prateek chauhan cv senior business analyst_csc
Prateek chauhan cv senior business analyst_cscPrateek chauhan cv senior business analyst_csc
Prateek chauhan cv senior business analyst_csc
 
Albertini egm master degree
Albertini egm master degreeAlbertini egm master degree
Albertini egm master degree
 
aprendizaje y pensamiento critico
aprendizaje y pensamiento criticoaprendizaje y pensamiento critico
aprendizaje y pensamiento critico
 
Manjunath_V_CV
Manjunath_V_CVManjunath_V_CV
Manjunath_V_CV
 
Phân biệt giảm cân an toàn và giảm cân không an toàn
Phân biệt giảm cân an toàn và giảm cân không an toànPhân biệt giảm cân an toàn và giảm cân không an toàn
Phân biệt giảm cân an toàn và giảm cân không an toàn
 
Seminario 2 de investigación penal
Seminario 2 de investigación penalSeminario 2 de investigación penal
Seminario 2 de investigación penal
 

Similar to NoSQL as Not Only SQL

NoSQL для PostgreSQL: Jsquery — язык запросов
NoSQL для PostgreSQL: Jsquery — язык запросовNoSQL для PostgreSQL: Jsquery — язык запросов
NoSQL для PostgreSQL: Jsquery — язык запросовCodeFest
 
Going Native: Leveraging the New JSON Native Datatype in Oracle 21c
Going Native: Leveraging the New JSON Native Datatype in Oracle 21cGoing Native: Leveraging the New JSON Native Datatype in Oracle 21c
Going Native: Leveraging the New JSON Native Datatype in Oracle 21cJim Czuprynski
 
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Andrii Lashchenko
 
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...Ontico
 
Functional Programming in Java 8
Functional Programming in Java 8Functional Programming in Java 8
Functional Programming in Java 8宇 傅
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackGaryCoady
 
Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Gilbok Lee
 
1.1 Intro to WinDDI.pdf
1.1 Intro to WinDDI.pdf1.1 Intro to WinDDI.pdf
1.1 Intro to WinDDI.pdfssuser8b6c85
 
Conquering JSONB in PostgreSQL
Conquering JSONB in PostgreSQLConquering JSONB in PostgreSQL
Conquering JSONB in PostgreSQLInes Panker
 
JSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger pictureJSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger pictureSergey Petrunya
 
Syntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQLSyntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQLAntony Abramchenko
 
BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...
BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...
BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...Amazon Web Services
 
Syntactic sugar in postgre sql
Syntactic sugar in postgre sqlSyntactic sugar in postgre sql
Syntactic sugar in postgre sqlAntony Abramchenko
 
JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)Faysal Shaarani (MBA)
 

Similar to NoSQL as Not Only SQL (20)

One Database To Rule 'em All
One Database To Rule 'em AllOne Database To Rule 'em All
One Database To Rule 'em All
 
One Database to Rule 'em all (FrOSCon 11)
One Database to Rule 'em all (FrOSCon 11)One Database to Rule 'em all (FrOSCon 11)
One Database to Rule 'em all (FrOSCon 11)
 
NoSQL для PostgreSQL: Jsquery — язык запросов
NoSQL для PostgreSQL: Jsquery — язык запросовNoSQL для PostgreSQL: Jsquery — язык запросов
NoSQL для PostgreSQL: Jsquery — язык запросов
 
Going Native: Leveraging the New JSON Native Datatype in Oracle 21c
Going Native: Leveraging the New JSON Native Datatype in Oracle 21cGoing Native: Leveraging the New JSON Native Datatype in Oracle 21c
Going Native: Leveraging the New JSON Native Datatype in Oracle 21c
 
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.Spray Json and MongoDB Queries: Insights and Simple Tricks.
Spray Json and MongoDB Queries: Insights and Simple Tricks.
 
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
 
Functional Programming in Java 8
Functional Programming in Java 8Functional Programming in Java 8
Functional Programming in Java 8
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
 
Http4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web StackHttp4s, Doobie and Circe: The Functional Web Stack
Http4s, Doobie and Circe: The Functional Web Stack
 
Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)
 
Neo4j (Part 1)
Neo4j (Part 1)Neo4j (Part 1)
Neo4j (Part 1)
 
Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !Oh, that ubiquitous JSON !
Oh, that ubiquitous JSON !
 
1.1 Intro to WinDDI.pdf
1.1 Intro to WinDDI.pdf1.1 Intro to WinDDI.pdf
1.1 Intro to WinDDI.pdf
 
Conquering JSONB in PostgreSQL
Conquering JSONB in PostgreSQLConquering JSONB in PostgreSQL
Conquering JSONB in PostgreSQL
 
JSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger pictureJSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger picture
 
Syntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQLSyntactic sugar in Postgre SQL
Syntactic sugar in Postgre SQL
 
BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...
BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...
BDA305 NEW LAUNCH! Intro to Amazon Redshift Spectrum: Now query exabytes of d...
 
Syntactic sugar in postgre sql
Syntactic sugar in postgre sqlSyntactic sugar in postgre sql
Syntactic sugar in postgre sql
 
JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)JSON Data Parsing in Snowflake (By Faysal Shaarani)
JSON Data Parsing in Snowflake (By Faysal Shaarani)
 
Java 1-contd
Java 1-contdJava 1-contd
Java 1-contd
 

Recently uploaded

Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 

Recently uploaded (20)

Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 

NoSQL as Not Only SQL

  • 1. NoSQL as Not Only SQL Backend Berlin Dezember 2015 Stefanie Janine Stölting @sjstoelting
  • 2. JSON Datatypes JSON Available since 9.2 BSON Available as extension on GitHub since 2013 JSONB Available since 9.4 Crompessed JSON
  • 3. ACID Atomicity, Consistency, Isolation, Durability is a set of properties that guarantee that database transactions are processed reliably. 1 1 See https://en.wikipedia.org/wiki/ACID
  • 4. Performance Test done by EnterpriseDB, see the articleby Marc Linster
  • 5. JSON Functions row_to_json({row}) Returns the row as JSON array_to_json({array}) Returns the array as JSON
  • 6. JSON Opertators Array element ->{int} Array element by name ->{text} Object element ->> {text} Value at path #> {text}
  • 7. Index on JSON Index JSONB content for faster access GIN index overall CREATE INDEX idx_1 ON jsonb.actor USING GIN (jsondata); Even unique B-Tree indexes are possible CREATE UNIQUE INDEX actor_id_2 ON jsonb.actor((CAST(jsondata→'actor_id'::int)));
  • 8. New JSON functions With the next version, 9.5, there will be new functions available, for example: jsonb_pretty jsonb_set If you can't wait using them, you might be interested in the jsonbx extentsion available at PGXN
  • 9. Data sources The Chinook database is available at chinookdatabase.codeplex.com Amazon book reviews of 1998 are available at examples.citusdata.com/customer_review
  • 10. CTE Common Table Expressions will be used in examples Example: WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100 ) SELECT sum(n), min(n), max(n) FROM t; Result:
  • 11. Live Examples Let's see, how it does work.
  • 12. Live with Chinook data -- Step 1: Albums with tracks as JSON WITH albums AS ( SELECT a."ArtistId" AS artist_id , a."Title" AS album_title , array_agg(t."Name") AS album_tracks FROM "Album" AS a INNER JOIN "Track" AS t ON a."AlbumId" = t."AlbumId" GROUP BY a."ArtistId" , a."Title" ) SELECT row_to_json(albums) AS album_tracks FROM albums ;
  • 13. Live with Chinook data -- Step 2 Abums including tracks with aritsts WITH albums AS ( SELECT a."ArtistId" AS artist_id , a."Title" AS album_title , array_agg(t."Name") AS album_tracks FROM "Album" AS a INNER JOIN "Track" AS t ON a."AlbumId" = t."AlbumId" GROUP BY a."ArtistId" , a."Title" ) , js_albums AS ( SELECT row_to_json(albums) AS album_tracks FROM albums ) SELECT a."Name" AS artist , al.album_tracks AS albums_tracks FROM sqlite_artist AS a INNER JOIN js_albums AS al ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT) ;
  • 14. Live with Chinook data -- Step 3 Return one row for an artist with all albums as VIEW CREATE OR REPLACE VIEW v_artist_data AS WITH albums AS ( SELECT a."ArtistId" AS artist_id , a."Title" AS album_title , array_agg(t."Name") AS album_tracks FROM "Album" AS a INNER JOIN "Track" AS t ON a."AlbumId" = t."AlbumId" GROUP BY a."ArtistId" , a."Title" ) , js_albums AS ( SELECT row_to_json(albums) AS album_tracks FROM albums ) , artist_albums AS ( SELECT a."Name" AS artist , array_agg(al.album_tracks) AS albums_tracks FROM "Artist" AS a INNER JOIN js_albums AS al ON a."ArtistId" = CAST(al.album_tracks->>'artist_id' AS INT) GROUP BY a."Name" ) SELECT CAST(row_to_json(artist_albums) AS JSONB) AS artist_data FROM artist_albums ;
  • 15. Live with Chinook data -- Select data from the view SELECT * FROM v_artist_data ;
  • 16. Live with Chinook data -- SELECT data from that VIEW, that does querying SELECT jsonb_pretty(artist_data) pretty_artistdata FROM v_artist_data WHERE artist_data->>'artist' IN ('Miles Davis', 'AC/DC') ;
  • 17. Live with Chinook data -- SELECT some data that VIEW using JSON methods SELECT jsonb_pretty(artist_data#>'{albums_tracks}') AS all_albums , jsonb_pretty(artist_data#>'{albums_tracks, 0}') AS tracks_0 , artist_data#>'{albums_tracks, 0, album_title}' AS title , artist_data#>'{albums_tracks, 0, artist_id}' AS artist_id , artist_data->>'artist' AS artist FROM v_artist_data WHERE artist_data->'albums_tracks' @> '[{"album_title":"Miles Ahead"}]' ;
  • 18. Live with Chinook data -- Array to records SELECT jsonb_array_elements(artist_data#>'{albums_tracks}')->>'artist_id' AS artist_id , artist_data->>'artist' AS artist , jsonb_array_elements(artist_data#>'{albums_tracks}')->>'album_title' AS ablum_title , jsonb_array_elements(jsonb_array_elements(artist_data#>'{albums_tracks}')#>'{album_tracks}') AS song_titles FROM v_artist_data WHERE artist_data->'albums_tracks' @> '[{"artist_id":139}]' ORDER BY 3, 4;
  • 19. Live with Chinook data -- Create a function, which will be used for UPDATE on the view v_artrist_data CREATE OR REPLACE FUNCTION trigger_v_artist_data_update() RETURNS trigger AS $BODY$ -- Data variables DECLARE rec RECORD; -- Error variables DECLARE v_state TEXT; DECLARE v_msg TEXT; DECLARE v_detail TEXT; DECLARE v_hint TEXT; DECLARE v_context TEXT; BEGIN -- Update table Artist IF OLD.artist_data->>'artist' <> NEW.artist_data->>'artist' THEN UPDATE "Artist" SET "Name" = NEW.artist_data->>'artist' WHERE "ArtistId" = artist_data#>'{albums_tracks, 0, artist_id}'; END IF; -- Update table Album in a foreach -- Update table Track in a foreach RETURN NEW; EXCEPTION WHEN unique_violation THEN RAISE NOTICE 'Sorry, but the something went wrong while trying to update artist data'; RETURN OLD; WHEN others THEN GET STACKED DIAGNOSTICS v_state = RETURNED_SQLSTATE, v_msg = MESSAGE_TEXT, v_detail = PG_EXCEPTION_DETAIL, v_hint = PG_EXCEPTION_HINT, v_context = PG_EXCEPTION_CONTEXT; RAISE NOTICE '%', v_msg; RETURN OLD; END; $BODY$ LANGUAGE plpgsql;
  • 20. Live with Chinook data -- The trigger will be fired instead of an UPDATE statemen to save data CREATE TRIGGER v_artist_data_instead_update INSTEAD OF UPDATE ON v_artist_data FOR EACH ROW EXECUTE PROCEDURE trigger_v_artist_data_update() ;
  • 21. Live Amazon reviews -- Create a table for JSON data with 1998 Amazon reviews CREATE TABLE reviews(review_jsonb jsonb);
  • 22. Live Amazon reviews -- Import customer reviews from a file COPY reviews FROM '/var/tmp/customer_reviews_nested_1998.json' -- Maintenance the filled table VACUUM ANALYZE reviews;
  • 23. Live Amazon reviews -- There should be 589.859 records imported into the table SELECT count(*) FROM reviews;
  • 24. Live Amazon reviews -- Select data with JSON SELECT review_jsonb#>> '{product,title}' AS title , avg((review_jsonb#>> '{review,rating}')::int) AS average_rating FROM reviews WHERE review_jsonb@>'{"product": {"category": "Sheet Music & Scores"}}' GROUP BY 1 ORDER BY 2 DESC ; Without an Index: 248ms
  • 25. Live Amazon reviews -- Create a GIN index CREATE INDEX review_review_jsonb ON reviews USING GIN (review_jsonb);
  • 26. Live Amazon reviews -- Select data with JSON SELECT review_jsonb#>> '{product,title}' AS title , avg((review_jsonb#>> '{review,rating}')::int) AS average_rating FROM reviews WHERE review_jsonb@>'{"product": {"category": "Sheet Music & Scores"}}' GROUP BY 1 ORDER BY 2 DESC ; The same query as before with the previously created GIN Index: 7ms
  • 27. Live Amazon reviews -- SELECT some statistics from the JSON data SELECT review_jsonb#>>'{product,category}' AS category , avg((review_jsonb#>>'{review,rating}')::int) AS average_rating , count((review_jsonb#>>'{review,rating}')::int) AS count_rating FROM reviews GROUP BY 1 ; Without an Index: 9747ms
  • 28. Live Amazon reviews -- Create a B-Tree index on a JSON expression CREATE INDEX reviews_product_category ON reviews ((review_jsonb#>>'{product,category}'));
  • 29. Live Amazon reviews -- SELECT some statistics from the JSON data SELECT review_jsonb#>>'{product,category}' AS category , avg((review_jsonb#>>'{review,rating}')::int) AS average_rating , count((review_jsonb#>>'{review,rating}')::int) AS count_rating FROM reviews GROUP BY 1 ; The same query as before with the previously created BTREE Index: 1605ms
  • 30. User Group / Conferences PostgreSQL User Group Berlin PGDay FOSDEM Brussels, January 29 2016 FOSDEM Dev Room Brussels, January 31 2016
  • 31. Link List Extended JSONB functions for 9.5 Extended JSONB functions as extension for 9.4 from PGXN (jsonbx) Slide and source on Github: https://github.com/sjstoelting/talks
  • 32. JSON by the other Elephant This document by Stefanie Janine Stölting is covered by the Creative Commons Attribution 4.0 International