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/Kayaki...
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/Kayaki...
• 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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
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
3...
• WHO AM I ?
• NESTED SET INTRO
• NESTED SET BASIC QUERIES
• NESTED SET BASIC QUERIES OPTIMIZATION
•
•
•
JWC13 - Eli Aschk...
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,…....
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Common INSERT
INSERT INTO #__ (part_number, unit_price,
eau,…....
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Amended INSERT
php: $concat = preg_replace('/s+/', '', $input)...
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Optimized INSERT
php: $concat = preg_replace('/s+/', '', $inpu...
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Optimized INSERT (* BATCH)
php: $concat = preg_replace('/s+/',...
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 EXPLAI...
• WHO AM I ?
• NESTED SET INTRO
• NESTED SET BASIC QUERIES
• NESTED SET BASIC QUERIES OPTIMIZATION
• STRING LOOKUP OPTIMIZ...
•
•
•
•
•
•
•
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
Upcoming SlideShare
Loading in …5
×

Simple Nested Sets and some other DB optimizations

449 views

Published on

Simple explanation of how nested sets work and how to perform some basic tasks on them

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
449
On SlideShare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Simple Nested Sets and some other DB optimizations

  1. 1. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  2. 2. • • • • • • • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  3. 3. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Eli Aschkenasy I LOVE DATA!
  4. 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. 5. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Eli Aschkenasy I LOVE JOOMLA!
  6. 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. 7. • WHO AM I ? • • • • • • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  8. 8. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  9. 9. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  10. 10. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  11. 11. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  12. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 34. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  35. 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. 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. 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. 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. 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. 40. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles Indexing Options ALWAYS BENCHMARK (against COLD DB)
  41. 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. 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. 43. • • • • • • • JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles
  44. 44. JWC13 - Eli Aschkenasy - Nested Sets & DB Design Principles

×