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 Simple Nested Sets and some other DB optimizations

OOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASOOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AAS
Kyle Hailey
 
Descriptive analytics in r programming language
Descriptive analytics in r programming languageDescriptive analytics in r programming language
Descriptive analytics in r programming language
Ashwini Mathur
 
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
Kyle Hailey
 

Similar to Simple Nested Sets and some other DB optimizations (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

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
Enterprise Knowledge
 
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
vu2urc
 

Recently uploaded (20)

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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 
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
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
[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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Simple Nested Sets and some other DB optimizations

  • 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