Your SlideShare is downloading. ×
0
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Simple Nested Sets and some other DB optimizations
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Simple Nested Sets and some other DB optimizations

123

Published on

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

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
123
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  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

×