SlideShare a Scribd company logo
1 of 44
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
•
•
•
•
•
•
•
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Eli Aschkenasy
I LOVE
DATA!
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Eli Aschkenasy
 Lived in 7 countries
 Live for Skiing/Kayaking
 Love! Data
 Like Joomla!
DATA
I LOVE
DATA!
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Eli Aschkenasy
I LOVE
JOOMLA!
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Eli Aschkenasy
 Lived in 7 countries
 Live for Skiing/Kayaking
 Like Data
 Love Joomla!
JOOMLA
• WHO AM I ?
•
•
•
•
•
•
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
ROOT NODE
root - lft = 1
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
TOTAL NODES
(rgt – lft + 1) / 2 = total
(36 – 1 + 1) / 2 = 18
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
IS LEAF NODE?
leaf = (rgt – lft ==1) ? true : false;
true = (5 – 4 == 1)
false = (8 – 3 == 1)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
LEAF NODE OPTIMIZATION
Naïve implementation
SELECT *
FROM #__
WHERE rgt – lft = 1
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
LEAF NODE OPTIMIZATION
Normal implementation
SELECT *
FROM #__
WHERE lft = (rgt -1)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
LEAF NODE OPTIMIZATION
Optimized implementation
SELECT x, x, x
FROM #__
WHERE lft = (rgt -1)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
SUBTREE SELECTION
Schoolbook implementation
SELECT c.type AS choices, b.type AS
bottom
FROM #__ AS c, #__ AS b
WHERE c.lft BETWEEN b.lft AND b.rgt
AND c.rgt BETWEEN b.lft AND b.rgt;
Anything between 22 and 35
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
SUBTREE SELECTION OPTIMIZATION
Naïve implementation
SELECT c.type AS choices, b.type AS
bottom
FROM #__ AS c, #__ AS b
WHERE c.lft BETWEEN b.lft AND b.rgt;
Anything between 22 and 35
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
SUBTREE SELECTION ONLY SUBTREE
Naïve implementation
SELECT c.type AS choices, b.type AS
bottom
FROM #__ AS c, #__ AS b
WHERE c.lft BETWEEN b.lft AND b.rgt
AND c.lft <> b.lft;
Anything between 22 and 35 and isn’t 22
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
SUBTREE SELECTION ONLY SUBTREE
Optimized implementation
SELECT c.type AS choices, b.type AS
bottom
FROM #__ AS c, #__ AS b
WHERE c.lft BETWEEN (b.lft+1) AND b.rgt;
Anyone between 23and 35
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
PATH TO A NODE
SELECT alias
FROM #__
WHERE lft < 4 AND rgt > 5
ORDER BY lft ASC;
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
PATH TO A NODE
Including leaf node
SELECT alias
FROM #__
WHERE lft < 5 AND rgt > 4
ORDER BY lft ASC;
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
PATH TO A NODE
SELECT alias
FROM #__
WHERE lft < 27 AND rgt > 26
ORDER BY lft ASC;
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
LEVEL OF NODE
SELECT b.id, COUNT(a.id) AS level
FROM #__ AS a, #__ AS b
WHERE b.lft BETWEEN a.lft AND a.rgt
GROUP BY b.id
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
MAXIMUM DEPTH
SELECT MAX(level) AS height
FROM (
SELECT b.id, (COUNT(a.id) - 1) AS level
FROM #__ AS a, #__ AS b
WHERE b.lft BETWEEN a.lft AND a.rgt
GROUP BY b.id
) AS L1
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
PARENT ID OF A NODE
SELECT id, (SELECT id
FROM #__ t2
WHERE t2.lft < t1.lft AND t2.rgt > t1.rgt
ORDER BY t2.rgt-t1.rgt ASC
LIMIT 1)
AS parent FROM #__ t1
ORDER BY (rgt-lft) DESC
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
INSERT A NODE
UPDATE #__ SET rgt=rgt+2 WHERE rgt >= 25;
UPDATE #__ SET lft=lft+2 WHERE lft >= 24;
INSERT INTO #__ SET lft=24, rgt=25, ….;
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
5
2
3
2
8
2
4
2
5
2
6
2
7
2
9
3
4
3
0
3
2
3
1
3
3
3
6
INSERT A NODE
UPDATE #__ SET rgt=rgt+2 WHERE rgt >= 25;
UPDATE #__ SET lft=lft+2 WHERE lft >= 24;
INSERT INTO #__ SET lft=24, rgt=25, ….;
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Root Node
1
2
3
4 5
lft rgt
1 36
2 15
3 8
4 5
6 7
9 14
10 11
12 13
16 21
17 18
19 20
22 35
23 28
24 25
26 27
29 34
30 31
32 33
6 7
8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
3
7
2
3
3
0
2
6
2
7
2
8
2
9
3
1
3
6
3
2
3
4
3
3
3
5
3
8
INSERT A NODE
UPDATE #__ SET rgt=rgt+2 WHERE rgt >= 25;
UPDATE #__ SET lft=lft+2 WHERE lft >= 24;
INSERT INTO #__ SET lft=24, rgt=25, ….;
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
2
4
2
5
• WHO AM I ?
• NESTED SET INTRO
• NESTED SET BASIC QUERIES
• NESTED SET BASIC QUERIES OPTIMIZATION
•
•
•
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Common INSERT
INSERT INTO #__ (part_number, unit_price,
eau,….)
VALUES (….);
Common SELECT
SELECT unit_price FROM #__
WHERE part_number = $part_number;
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Common INSERT
INSERT INTO #__ (part_number, unit_price,
eau,….)
VALUES (….);
Common SELECT
SELECT unit_price FROM #__
WHERE part_number = $part_number;
PROBLEMS:
1. Speed
2. Data accuracy (inconsistent white spacing)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Amended INSERT
php: $concat = preg_replace('/s+/', '', $input);
INSERT INTO #__ (part_number, unit_price,
eau,part_number_concat,…)
VALUES (…,$concat);
Amended SELECT
SELECT unit_price FROM #__
WHERE part_number_concat = $concat;
PROBLEMS:
1. Speed
2. Data accuracy (inconsistent white spacing)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Optimized INSERT
php: $concat = preg_replace('/s+/', '', $input);
php: $crc = crc32($concat);
INSERT INTO #__ (part_number, unit_price,
eau,part_number_concat,crc_partnumberconcat…)
VALUES (…,$concat,$crc);
Optimized SELECT
SELECT unit_price FROM #__
WHERE crc_partnumberconcat = $crc
AND part_number_concat = $concat; (singularity)
PROBLEMS:
1. Speed
2. Data accuracy (inconsistent white spacing)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Optimized INSERT (* BATCH)
php: $concat = preg_replace('/s+/', '', $input);
php: $crc = crc32($concat);
ALTER TABLE #__ DROP INDEX x
INSERT INTO #__ (part_number, unit_price,
eau,part_number_concat,crc_partnumberconcat…)
VALUES (…,$concat,$crc);
ALTER TABLE #__ ADD INDEX x (‘column’)
Optimized SELECT
SELECT unit_price FROM #__
WHERE crc_partnumberconcat = $crc
AND part_number_concat = $concat;
PROBLEMS:
1. Speed
2. Data accuracy (inconsistent white spacing)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Indexing Options
ALWAYS BENCHMARK (against COLD DB)
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Indexing Options
ALWAYS BENCHMARK (against COLD DB)
USE EXPLAIN (EXTENSIVELY!!!)
Assumption:
I might search part_number_concat directly:
idx_pnc_crc (part_number_concat, crc_partnumberconcat)
Alternative:
idx_crc_pnc (crc_partnumberconcat, part_number_concat)
idx_pnc (part_number_concat)
• WHO AM I ?
• NESTED SET INTRO
• NESTED SET BASIC QUERIES
• NESTED SET BASIC QUERIES OPTIMIZATION
• STRING LOOKUP OPTIMIZATION TRICK
• INDEXING OPTIONS
•
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
•
•
•
•
•
•
•
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles

More Related Content

Similar to JWC13 Nested Sets DB Design

OOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASOOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASKyle Hailey
 
SequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational DatabaseSequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational Databasewangzhonnew
 
Access pattern of tags
Access pattern of tagsAccess pattern of tags
Access pattern of tagsHarish Chetty
 
A Deep Dive Into Understanding Apache Cassandra
A Deep Dive Into Understanding Apache CassandraA Deep Dive Into Understanding Apache Cassandra
A Deep Dive Into Understanding Apache CassandraDataStax Academy
 
Build a minial DBMS from scratch by Rust
Build a minial DBMS from scratch by RustBuild a minial DBMS from scratch by Rust
Build a minial DBMS from scratch by Rust安齊 劉
 
Advanced MongoDB Aggregation Pipelines
Advanced MongoDB Aggregation PipelinesAdvanced MongoDB Aggregation Pipelines
Advanced MongoDB Aggregation PipelinesTom Schreiber
 
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation PipelinesMongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation PipelinesMongoDB
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRsquared Academy
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
DrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal Cloud
DrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal CloudDrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal Cloud
DrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal CloudRonWilliamsIO
 
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018Alexander Tokarev
 
String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?Jeremy Schneider
 
Descriptive analytics in r programming language
Descriptive analytics in r programming languageDescriptive analytics in r programming language
Descriptive analytics in r programming languageAshwini Mathur
 
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...Sebastian Wild
 
Oracle 10g Performance: chapter 00 intro live_short
Oracle 10g Performance: chapter 00 intro live_shortOracle 10g Performance: chapter 00 intro live_short
Oracle 10g Performance: chapter 00 intro live_shortKyle Hailey
 
Another Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and PerlAnother Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and PerlRoy Zimmer
 
Solving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsSolving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsAlexander Korotkov
 
Modeling computer networks by colored Petri nets
Modeling computer networks by colored Petri netsModeling computer networks by colored Petri nets
Modeling computer networks by colored Petri netsDmitryZaitsev5
 
Bind Peeking - The Endless Tuning Nightmare
Bind Peeking - The Endless Tuning NightmareBind Peeking - The Endless Tuning Nightmare
Bind Peeking - The Endless Tuning NightmareSage Computing Services
 

Similar to JWC13 Nested Sets DB Design (20)

OOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASOOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AAS
 
SequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational DatabaseSequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational Database
 
SSL Securing Oracle DB
SSL Securing Oracle DBSSL Securing Oracle DB
SSL Securing Oracle DB
 
Access pattern of tags
Access pattern of tagsAccess pattern of tags
Access pattern of tags
 
A Deep Dive Into Understanding Apache Cassandra
A Deep Dive Into Understanding Apache CassandraA Deep Dive Into Understanding Apache Cassandra
A Deep Dive Into Understanding Apache Cassandra
 
Build a minial DBMS from scratch by Rust
Build a minial DBMS from scratch by RustBuild a minial DBMS from scratch by Rust
Build a minial DBMS from scratch by Rust
 
Advanced MongoDB Aggregation Pipelines
Advanced MongoDB Aggregation PipelinesAdvanced MongoDB Aggregation Pipelines
Advanced MongoDB Aggregation Pipelines
 
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation PipelinesMongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
DrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal Cloud
DrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal CloudDrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal Cloud
DrupalCamp Colorado 2014 Building A Scalable Private VMware Based Drupal Cloud
 
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
 
String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?
 
Descriptive analytics in r programming language
Descriptive analytics in r programming languageDescriptive analytics in r programming language
Descriptive analytics in r programming language
 
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
 
Oracle 10g Performance: chapter 00 intro live_short
Oracle 10g Performance: chapter 00 intro live_shortOracle 10g Performance: chapter 00 intro live_short
Oracle 10g Performance: chapter 00 intro live_short
 
Another Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and PerlAnother Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
Another Way to Attack the BLOB: Server-side Access via PL/SQL and Perl
 
Solving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsSolving PostgreSQL wicked problems
Solving PostgreSQL wicked problems
 
Modeling computer networks by colored Petri nets
Modeling computer networks by colored Petri netsModeling computer networks by colored Petri nets
Modeling computer networks by colored Petri nets
 
Bind Peeking - The Endless Tuning Nightmare
Bind Peeking - The Endless Tuning NightmareBind Peeking - The Endless Tuning Nightmare
Bind Peeking - The Endless Tuning Nightmare
 

Recently uploaded

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

JWC13 Nested Sets DB Design

  • 1. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 2. • • • • • • • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 3. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Eli Aschkenasy I LOVE DATA!
  • 4. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Eli Aschkenasy  Lived in 7 countries  Live for Skiing/Kayaking  Love! Data  Like Joomla! DATA I LOVE DATA!
  • 5. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Eli Aschkenasy I LOVE JOOMLA!
  • 6. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Eli Aschkenasy  Lived in 7 countries  Live for Skiing/Kayaking  Like Data  Love Joomla! JOOMLA
  • 7. • WHO AM I ? • • • • • • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 8. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 9. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 10. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 11. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 12. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 13. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 ROOT NODE root - lft = 1 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 14. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 TOTAL NODES (rgt – lft + 1) / 2 = total (36 – 1 + 1) / 2 = 18 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 15. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 IS LEAF NODE? leaf = (rgt – lft ==1) ? true : false; true = (5 – 4 == 1) false = (8 – 3 == 1) JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 16. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 LEAF NODE OPTIMIZATION Naïve implementation SELECT * FROM #__ WHERE rgt – lft = 1 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 17. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 LEAF NODE OPTIMIZATION Normal implementation SELECT * FROM #__ WHERE lft = (rgt -1) JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 18. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 LEAF NODE OPTIMIZATION Optimized implementation SELECT x, x, x FROM #__ WHERE lft = (rgt -1) JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 19. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 SUBTREE SELECTION Schoolbook implementation SELECT c.type AS choices, b.type AS bottom FROM #__ AS c, #__ AS b WHERE c.lft BETWEEN b.lft AND b.rgt AND c.rgt BETWEEN b.lft AND b.rgt; Anything between 22 and 35 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 20. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 SUBTREE SELECTION OPTIMIZATION Naïve implementation SELECT c.type AS choices, b.type AS bottom FROM #__ AS c, #__ AS b WHERE c.lft BETWEEN b.lft AND b.rgt; Anything between 22 and 35 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 21. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 SUBTREE SELECTION ONLY SUBTREE Naïve implementation SELECT c.type AS choices, b.type AS bottom FROM #__ AS c, #__ AS b WHERE c.lft BETWEEN b.lft AND b.rgt AND c.lft <> b.lft; Anything between 22 and 35 and isn’t 22 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 22. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 SUBTREE SELECTION ONLY SUBTREE Optimized implementation SELECT c.type AS choices, b.type AS bottom FROM #__ AS c, #__ AS b WHERE c.lft BETWEEN (b.lft+1) AND b.rgt; Anyone between 23and 35 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 23. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 24. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 PATH TO A NODE SELECT alias FROM #__ WHERE lft < 4 AND rgt > 5 ORDER BY lft ASC; JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 25. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 PATH TO A NODE Including leaf node SELECT alias FROM #__ WHERE lft < 5 AND rgt > 4 ORDER BY lft ASC; JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 26. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 PATH TO A NODE SELECT alias FROM #__ WHERE lft < 27 AND rgt > 26 ORDER BY lft ASC; JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 27. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 LEVEL OF NODE SELECT b.id, COUNT(a.id) AS level FROM #__ AS a, #__ AS b WHERE b.lft BETWEEN a.lft AND a.rgt GROUP BY b.id JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 28. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 MAXIMUM DEPTH SELECT MAX(level) AS height FROM ( SELECT b.id, (COUNT(a.id) - 1) AS level FROM #__ AS a, #__ AS b WHERE b.lft BETWEEN a.lft AND a.rgt GROUP BY b.id ) AS L1 JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 29. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 PARENT ID OF A NODE SELECT id, (SELECT id FROM #__ t2 WHERE t2.lft < t1.lft AND t2.rgt > t1.rgt ORDER BY t2.rgt-t1.rgt ASC LIMIT 1) AS parent FROM #__ t1 ORDER BY (rgt-lft) DESC JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 30. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 INSERT A NODE UPDATE #__ SET rgt=rgt+2 WHERE rgt >= 25; UPDATE #__ SET lft=lft+2 WHERE lft >= 24; INSERT INTO #__ SET lft=24, rgt=25, ….; JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 31. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 5 2 3 2 8 2 4 2 5 2 6 2 7 2 9 3 4 3 0 3 2 3 1 3 3 3 6 INSERT A NODE UPDATE #__ SET rgt=rgt+2 WHERE rgt >= 25; UPDATE #__ SET lft=lft+2 WHERE lft >= 24; INSERT INTO #__ SET lft=24, rgt=25, ….; JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 32. Root Node 1 2 3 4 5 lft rgt 1 36 2 15 3 8 4 5 6 7 9 14 10 11 12 13 16 21 17 18 19 20 22 35 23 28 24 25 26 27 29 34 30 31 32 33 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 3 7 2 3 3 0 2 6 2 7 2 8 2 9 3 1 3 6 3 2 3 4 3 3 3 5 3 8 INSERT A NODE UPDATE #__ SET rgt=rgt+2 WHERE rgt >= 25; UPDATE #__ SET lft=lft+2 WHERE lft >= 24; INSERT INTO #__ SET lft=24, rgt=25, ….; JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles 2 4 2 5
  • 33. • WHO AM I ? • NESTED SET INTRO • NESTED SET BASIC QUERIES • NESTED SET BASIC QUERIES OPTIMIZATION • • • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 34. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 35. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Common INSERT INSERT INTO #__ (part_number, unit_price, eau,….) VALUES (….); Common SELECT SELECT unit_price FROM #__ WHERE part_number = $part_number;
  • 36. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Common INSERT INSERT INTO #__ (part_number, unit_price, eau,….) VALUES (….); Common SELECT SELECT unit_price FROM #__ WHERE part_number = $part_number; PROBLEMS: 1. Speed 2. Data accuracy (inconsistent white spacing)
  • 37. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Amended INSERT php: $concat = preg_replace('/s+/', '', $input); INSERT INTO #__ (part_number, unit_price, eau,part_number_concat,…) VALUES (…,$concat); Amended SELECT SELECT unit_price FROM #__ WHERE part_number_concat = $concat; PROBLEMS: 1. Speed 2. Data accuracy (inconsistent white spacing)
  • 38. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Optimized INSERT php: $concat = preg_replace('/s+/', '', $input); php: $crc = crc32($concat); INSERT INTO #__ (part_number, unit_price, eau,part_number_concat,crc_partnumberconcat…) VALUES (…,$concat,$crc); Optimized SELECT SELECT unit_price FROM #__ WHERE crc_partnumberconcat = $crc AND part_number_concat = $concat; (singularity) PROBLEMS: 1. Speed 2. Data accuracy (inconsistent white spacing)
  • 39. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Optimized INSERT (* BATCH) php: $concat = preg_replace('/s+/', '', $input); php: $crc = crc32($concat); ALTER TABLE #__ DROP INDEX x INSERT INTO #__ (part_number, unit_price, eau,part_number_concat,crc_partnumberconcat…) VALUES (…,$concat,$crc); ALTER TABLE #__ ADD INDEX x (‘column’) Optimized SELECT SELECT unit_price FROM #__ WHERE crc_partnumberconcat = $crc AND part_number_concat = $concat; PROBLEMS: 1. Speed 2. Data accuracy (inconsistent white spacing)
  • 40. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Indexing Options ALWAYS BENCHMARK (against COLD DB)
  • 41. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Indexing Options ALWAYS BENCHMARK (against COLD DB) USE EXPLAIN (EXTENSIVELY!!!) Assumption: I might search part_number_concat directly: idx_pnc_crc (part_number_concat, crc_partnumberconcat) Alternative: idx_crc_pnc (crc_partnumberconcat, part_number_concat) idx_pnc (part_number_concat)
  • 42. • WHO AM I ? • NESTED SET INTRO • NESTED SET BASIC QUERIES • NESTED SET BASIC QUERIES OPTIMIZATION • STRING LOOKUP OPTIMIZATION TRICK • INDEXING OPTIONS • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 43. • • • • • • • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  • 44. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles