SlideShare a Scribd company logo
#JAB14 - @EliAschkenasy – Integral DB Design
•
•
•
•
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
• WHO AM I ?
•
•
•
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
#JAB14 - @EliAschkenasy – Integral DB Design
#JAB14 - @EliAschkenasy – Integral DB Design
#JAB14 - @EliAschkenasy – Integral DB Design
Categories
Articles
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
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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)
#JAB14 - @EliAschkenasy – Integral DB Design
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)
#JAB14 - @EliAschkenasy – Integral DB Design
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
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
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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 (breadcrumb)
SELECT alias
FROM #__
WHERE lft < 4 AND rgt > 5
ORDER BY lft ASC;
#JAB14 - @EliAschkenasy – Integral DB Design
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 GROUP_CONCAT(alias SEPARATOR '/') as
path
FROM options
WHERE lft <= 4 AND rgt >= 5
ORDER BY lft ASC;
(sandal/blouse/skirt/woman)
#JAB14 - @EliAschkenasy – Integral DB Design
PATH TO A NODE
SELECT GROUP_CONCAT(alias SEPARATOR '/') as
path
FROM con_menu
WHERE lft <= 20 AND rgt >= 21 AND lft > 1
ORDER BY lft ASC;
(Messaging/Read Private Message)
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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
AND a.lft = 19
GROUP BY b.id
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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 parentid FROM #__ t1
ORDER BY (rgt-lft) DESC
#JAB14 - @EliAschkenasy – Integral DB Design
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
#JAB14 - @EliAschkenasy – Integral DB Design
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, ….;
#JAB14 - @EliAschkenasy – Integral DB Design
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, ….;
#JAB14 - @EliAschkenasy – Integral DB Design
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, ….;
2
4
2
5
#JAB14 - @EliAschkenasy – Integral DB Design
• WHO AM I ?
• NESTED SET INTRO
• NESTED SET BASIC QUERIES
• NESTED SET BASIC QUERIES OPTIMIZATION
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
#JAB14 - @EliAschkenasy – Integral DB Design
Common INSERT
INSERT INTO #__ (part_number, unit_price,
eau,….)
VALUES (….);
Common SELECT
SELECT unit_price FROM #__
WHERE part_number = $part_number;
#JAB14 - @EliAschkenasy – Integral DB Design
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)
#JAB14 - @EliAschkenasy – Integral DB Design
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)
#JAB14 - @EliAschkenasy – Integral DB Design
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)
#JAB14 - @EliAschkenasy – Integral DB Design
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 32bit vs 64bit)
#JAB14 - @EliAschkenasy – Integral DB Design
Optimized INSERT
php: $concat = preg_replace('/s+/', '', $input);
INSERT INTO #__ (part_number, unit_price,
eau,part_number_concat,sha_partnumberconcat…)
VALUES (…,$concat,SHA1($concat));
Optimized SELECT
SELECT unit_price FROM #__
WHERE sha_partnumberconcat = SHA1($concat);
AND part_number_concat = $concat; (singularity)
PROBLEMS:
1. Speed
2. Data accuracy (inconsistent 32bit vs 64bit)
#JAB14 - @EliAschkenasy – Integral DB Design
Optimized INSERT (* BATCH)
php: $concat = preg_replace('/s+/', '', $input);
ALTER TABLE #__ DROP INDEX x
INSERT INTO #__ (part_number, unit_price,
eau,part_number_concat,sha_partnumberconcat…)
VALUES (…,$concat concat,SHA1($concat));
ALTER TABLE #__ ADD INDEX x (‘column’)
#JAB14 - @EliAschkenasy – Integral DB Design
Hits Counter Table
CREATE TABLE hit_counter (
cnt INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB;
INSERT INTO hit_counter (cnt) VALUES (0);
UPDATE hit_counter SET cnt = cnt + 1;
SELECT cnt FROM hit_counter;
#JAB14 - @EliAschkenasy – Integral DB Design
Hits Counter Table
CREATE TABLE hit_counter (
cnt INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB;
INSERT INTO hit_counter (cnt) VALUES (0);
UPDATE hit_counter SET cnt = cnt + 1;
SELECT cnt FROM hit_counter;
#JAB14 - @EliAschkenasy – Integral DB Design
Hits Counter Table (Optimized)
CREATE TABLE hit_counter (
slot TINYINT UNSIGNED NOT NULL PRIMARY KEY,
cnt INT UNSIGNED NOT NULL
) ENGINE=InnoDB;
INSERT INTO hit_counter VALUES
(0,0), (1,0), (2,0), (3,0), (4,0);
-- any amount of slots you require (n-1)
UPDATE hit_counter SET cnt = cnt + 1
WHERE slot = FLOOR(RAND() * 5);
-- the amount of slots you assigned
SELECT SUM(cnt) FROM hit_counter;
• WHO AM I ?
• NESTED SET INTRO
• NESTED SET BASIC QUERIES
• NESTED SET BASIC QUERIES OPTIMIZATION
• STRING LOOKUP OPTIMIZATION TRICK
• INDEXING OPTIONS
•
#JAB14 - @EliAschkenasy – Integral DB Design
#JAB14 - @EliAschkenasy – Integral DB Design
SELECT * FROM Orgchart SELECT a, c FROM Orgchart SELECT a, c FROM Orgchart
WHERE lft - rgt = 1; WHERE lft - rgt = 1; WHERE lft =(rgt – 1);
SELECT name FROM people mysql> ALTER TABLE people ADD KEY (idx_name(6));
WHERE name = ‘Hans’;
CREATE TABLE t ( KEY(c1,c2,c3) ? KEY(c1,c3) Specificity!
c1 INT,
c2 INT,
c3 INT,
KEY(c1),
KEY(c2),
KEY(c3)
);
#JAB14 - @EliAschkenasy – Integral DB Design
SELECT * FROM Orgchart SELECT a, c FROM Orgchart SELECT a, c FROM Orgchart
WHERE lft - rgt = 1; WHERE lft - rgt = 1; WHERE lft =(rgt – 1);
SELECT name FROM people mysql> ALTER TABLE people ADD KEY (idx_name(6));
WHERE name = ‘Hans’;
CREATE TABLE t ( KEY(c1,c2,c3) ? KEY(c1,c3) Specificity!
c1 INT,
c2 INT,
c3 INT,
KEY(c1),
KEY(c2),
KEY(c3)
);
SELECT cc FROM payment WHERE staff_id = 2 AND customer_id = 584;
KEY(staff_id,customer_id) ?
SELECT SUM(staff_id = 2), SUM(customer_id = 584) FROM paymentG
*************************** 1. row ***************************
SUM(staff_id = 2): 7992
SUM(customer_id = 584): 30
ALTER TABLE payment ADD KEY(customer_id, staff_id);
#JAB14 - @EliAschkenasy – Integral DB Design
CREATE TABLE profile(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sex CHAR(1) NOT NULL,
age TINYINT NOT NULL,
country VARCHAR(255) NOT NULL,
region VARCHAR(255) NOT NULL DEFAULT ‘’,
city VARCHAR(255) NOT NULL DEFAULT ‘’,
color_hair VARCHAR(255) NOT NULL DEFAULT ‘’,
color_eyes VARCHAR(255) NOT NULL DEFAULT ‘’,
name
…
…
rating TINYINT NOT NULL DEFAULT 1,
PRIMARY KEY(id)
);
#JAB14 - @EliAschkenasy – Integral DB Design
CREATE TABLE profile(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sex CHAR(1) NOT NULL,
age TINYINT NOT NULL,
country VARCHAR(255) NOT NULL,
region VARCHAR(255) NOT NULL DEFAULT ‘’,
city VARCHAR(255) NOT NULL DEFAULT ‘’,
color_hair VARCHAR(255) NOT NULL DEFAULT ‘’,
color_eyes VARCHAR(255) NOT NULL DEFAULT ‘’,
name
…
…
rating TINYINT NOT NULL DEFAULT 1,
PRIMARY KEY(id)
);
WHERE age BETWEEN 18 AND 25
ORDER BY rating ASC
MySQL can’t use added index if primary
index uses range criterion
KEY(sex, country)
NO Selectivity!!!
KEY(sex, country)
Assumption: all searches will include sex and
most will include country
Trick: AND sex IN('m', 'f')
#JAB14 - @EliAschkenasy – Integral DB Design
CREATE TABLE profile(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sex CHAR(1) NOT NULL,
age TINYINT NOT NULL,
country VARCHAR(255) NOT NULL,
region VARCHAR(255) NOT NULL DEFAULT ‘’,
city VARCHAR(255) NOT NULL DEFAULT ‘’,
color_hair VARCHAR(255) NOT NULL DEFAULT ‘’,
color_eyes VARCHAR(255) NOT NULL DEFAULT ‘’,
name
…
…
rating TINYINT NOT NULL DEFAULT 1,
PRIMARY KEY(id)
);
(sex, country, age)
(sex, country, region, age)
(sex, country, region, city, age)
Using the IN() trick, we can implement just
the
(sex, country, region, city, age) index.
Why is age at end?
Remember our range problem?
MySQL uses indexes from left to right until
the first range query
Trick: Convert WHERE age BETWEEN 18 and 25 to
WHERE age IN(18,19,20,21,22,23,24,25)
#JAB14 - @EliAschkenasy – Integral DB Design
•
#JAB14 - @EliAschkenasy – Integral DB Design
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
•
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
•
•
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
•
•
•
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
•
•
•
•
•
•
•
#JAB14 - @EliAschkenasy – Integral DB Design
#JAB14 - @EliAschkenasy – Integral DB Design

More Related Content

Similar to MySQL Integral DB Design #JAB14

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
 
SQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27thSQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27th
Dave Stokes
 
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
Scott Keck-Warren
 
Data Exploration with Apache Drill: Day 1
Data Exploration with Apache Drill:  Day 1Data Exploration with Apache Drill:  Day 1
Data Exploration with Apache Drill: Day 1
Charles Givre
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Citus Data
 
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
Amazon Web Services
 
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Dave Stokes
 
SequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational DatabaseSequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational Database
wangzhonnew
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
Om Vikram Thapa
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
guest9912e5
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
Jonathan Levin
 
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Cathrine Wilhelmsen
 
It's Not You. It's Your Data Model.
It's Not You. It's Your Data Model.It's Not You. It's Your Data Model.
It's Not You. It's Your Data Model.
Alex Powers
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
Alex Zaballa
 
MySQL Indexing Crash Course
MySQL Indexing Crash CourseMySQL Indexing Crash Course
MySQL Indexing Crash Course
Aaron Silverman
 
dbms-unit-_part-1.pptxeqweqweqweqweqweqweqweq
dbms-unit-_part-1.pptxeqweqweqweqweqweqweqweqdbms-unit-_part-1.pptxeqweqweqweqweqweqweqweq
dbms-unit-_part-1.pptxeqweqweqweqweqweqweqweq
wrushabhsirsat
 
Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...
Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...
Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...
Jim Czuprynski
 
How elephants survive in big data environments
How elephants survive in big data environmentsHow elephants survive in big data environments
How elephants survive in big data environments
Mary Prokhorova
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
Wim Godden
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
Wim Godden
 

Similar to MySQL Integral DB Design #JAB14 (20)

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?
 
SQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27thSQL For Programmers -- Boston Big Data Techcon April 27th
SQL For Programmers -- Boston Big Data Techcon April 27th
 
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
 
Data Exploration with Apache Drill: Day 1
Data Exploration with Apache Drill:  Day 1Data Exploration with Apache Drill:  Day 1
Data Exploration with Apache Drill: Day 1
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
 
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
(BDT203) From Zero to NoSQL Hero: Amazon DynamoDB Tutorial | AWS re:Invent 2014
 
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
 
SequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational DatabaseSequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational Database
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
 
It's Not You. It's Your Data Model.
It's Not You. It's Your Data Model.It's Not You. It's Your Data Model.
It's Not You. It's Your Data Model.
 
Oracle SQL Tuning
Oracle SQL TuningOracle SQL Tuning
Oracle SQL Tuning
 
MySQL Indexing Crash Course
MySQL Indexing Crash CourseMySQL Indexing Crash Course
MySQL Indexing Crash Course
 
dbms-unit-_part-1.pptxeqweqweqweqweqweqweqweq
dbms-unit-_part-1.pptxeqweqweqweqweqweqweqweqdbms-unit-_part-1.pptxeqweqweqweqweqweqweqweq
dbms-unit-_part-1.pptxeqweqweqweqweqweqweqweq
 
Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...
Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...
Vote Early, Vote Often: From Napkin to Canvassing Application in a Single Wee...
 
How elephants survive in big data environments
How elephants survive in big data environmentsHow elephants survive in big data environments
How elephants survive in big data environments
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 

Recently uploaded

Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
aisafed42
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
anfaltahir1010
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
Reetu63
 

Recently uploaded (20)

Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
 

MySQL Integral DB Design #JAB14

  • 1. #JAB14 - @EliAschkenasy – Integral DB Design
  • 3. • WHO AM I ? • • • • • • #JAB14 - @EliAschkenasy – Integral DB Design
  • 4. #JAB14 - @EliAschkenasy – Integral DB Design
  • 5. #JAB14 - @EliAschkenasy – Integral DB Design
  • 6. #JAB14 - @EliAschkenasy – Integral DB Design
  • 7.
  • 9. 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 10. 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 11. 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 IS LEAF NODE? leaf = (rgt – lft ==1) ? true : false; true = (5 – 4 == 1) false = (8 – 3 == 1) #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 LEAF NODE OPTIMIZATION Optimized implementation SELECT x, x, x FROM #__ WHERE lft = (rgt -1) #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 SUBTREE SELECTION OPTIMIZATION 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 PATH TO A NODE (breadcrumb) SELECT alias FROM #__ WHERE lft < 4 AND rgt > 5 ORDER BY lft ASC; #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 PATH TO A NODE SELECT GROUP_CONCAT(alias SEPARATOR '/') as path FROM options WHERE lft <= 4 AND rgt >= 5 ORDER BY lft ASC; (sandal/blouse/skirt/woman) #JAB14 - @EliAschkenasy – Integral DB Design
  • 20. PATH TO A NODE SELECT GROUP_CONCAT(alias SEPARATOR '/') as path FROM con_menu WHERE lft <= 20 AND rgt >= 21 AND lft > 1 ORDER BY lft ASC; (Messaging/Read Private Message) #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 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 AND a.lft = 19 GROUP BY b.id #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 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 parentid FROM #__ t1 ORDER BY (rgt-lft) DESC #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 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 #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 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, ….; #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 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, ….; #JAB14 - @EliAschkenasy – Integral DB Design
  • 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 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, ….; 2 4 2 5 #JAB14 - @EliAschkenasy – Integral DB Design
  • 29. • WHO AM I ? • NESTED SET INTRO • NESTED SET BASIC QUERIES • NESTED SET BASIC QUERIES OPTIMIZATION • • • #JAB14 - @EliAschkenasy – Integral DB Design
  • 30. #JAB14 - @EliAschkenasy – Integral DB Design
  • 31. Common INSERT INSERT INTO #__ (part_number, unit_price, eau,….) VALUES (….); Common SELECT SELECT unit_price FROM #__ WHERE part_number = $part_number; #JAB14 - @EliAschkenasy – Integral DB Design
  • 32. 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) #JAB14 - @EliAschkenasy – Integral DB Design
  • 33. 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) #JAB14 - @EliAschkenasy – Integral DB Design
  • 34. 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) #JAB14 - @EliAschkenasy – Integral DB Design
  • 35. 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 32bit vs 64bit) #JAB14 - @EliAschkenasy – Integral DB Design
  • 36. Optimized INSERT php: $concat = preg_replace('/s+/', '', $input); INSERT INTO #__ (part_number, unit_price, eau,part_number_concat,sha_partnumberconcat…) VALUES (…,$concat,SHA1($concat)); Optimized SELECT SELECT unit_price FROM #__ WHERE sha_partnumberconcat = SHA1($concat); AND part_number_concat = $concat; (singularity) PROBLEMS: 1. Speed 2. Data accuracy (inconsistent 32bit vs 64bit) #JAB14 - @EliAschkenasy – Integral DB Design
  • 37. Optimized INSERT (* BATCH) php: $concat = preg_replace('/s+/', '', $input); ALTER TABLE #__ DROP INDEX x INSERT INTO #__ (part_number, unit_price, eau,part_number_concat,sha_partnumberconcat…) VALUES (…,$concat concat,SHA1($concat)); ALTER TABLE #__ ADD INDEX x (‘column’) #JAB14 - @EliAschkenasy – Integral DB Design
  • 38. Hits Counter Table CREATE TABLE hit_counter ( cnt INT UNSIGNED NOT NULL DEFAULT 0 ) ENGINE=InnoDB; INSERT INTO hit_counter (cnt) VALUES (0); UPDATE hit_counter SET cnt = cnt + 1; SELECT cnt FROM hit_counter; #JAB14 - @EliAschkenasy – Integral DB Design
  • 39. Hits Counter Table CREATE TABLE hit_counter ( cnt INT UNSIGNED NOT NULL DEFAULT 0 ) ENGINE=InnoDB; INSERT INTO hit_counter (cnt) VALUES (0); UPDATE hit_counter SET cnt = cnt + 1; SELECT cnt FROM hit_counter; #JAB14 - @EliAschkenasy – Integral DB Design Hits Counter Table (Optimized) CREATE TABLE hit_counter ( slot TINYINT UNSIGNED NOT NULL PRIMARY KEY, cnt INT UNSIGNED NOT NULL ) ENGINE=InnoDB; INSERT INTO hit_counter VALUES (0,0), (1,0), (2,0), (3,0), (4,0); -- any amount of slots you require (n-1) UPDATE hit_counter SET cnt = cnt + 1 WHERE slot = FLOOR(RAND() * 5); -- the amount of slots you assigned SELECT SUM(cnt) FROM hit_counter;
  • 40. • WHO AM I ? • NESTED SET INTRO • NESTED SET BASIC QUERIES • NESTED SET BASIC QUERIES OPTIMIZATION • STRING LOOKUP OPTIMIZATION TRICK • INDEXING OPTIONS • #JAB14 - @EliAschkenasy – Integral DB Design
  • 41. #JAB14 - @EliAschkenasy – Integral DB Design SELECT * FROM Orgchart SELECT a, c FROM Orgchart SELECT a, c FROM Orgchart WHERE lft - rgt = 1; WHERE lft - rgt = 1; WHERE lft =(rgt – 1); SELECT name FROM people mysql> ALTER TABLE people ADD KEY (idx_name(6)); WHERE name = ‘Hans’; CREATE TABLE t ( KEY(c1,c2,c3) ? KEY(c1,c3) Specificity! c1 INT, c2 INT, c3 INT, KEY(c1), KEY(c2), KEY(c3) );
  • 42. #JAB14 - @EliAschkenasy – Integral DB Design SELECT * FROM Orgchart SELECT a, c FROM Orgchart SELECT a, c FROM Orgchart WHERE lft - rgt = 1; WHERE lft - rgt = 1; WHERE lft =(rgt – 1); SELECT name FROM people mysql> ALTER TABLE people ADD KEY (idx_name(6)); WHERE name = ‘Hans’; CREATE TABLE t ( KEY(c1,c2,c3) ? KEY(c1,c3) Specificity! c1 INT, c2 INT, c3 INT, KEY(c1), KEY(c2), KEY(c3) ); SELECT cc FROM payment WHERE staff_id = 2 AND customer_id = 584; KEY(staff_id,customer_id) ? SELECT SUM(staff_id = 2), SUM(customer_id = 584) FROM paymentG *************************** 1. row *************************** SUM(staff_id = 2): 7992 SUM(customer_id = 584): 30 ALTER TABLE payment ADD KEY(customer_id, staff_id);
  • 43. #JAB14 - @EliAschkenasy – Integral DB Design CREATE TABLE profile( id INT UNSIGNED NOT NULL AUTO_INCREMENT, sex CHAR(1) NOT NULL, age TINYINT NOT NULL, country VARCHAR(255) NOT NULL, region VARCHAR(255) NOT NULL DEFAULT ‘’, city VARCHAR(255) NOT NULL DEFAULT ‘’, color_hair VARCHAR(255) NOT NULL DEFAULT ‘’, color_eyes VARCHAR(255) NOT NULL DEFAULT ‘’, name … … rating TINYINT NOT NULL DEFAULT 1, PRIMARY KEY(id) );
  • 44. #JAB14 - @EliAschkenasy – Integral DB Design CREATE TABLE profile( id INT UNSIGNED NOT NULL AUTO_INCREMENT, sex CHAR(1) NOT NULL, age TINYINT NOT NULL, country VARCHAR(255) NOT NULL, region VARCHAR(255) NOT NULL DEFAULT ‘’, city VARCHAR(255) NOT NULL DEFAULT ‘’, color_hair VARCHAR(255) NOT NULL DEFAULT ‘’, color_eyes VARCHAR(255) NOT NULL DEFAULT ‘’, name … … rating TINYINT NOT NULL DEFAULT 1, PRIMARY KEY(id) ); WHERE age BETWEEN 18 AND 25 ORDER BY rating ASC MySQL can’t use added index if primary index uses range criterion KEY(sex, country) NO Selectivity!!! KEY(sex, country) Assumption: all searches will include sex and most will include country Trick: AND sex IN('m', 'f')
  • 45. #JAB14 - @EliAschkenasy – Integral DB Design CREATE TABLE profile( id INT UNSIGNED NOT NULL AUTO_INCREMENT, sex CHAR(1) NOT NULL, age TINYINT NOT NULL, country VARCHAR(255) NOT NULL, region VARCHAR(255) NOT NULL DEFAULT ‘’, city VARCHAR(255) NOT NULL DEFAULT ‘’, color_hair VARCHAR(255) NOT NULL DEFAULT ‘’, color_eyes VARCHAR(255) NOT NULL DEFAULT ‘’, name … … rating TINYINT NOT NULL DEFAULT 1, PRIMARY KEY(id) ); (sex, country, age) (sex, country, region, age) (sex, country, region, city, age) Using the IN() trick, we can implement just the (sex, country, region, city, age) index. Why is age at end? Remember our range problem? MySQL uses indexes from left to right until the first range query Trick: Convert WHERE age BETWEEN 18 and 25 to WHERE age IN(18,19,20,21,22,23,24,25)
  • 46. #JAB14 - @EliAschkenasy – Integral DB Design
  • 47. • #JAB14 - @EliAschkenasy – Integral DB Design
  • 48. • • #JAB14 - @EliAschkenasy – Integral DB Design
  • 49. • • • #JAB14 - @EliAschkenasy – Integral DB Design
  • 50. • • • • #JAB14 - @EliAschkenasy – Integral DB Design
  • 54. #JAB14 - @EliAschkenasy – Integral DB Design