SlideShare a Scribd company logo
1 of 61
10 PERFORMANCE TIPS
WE TEND TO FORGET
Eli Aschkenasy
@EliAschkenasy
#JDNL16
April 16, 2016
10 performance tips we tend to forget
3 Categories
1. Indexing
2. Query Optimization
3. Resource Handling
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Indexes have two purposes:
1. Speed up access to data
2. (help) Enforce constraints
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Query Optimization
Happens in conjunction
with index strategy and
only with real data.
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Resource Handling
Might be the most
overlooked yet easiest-to-
fix part, but requires real
data as well.
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Gather
Data
Eli Aschkenasy - @EliAschkenasy - #JDNL16
DB Optimization – Logging Queries
mysql> SET GLOBAL slow_query_log = ON;
mysql> SET GLOBAL long_query_time = 0;
mysql> SET GLOBAL log_queries_not_using_indexes = ON;
Eli Aschkenasy - @EliAschkenasy - #JDNL16
DB Optimization – Logging Queries
Change to ON
Eli Aschkenasy - @EliAschkenasy - #JDNL16
DB Optimization – Analyzing Queries
Percona Toolkit for MySQL
Eli Aschkenasy - @EliAschkenasy - #JDNL16
DB Optimization – Analyzing Queries
http://nk.gl/slow_queries/analyze
For those of us who don’t want to commit.
Eli Aschkenasy - @EliAschkenasy - #JDNL16
WITH THIS DATA
WE CAN CHECK THE
IMPLEMENTATION
Eli Aschkenasy - @EliAschkenasy - #JDNL16
DB Optimization
Eli Aschkenasy - @EliAschkenasy - #JDNL16
DB Optimization
Here is my schema, what indexes do I need?
Eli Aschkenasy - @EliAschkenasy - #JDNL16
DB Optimization
Index choice depends on the queries
you run, not the data you have!
relational schema
design is based on
DATA
index
design is based on
QUERIES
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
works
Simple/Compound Searches
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
works
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
AND f_name = ‘Hans’
works
Simple/Compound Searches
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
works
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
AND f_name = ‘Hans’
works
SELECT * FROM PhoneBook
WHERE f_name = ‘Hans’
doesn‘t work
Simple/Compound Searches
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name LIKE ‘Ku%’
works
Range Lookup
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name LIKE ‘Ku%’
works
SELECT * FROM PhoneBook
WHERE l_name = ‘Ku%’
AND f_name = ‘Hans’
doesn‘t work
Range Lookup
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name LIKE ‘Ku%’
works
SELECT * FROM PhoneBook
WHERE l_name = ‘Ku%’
AND f_name = ‘Hans’
doesn‘t work
Range Lookup
Tip #1:
Remember: Any Range Comparison breaks index
benefit for subsequent columns
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
ORDER BY f_name
works
Sorting
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
ORDER BY f_name
works
Sorting
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
ORDER BY phone
doesn‘t work
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
ORDER BY f_name
works
Sorting
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
ORDER BY phone
doesn‘t work
Tip #2:
Remember: Index can only benefit the sorting of the
column immediately following last column used for
search Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT phone FROM PhoneBook
WHERE l_name = ‘Kuijpers’
AND f_name = ‘Hans’
works
Index – Only Search
Because phone is included in index
we can select it without overhead
even though we didn’t include it in
the search
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT phone FROM PhoneBook
WHERE l_name = ‘Kuijpers’
AND f_name = ‘Hans’
works
Index – Only Search
Tip #3:
Putting columns in index even if they are not part of
the query might speed things up – COVERING INDEX
Because phone is included in index
we can select it without overhead
even though we didn’t include it in
the search
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
idx (l_name, f_name, phone)
SELECT * FROM PhoneBook
WHERE l_name = ‘Kuijpers’
OR f_name = ‘Hans’
doesn‘t work
Disjoint Selector
additional index (f_name, phone)
would be required
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Insurance Company:
PolicyNumber
Agent
Type
idx (policy, name, type)
Index Size
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Insurance Company:
PolicyNumber
Agent
Type
idx (policy, name, type)
SELECT * FROM InsuranceCompany
WHERE type =
‘Vervoerdersaansprakelijkheidsverzekering’
Index Size
Type Options:
1. Vervoerdersaansprakelijkheidsverzekering
2. Bestuurdersaansprakelijkheidsverzekering
3. Overeenstemmingsbeoordelingsprocedures
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Insurance Company:
PolicyNumber
Agent
Type
idx (type(1))
SELECT * FROM InsuranceCompany
WHERE type =
‘Vervoerdersaansprakelijkheidsverzekering’
Index Size
Type Options:
1. Vervoerdersaansprakelijkheidsverzekering
2. Bestuurdersaansprakelijkheidsverzekering
3. Overeenstemmingsbeoordelingsprocedures
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Insurance Company:
PolicyNumber
Agent
Type
idx (type(1))
SELECT * FROM InsuranceCompany
WHERE type =
‘Vervoerdersaansprakelijkheidsverzekering’
Index Size
Type Options:
1. Vervoerdersaansprakelijkheidsverzekering
2. Bestuurdersaansprakelijkheidsverzekering
3. Overeenstemmingsbeoordelingsprocedures
Trick #1:
Use only part of the column as length of the index,
but remember that it will break the ‘covering’
property. Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Insurance Company:
PolicyNumber
Agent
Type
idx (type(1))
SELECT * FROM InsuranceCompany
WHERE type =
‘Vervoerdersaansprakelijkheidsverzekering’
Index Size
Type Options:
1. Vervoerdersaansprakelijkheidsverzekering
2. Bestuurdersaansprakelijkheidsverzekering
3. Overeenstemmingsbeoordelingsprocedures
Trick #1a:
Specificity Check
SELECT
COUNT(DISTINCT(type)) AS total,
COUNT(DISTINCT(LEFT(type,10))) AS t10,
COUNT(DISTINCT(LEFT(type,20))) AS t20
FROM Insurance Company;
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Insurance Company:
PolicyNumber
Agent
Type
idx (type(1))
SELECT * FROM InsuranceCompany
WHERE type =
‘Vervoerdersaansprakelijkheidsverzekering’
Index Estimation
Type Options:
1. Vervoerdersaansprakelijkheidsverzekering
2. Bestuurdersaansprakelijkheidsverzekering
3. Overeenstemmingsbeoordelingsprocedures
Trick #1b:
Cardinality
ANALYZE TABLES;
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Primer
Phone Book:
Last Name (ASC)
First Name (ASC)
Phone Number (RAND)
MySQL 5.5
idx (l_name) – traditional
idx (l_name, f_name, phone) – covering
Range Lookup
SELECT phone FROM PhoneBook
WHERE l_name = ‘Kuijpers’
AND f_name LIKE ‘%Hans%’
MySQL 5.6
idx (l_name, f_name)
Range access l_name, Filter clause on
f_name (only read if full row match)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Strategy
Choose Index order which benefits more queries
• SELECT * FROM t WHERE a=1 AND b=2
• SELECT * FROM t WHERE a>1 AND b=2
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Strategy
Choose Index order which benefits more queries
• SELECT * FROM t WHERE a=1 AND b=2
• SELECT * FROM t WHERE a>1 AND b=2
KEY (b, a) is better than KEY (a, b)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Strategy (Trick #2)
Choose Index order which benefits more queries
• SELECT * FROM t WHERE a=1 AND b=2
• SELECT * FROM t WHERE a>1 AND b=2
KEY (b, a) is better than KEY (a, b)
Index order (without restrictions) should be most
selective to least selective
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Strategy (Trick #1)
• SELECT * FROM t WHERE a=1 AND b=2
• SELECT * FROM t WHERE a>1 AND b=2
KEY (a, b)
• SELECT * FROM t WHERE a BETWEEN 2 AND 4 AND b=2
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Strategy (Trick #1)
• SELECT * FROM t WHERE a=1 AND b=2
• SELECT * FROM t WHERE a>1 AND b=2
KEY (a, b)
• SELECT * FROM t WHERE a BETWEEN 2 AND 4 AND b=2
KEY (a, b)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Query Optimization (Trick #1)
• SELECT * FROM t WHERE a=1 AND b=2
• SELECT * FROM t WHERE a>1 AND b=2
KEY (a, b)
• SELECT * FROM t WHERE a BETWEEN 2 AND 4 AND b=2
KEY (a, b)
• SELECT * FROM t WHERE a IN(2,3,4) AND b=2
KEY (a, b)
Enumerating Ranges will ensure the usage of both key parts
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Query Optimization (Trick #2)
CREATE TABLE profile(
id INT,
gender CHAR(1),
age TINYINT(3),
city VARCHAR(100),
PRIMARY KEY(id)
);
SELECT id FROM profile
WHERE city = “Amsterdam”
AND age BETWEEN 35 AND 40
KEY (city, age, id)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Query Optimization (Trick #2)
CREATE TABLE profile(
id INT,
gender CHAR(1),
age TINYINT(3),
city VARCHAR(100),
PRIMARY KEY(id)
);
SELECT id FROM profile
WHERE city = “Amsterdam”
AND age BETWEEN 35 AND 40
KEY (city, age, id)
SELECT id FROM profile
WHERE city = “Amsterdam”
AND gender = “F”
AND age BETWEEN 35 AND 40
KEY (city, gender, age, id)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Index Strategy (Trick #3)
CREATE TABLE profile(
id INT,
gender CHAR(1),
age TINYINT(3),
city VARCHAR(100),
PRIMARY KEY(id)
);
SELECT id FROM profile
WHERE city = “Amsterdam”
AND age BETWEEN 35 AND 40
KEY (city, gender, age, id)
SELECT id FROM profile
WHERE city = “Amsterdam”
AND gender = “F”
AND age BETWEEN 35 AND 40
KEY (city, gender, age, id)
Trick #3:
Use as little indexes as possible. A little query rewrite
could save massive overhead on the index-side.
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Query Optimization (Trick #2)
SELECT id FROM profile
WHERE city = “Amsterdam”
AND age BETWEEN 35 AND 40
KEY (city, gender, age, id)
SELECT id FROM profile
WHERE city = “Amsterdam”
AND gender = “F”
AND age BETWEEN 35 AND 40
KEY (city, gender, age, id)
SELECT id FROM profile
WHERE city = “Amsterdam”
AND gender IN(‘M’,’F’,’O’)
AND age IN(35,36,37,38,39,40)
KEY (city, gender, age, id)
SELECT id FROM profile
WHERE city = “Amsterdam”
AND gender = “F”
AND age IN(35,36,37,38,39,40)
KEY (city, gender, age, id)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Resource Handling (Trick #1)
SELECT id, name, picture, … , age
FROM profile
WHERE city = “Amsterdam”
AND gender IN(‘M’,’F’,’O’)
AND age IN(35,36,37,38,39,40)
ORDER BY rating
LIMIT 100, 10
(page 11)
Retrieves 110 rows and discards 100
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Resource Handling (Trick #1)
SELECT id, name, picture, … , age
FROM profile
WHERE city = “Amsterdam”
AND gender IN(‘M’,’F’,’O’)
AND age IN(35,36,37,38,39,40)
ORDER BY rating
LIMIT 100, 10
(page 11)
Retrieves 110 rows and discards 100
SELECT id, name, picture, … , age
FROM profile
INNER JOIN(
SELECT id
FROM profile
WHERE city = “Amsterdam”
AND gender IN(‘M’,’F’,’O’)
AND age IN(35,36,37,38,39,40)
ORDER BY rating
LIMIT 100, 10)
AS x
USING (id);
(page 11)
Retrieves 110 ids and discards 100
Retrieves only 10 rows of data
Eli Aschkenasy - @EliAschkenasy - #JDNL16
Query Optimization (Trick #3)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT id FROM url
WHERE url="http://www.joomladagen.nl";
CREATE TABLE pseudohash (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
url VARCHAR(255) NOT NULL,
url_crc INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY(id)
);
CREATE TRIGGER pseudohash_crc_ins
BEFORE INSERT ON pseudohash FOR EACH ROW BEGIN
SET NEW.url_crc=crc32(NEW.url);
CREATE TRIGGER pseudohash_crc_upd
BEFORE UPDATE ON pseudohash FOR EACH ROW BEGIN
SET NEW.url_crc=crc32(NEW.url);
SELECT id FROM url
WHERE url_crc=CRC32("http://www.joomladagen.nl")
AND url="http://www.joomladagen.nl";
Query Optimization (Trick #3)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT id FROM url
WHERE url="http://www.joomladagen.nl";
CREATE TABLE pseudohash (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
url VARCHAR(255) NOT NULL,
url_crc INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY(id)
);
CREATE TRIGGER pseudohash_crc_ins
BEFORE INSERT ON pseudohash FOR EACH ROW BEGIN
SET NEW.url_crc=crc32(NEW.url);
CREATE TRIGGER pseudohash_crc_upd
BEFORE UPDATE ON pseudohash FOR EACH ROW BEGIN
SET NEW.url_crc=crc32(NEW.url);
SELECT id FROM url
WHERE url_crc=CRC32("http://www.joomladagen.nl")
AND url="http://www.joomladagen.nl";
 Fixed length index
 Fixed length data
Resource Handling (Trick #2)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT *
FROM comment AS c
LEFT JOIN article AS a ON a.id_comment = c.id
LEFT JOIN users AS u ON a.created_by = u.id
WHERE a.title=‘joomladagen‘
AND u.name = ‘Hans Kuijpers’;
Resource Handling (Trick #2)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT *
FROM comment AS c
LEFT JOIN article AS a ON a.id_comment = c.id
LEFT JOIN users AS u ON a.created_by = u.id
WHERE a.title=‘joomladagen‘
AND u.name = ‘Jisse Reitsma’;
Resource Handling (Trick #2)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT *
FROM comment AS c
LEFT JOIN article AS a ON a.id_comment = c.id
LEFT JOIN users AS u ON a.created_by = u.id
WHERE a.title=‘joomladagen‘
AND u.name = ‘Jisse Reitsma’;
CREATE TABLE comment (
id INT(11) NOT NULL…,
tag VARCHAR(255)…,
importance TINYINT(3)…,
image BLOB…,
comment TEXT…,
created DATETIME…,
created_by INT(11)…,
modified…,
….,
….,
publish_up DATETIME,
PRIMARY KEY(`id`)
) ENGINE=InnoDB;
Resource Handling (Trick #2)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT *
FROM comment AS c
LEFT JOIN article AS a ON a.id_comment = c.id
LEFT JOIN users AS u ON a.created_by = u.id
WHERE a.title=‘joomladagen‘
AND u.name = ‘Jisse Reitsma’;
CREATE TABLE comment (
id INT(11) NOT NULL…,
tag VARCHAR(255)…,
importance TINYINT(3)…,
image BLOB…,
comment TEXT…,
created DATETIME…,
created_by INT(11)…,
modified…,
….,
….,
publish_up DATETIME,
PRIMARY KEY(`id`)
) ENGINE=InnoDB;
Resource Handling (Trick #2)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT c.comment
FROM comment AS c
LEFT JOIN article AS a ON a.id_comment = c.id
LEFT JOIN users AS u ON a.created_by = u.id
WHERE a.title=‘joomladagen‘
AND u.name = ‘Jisse Reitsma’;
CREATE TABLE comment (
id INT(11) NOT NULL…,
tag VARCHAR(255)…,
importance TINYINT(3)…,
image BLOB…,
comment TEXT…,
created DATETIME…,
created_by INT(11)…,
modified…,
….,
….,
publish_up DATETIME,
PRIMARY KEY(`id`)
) ENGINE=InnoDB;
Resource Handling (Trick #2a)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT SUBSTRING(c.comment, 0, 25) AS short_comment
FROM comment AS c
LEFT JOIN article AS a ON a.id_comment = c.id
LEFT JOIN users AS u ON a.created_by = u.id
WHERE a.title=‘joomladagen‘
AND u.name = ‘Jisse Reitsma’;
CREATE TABLE comment (
id INT(11) NOT NULL…,
tag VARCHAR(255)…,
importance TINYINT(3)…,
image BLOB…,
comment TEXT…,
created DATETIME…,
created_by INT(11)…,
modified…,
….,
….,
publish_up DATETIME,
PRIMARY KEY(`id`)
) ENGINE=InnoDB;
Query Optimization (Trick #4)
Eli Aschkenasy - @EliAschkenasy - #JDNL16
SELECT SUBSTRING(c.comment, 0, 25) AS short_comment
FROM comment AS c
LEFT JOIN article AS a ON a.id_comment = c.id
LEFT JOIN users AS u ON a.created_by = u.id
WHERE a.title=‘joomladagen‘
AND u.name = ‘Jisse Reitsma’;
SELECT id FROM users
WHERE name = ‘Jisse Reitsma’; 57125
SELECT id FROM article
WHERE title = ‘Joomladagen’
AND created_by = 57125; (123,223,934,1145)
SELECT SUBSTRING(comment, 0, 25) AS short_comment FROM comment
WHERE id IN(123,223,934,1145);
Trick #4:
Query cache validation of 3 tables.
Complex queries reduce likelihood of repetition.
THANK YOU
Eli Aschkenasy - @EliAschkenasy - #JDNL16
MySQL 5.7 – JSON
Eli Aschkenasy - @EliAschkenasy - #JDNL16
CREATE TABLE profile(
id INT(11)…,
profile TEXT…,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
INSERT INTO profile (id, profile)
VALUES (1, ‘{“handle”: “@EliAschkenasy”}’);
MySQL 5.7 – JSON
Eli Aschkenasy - @EliAschkenasy - #JDNL16
CREATE TABLE profile(
id INT(11)…,
profile TEXT…,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
INSERT INTO profile (id, profile)
VALUES (1, ‘{“handle”: “@EliAschkenasy”}’);
SELECT id FROM profile
WHERE field_name REGEXP '"handle":"([^"]*)@EliAschkenasy([^"]*)"';
MySQL 5.7 – JSON
Eli Aschkenasy - @EliAschkenasy - #JDNL16
CREATE TABLE profile(
id INT(11)…,
profile TEXT…,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
INSERT INTO profile (id, profile)
VALUES (1, ‘{“handle”: “@EliAschkenasy”}’);
SELECT id FROM profile
WHERE field_name REGEXP '"handle":"([^"]*)@EliAschkenasy([^"]*)"';
Full Table Scan (not cacheable just like full text search)
REGEX
MySQL 5.7 – JSON
Eli Aschkenasy - @EliAschkenasy - #JDNL16
CREATE TABLE profile(
id INT(11)…,
profile JSON,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
INSERT INTO profile (id, profile)
VALUES (1, ‘{“id”: 1, “handle”: “@EliAschkenasy”}’);
SELECT JSON_EXTRACT(profile, ‘$.handle’) FROM profile
WHERE id = 1; - @EliAschkenasy
MySQL 5.7 – JSON
Eli Aschkenasy - @EliAschkenasy - #JDNL16
CREATE TABLE profile(
id INT(11)…,
profile JSON,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
INSERT INTO profile (id, profile)
VALUES (1, ‘{“id”: 1, “handle”: “@EliAschkenasy”}’);
SELECT JSON_EXTRACT(profile, ‘$.handle’) FROM profile
WHERE id = 1; - @EliAschkenasy
 JSON data type consumes about 5% more memory
 JSON columns can’t have a default value
 JSON columns can’t be indexed
 BETWEEN, IN(), GREATEST(), LEAST() Are NOT supported yet!

More Related Content

Recently uploaded

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 

Recently uploaded (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

10 performance tips we tend to forget

  • 1. 10 PERFORMANCE TIPS WE TEND TO FORGET Eli Aschkenasy @EliAschkenasy #JDNL16 April 16, 2016
  • 2. 10 performance tips we tend to forget 3 Categories 1. Indexing 2. Query Optimization 3. Resource Handling Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 3. Indexes have two purposes: 1. Speed up access to data 2. (help) Enforce constraints Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 4. Query Optimization Happens in conjunction with index strategy and only with real data. Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 5. Resource Handling Might be the most overlooked yet easiest-to- fix part, but requires real data as well. Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 6. Gather Data Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 7. DB Optimization – Logging Queries mysql> SET GLOBAL slow_query_log = ON; mysql> SET GLOBAL long_query_time = 0; mysql> SET GLOBAL log_queries_not_using_indexes = ON; Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 8. DB Optimization – Logging Queries Change to ON Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 9. DB Optimization – Analyzing Queries Percona Toolkit for MySQL Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 10. DB Optimization – Analyzing Queries http://nk.gl/slow_queries/analyze For those of us who don’t want to commit. Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 11. WITH THIS DATA WE CAN CHECK THE IMPLEMENTATION Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 12. DB Optimization Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 13. DB Optimization Here is my schema, what indexes do I need? Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 14. DB Optimization Index choice depends on the queries you run, not the data you have! relational schema design is based on DATA index design is based on QUERIES Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 15. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 16. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ works Simple/Compound Searches Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 17. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ works SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ AND f_name = ‘Hans’ works Simple/Compound Searches Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 18. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ works SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ AND f_name = ‘Hans’ works SELECT * FROM PhoneBook WHERE f_name = ‘Hans’ doesn‘t work Simple/Compound Searches Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 19. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name LIKE ‘Ku%’ works Range Lookup Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 20. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name LIKE ‘Ku%’ works SELECT * FROM PhoneBook WHERE l_name = ‘Ku%’ AND f_name = ‘Hans’ doesn‘t work Range Lookup Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 21. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name LIKE ‘Ku%’ works SELECT * FROM PhoneBook WHERE l_name = ‘Ku%’ AND f_name = ‘Hans’ doesn‘t work Range Lookup Tip #1: Remember: Any Range Comparison breaks index benefit for subsequent columns Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 22. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ ORDER BY f_name works Sorting Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 23. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ ORDER BY f_name works Sorting SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ ORDER BY phone doesn‘t work Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 24. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ ORDER BY f_name works Sorting SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ ORDER BY phone doesn‘t work Tip #2: Remember: Index can only benefit the sorting of the column immediately following last column used for search Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 25. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT phone FROM PhoneBook WHERE l_name = ‘Kuijpers’ AND f_name = ‘Hans’ works Index – Only Search Because phone is included in index we can select it without overhead even though we didn’t include it in the search Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 26. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT phone FROM PhoneBook WHERE l_name = ‘Kuijpers’ AND f_name = ‘Hans’ works Index – Only Search Tip #3: Putting columns in index even if they are not part of the query might speed things up – COVERING INDEX Because phone is included in index we can select it without overhead even though we didn’t include it in the search Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 27. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) idx (l_name, f_name, phone) SELECT * FROM PhoneBook WHERE l_name = ‘Kuijpers’ OR f_name = ‘Hans’ doesn‘t work Disjoint Selector additional index (f_name, phone) would be required Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 28. Index Primer Insurance Company: PolicyNumber Agent Type idx (policy, name, type) Index Size Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 29. Index Primer Insurance Company: PolicyNumber Agent Type idx (policy, name, type) SELECT * FROM InsuranceCompany WHERE type = ‘Vervoerdersaansprakelijkheidsverzekering’ Index Size Type Options: 1. Vervoerdersaansprakelijkheidsverzekering 2. Bestuurdersaansprakelijkheidsverzekering 3. Overeenstemmingsbeoordelingsprocedures Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 30. Index Primer Insurance Company: PolicyNumber Agent Type idx (type(1)) SELECT * FROM InsuranceCompany WHERE type = ‘Vervoerdersaansprakelijkheidsverzekering’ Index Size Type Options: 1. Vervoerdersaansprakelijkheidsverzekering 2. Bestuurdersaansprakelijkheidsverzekering 3. Overeenstemmingsbeoordelingsprocedures Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 31. Index Primer Insurance Company: PolicyNumber Agent Type idx (type(1)) SELECT * FROM InsuranceCompany WHERE type = ‘Vervoerdersaansprakelijkheidsverzekering’ Index Size Type Options: 1. Vervoerdersaansprakelijkheidsverzekering 2. Bestuurdersaansprakelijkheidsverzekering 3. Overeenstemmingsbeoordelingsprocedures Trick #1: Use only part of the column as length of the index, but remember that it will break the ‘covering’ property. Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 32. Index Primer Insurance Company: PolicyNumber Agent Type idx (type(1)) SELECT * FROM InsuranceCompany WHERE type = ‘Vervoerdersaansprakelijkheidsverzekering’ Index Size Type Options: 1. Vervoerdersaansprakelijkheidsverzekering 2. Bestuurdersaansprakelijkheidsverzekering 3. Overeenstemmingsbeoordelingsprocedures Trick #1a: Specificity Check SELECT COUNT(DISTINCT(type)) AS total, COUNT(DISTINCT(LEFT(type,10))) AS t10, COUNT(DISTINCT(LEFT(type,20))) AS t20 FROM Insurance Company; Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 33. Index Primer Insurance Company: PolicyNumber Agent Type idx (type(1)) SELECT * FROM InsuranceCompany WHERE type = ‘Vervoerdersaansprakelijkheidsverzekering’ Index Estimation Type Options: 1. Vervoerdersaansprakelijkheidsverzekering 2. Bestuurdersaansprakelijkheidsverzekering 3. Overeenstemmingsbeoordelingsprocedures Trick #1b: Cardinality ANALYZE TABLES; Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 34. Index Primer Phone Book: Last Name (ASC) First Name (ASC) Phone Number (RAND) MySQL 5.5 idx (l_name) – traditional idx (l_name, f_name, phone) – covering Range Lookup SELECT phone FROM PhoneBook WHERE l_name = ‘Kuijpers’ AND f_name LIKE ‘%Hans%’ MySQL 5.6 idx (l_name, f_name) Range access l_name, Filter clause on f_name (only read if full row match) Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 35. Index Strategy Choose Index order which benefits more queries • SELECT * FROM t WHERE a=1 AND b=2 • SELECT * FROM t WHERE a>1 AND b=2 Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 36. Index Strategy Choose Index order which benefits more queries • SELECT * FROM t WHERE a=1 AND b=2 • SELECT * FROM t WHERE a>1 AND b=2 KEY (b, a) is better than KEY (a, b) Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 37. Index Strategy (Trick #2) Choose Index order which benefits more queries • SELECT * FROM t WHERE a=1 AND b=2 • SELECT * FROM t WHERE a>1 AND b=2 KEY (b, a) is better than KEY (a, b) Index order (without restrictions) should be most selective to least selective Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 38. Index Strategy (Trick #1) • SELECT * FROM t WHERE a=1 AND b=2 • SELECT * FROM t WHERE a>1 AND b=2 KEY (a, b) • SELECT * FROM t WHERE a BETWEEN 2 AND 4 AND b=2 Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 39. Index Strategy (Trick #1) • SELECT * FROM t WHERE a=1 AND b=2 • SELECT * FROM t WHERE a>1 AND b=2 KEY (a, b) • SELECT * FROM t WHERE a BETWEEN 2 AND 4 AND b=2 KEY (a, b) Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 40. Query Optimization (Trick #1) • SELECT * FROM t WHERE a=1 AND b=2 • SELECT * FROM t WHERE a>1 AND b=2 KEY (a, b) • SELECT * FROM t WHERE a BETWEEN 2 AND 4 AND b=2 KEY (a, b) • SELECT * FROM t WHERE a IN(2,3,4) AND b=2 KEY (a, b) Enumerating Ranges will ensure the usage of both key parts Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 41. Query Optimization (Trick #2) CREATE TABLE profile( id INT, gender CHAR(1), age TINYINT(3), city VARCHAR(100), PRIMARY KEY(id) ); SELECT id FROM profile WHERE city = “Amsterdam” AND age BETWEEN 35 AND 40 KEY (city, age, id) Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 42. Query Optimization (Trick #2) CREATE TABLE profile( id INT, gender CHAR(1), age TINYINT(3), city VARCHAR(100), PRIMARY KEY(id) ); SELECT id FROM profile WHERE city = “Amsterdam” AND age BETWEEN 35 AND 40 KEY (city, age, id) SELECT id FROM profile WHERE city = “Amsterdam” AND gender = “F” AND age BETWEEN 35 AND 40 KEY (city, gender, age, id) Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 43. Index Strategy (Trick #3) CREATE TABLE profile( id INT, gender CHAR(1), age TINYINT(3), city VARCHAR(100), PRIMARY KEY(id) ); SELECT id FROM profile WHERE city = “Amsterdam” AND age BETWEEN 35 AND 40 KEY (city, gender, age, id) SELECT id FROM profile WHERE city = “Amsterdam” AND gender = “F” AND age BETWEEN 35 AND 40 KEY (city, gender, age, id) Trick #3: Use as little indexes as possible. A little query rewrite could save massive overhead on the index-side. Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 44. Query Optimization (Trick #2) SELECT id FROM profile WHERE city = “Amsterdam” AND age BETWEEN 35 AND 40 KEY (city, gender, age, id) SELECT id FROM profile WHERE city = “Amsterdam” AND gender = “F” AND age BETWEEN 35 AND 40 KEY (city, gender, age, id) SELECT id FROM profile WHERE city = “Amsterdam” AND gender IN(‘M’,’F’,’O’) AND age IN(35,36,37,38,39,40) KEY (city, gender, age, id) SELECT id FROM profile WHERE city = “Amsterdam” AND gender = “F” AND age IN(35,36,37,38,39,40) KEY (city, gender, age, id) Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 45. Resource Handling (Trick #1) SELECT id, name, picture, … , age FROM profile WHERE city = “Amsterdam” AND gender IN(‘M’,’F’,’O’) AND age IN(35,36,37,38,39,40) ORDER BY rating LIMIT 100, 10 (page 11) Retrieves 110 rows and discards 100 Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 46. Resource Handling (Trick #1) SELECT id, name, picture, … , age FROM profile WHERE city = “Amsterdam” AND gender IN(‘M’,’F’,’O’) AND age IN(35,36,37,38,39,40) ORDER BY rating LIMIT 100, 10 (page 11) Retrieves 110 rows and discards 100 SELECT id, name, picture, … , age FROM profile INNER JOIN( SELECT id FROM profile WHERE city = “Amsterdam” AND gender IN(‘M’,’F’,’O’) AND age IN(35,36,37,38,39,40) ORDER BY rating LIMIT 100, 10) AS x USING (id); (page 11) Retrieves 110 ids and discards 100 Retrieves only 10 rows of data Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 47. Query Optimization (Trick #3) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT id FROM url WHERE url="http://www.joomladagen.nl"; CREATE TABLE pseudohash ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, url VARCHAR(255) NOT NULL, url_crc INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY(id) ); CREATE TRIGGER pseudohash_crc_ins BEFORE INSERT ON pseudohash FOR EACH ROW BEGIN SET NEW.url_crc=crc32(NEW.url); CREATE TRIGGER pseudohash_crc_upd BEFORE UPDATE ON pseudohash FOR EACH ROW BEGIN SET NEW.url_crc=crc32(NEW.url); SELECT id FROM url WHERE url_crc=CRC32("http://www.joomladagen.nl") AND url="http://www.joomladagen.nl";
  • 48. Query Optimization (Trick #3) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT id FROM url WHERE url="http://www.joomladagen.nl"; CREATE TABLE pseudohash ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, url VARCHAR(255) NOT NULL, url_crc INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY(id) ); CREATE TRIGGER pseudohash_crc_ins BEFORE INSERT ON pseudohash FOR EACH ROW BEGIN SET NEW.url_crc=crc32(NEW.url); CREATE TRIGGER pseudohash_crc_upd BEFORE UPDATE ON pseudohash FOR EACH ROW BEGIN SET NEW.url_crc=crc32(NEW.url); SELECT id FROM url WHERE url_crc=CRC32("http://www.joomladagen.nl") AND url="http://www.joomladagen.nl";  Fixed length index  Fixed length data
  • 49. Resource Handling (Trick #2) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT * FROM comment AS c LEFT JOIN article AS a ON a.id_comment = c.id LEFT JOIN users AS u ON a.created_by = u.id WHERE a.title=‘joomladagen‘ AND u.name = ‘Hans Kuijpers’;
  • 50. Resource Handling (Trick #2) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT * FROM comment AS c LEFT JOIN article AS a ON a.id_comment = c.id LEFT JOIN users AS u ON a.created_by = u.id WHERE a.title=‘joomladagen‘ AND u.name = ‘Jisse Reitsma’;
  • 51. Resource Handling (Trick #2) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT * FROM comment AS c LEFT JOIN article AS a ON a.id_comment = c.id LEFT JOIN users AS u ON a.created_by = u.id WHERE a.title=‘joomladagen‘ AND u.name = ‘Jisse Reitsma’; CREATE TABLE comment ( id INT(11) NOT NULL…, tag VARCHAR(255)…, importance TINYINT(3)…, image BLOB…, comment TEXT…, created DATETIME…, created_by INT(11)…, modified…, …., …., publish_up DATETIME, PRIMARY KEY(`id`) ) ENGINE=InnoDB;
  • 52. Resource Handling (Trick #2) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT * FROM comment AS c LEFT JOIN article AS a ON a.id_comment = c.id LEFT JOIN users AS u ON a.created_by = u.id WHERE a.title=‘joomladagen‘ AND u.name = ‘Jisse Reitsma’; CREATE TABLE comment ( id INT(11) NOT NULL…, tag VARCHAR(255)…, importance TINYINT(3)…, image BLOB…, comment TEXT…, created DATETIME…, created_by INT(11)…, modified…, …., …., publish_up DATETIME, PRIMARY KEY(`id`) ) ENGINE=InnoDB;
  • 53. Resource Handling (Trick #2) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT c.comment FROM comment AS c LEFT JOIN article AS a ON a.id_comment = c.id LEFT JOIN users AS u ON a.created_by = u.id WHERE a.title=‘joomladagen‘ AND u.name = ‘Jisse Reitsma’; CREATE TABLE comment ( id INT(11) NOT NULL…, tag VARCHAR(255)…, importance TINYINT(3)…, image BLOB…, comment TEXT…, created DATETIME…, created_by INT(11)…, modified…, …., …., publish_up DATETIME, PRIMARY KEY(`id`) ) ENGINE=InnoDB;
  • 54. Resource Handling (Trick #2a) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT SUBSTRING(c.comment, 0, 25) AS short_comment FROM comment AS c LEFT JOIN article AS a ON a.id_comment = c.id LEFT JOIN users AS u ON a.created_by = u.id WHERE a.title=‘joomladagen‘ AND u.name = ‘Jisse Reitsma’; CREATE TABLE comment ( id INT(11) NOT NULL…, tag VARCHAR(255)…, importance TINYINT(3)…, image BLOB…, comment TEXT…, created DATETIME…, created_by INT(11)…, modified…, …., …., publish_up DATETIME, PRIMARY KEY(`id`) ) ENGINE=InnoDB;
  • 55. Query Optimization (Trick #4) Eli Aschkenasy - @EliAschkenasy - #JDNL16 SELECT SUBSTRING(c.comment, 0, 25) AS short_comment FROM comment AS c LEFT JOIN article AS a ON a.id_comment = c.id LEFT JOIN users AS u ON a.created_by = u.id WHERE a.title=‘joomladagen‘ AND u.name = ‘Jisse Reitsma’; SELECT id FROM users WHERE name = ‘Jisse Reitsma’; 57125 SELECT id FROM article WHERE title = ‘Joomladagen’ AND created_by = 57125; (123,223,934,1145) SELECT SUBSTRING(comment, 0, 25) AS short_comment FROM comment WHERE id IN(123,223,934,1145); Trick #4: Query cache validation of 3 tables. Complex queries reduce likelihood of repetition.
  • 56. THANK YOU Eli Aschkenasy - @EliAschkenasy - #JDNL16
  • 57. MySQL 5.7 – JSON Eli Aschkenasy - @EliAschkenasy - #JDNL16 CREATE TABLE profile( id INT(11)…, profile TEXT…, PRIMARY KEY (`id`) ) ENGINE = InnoDB; INSERT INTO profile (id, profile) VALUES (1, ‘{“handle”: “@EliAschkenasy”}’);
  • 58. MySQL 5.7 – JSON Eli Aschkenasy - @EliAschkenasy - #JDNL16 CREATE TABLE profile( id INT(11)…, profile TEXT…, PRIMARY KEY (`id`) ) ENGINE = InnoDB; INSERT INTO profile (id, profile) VALUES (1, ‘{“handle”: “@EliAschkenasy”}’); SELECT id FROM profile WHERE field_name REGEXP '"handle":"([^"]*)@EliAschkenasy([^"]*)"';
  • 59. MySQL 5.7 – JSON Eli Aschkenasy - @EliAschkenasy - #JDNL16 CREATE TABLE profile( id INT(11)…, profile TEXT…, PRIMARY KEY (`id`) ) ENGINE = InnoDB; INSERT INTO profile (id, profile) VALUES (1, ‘{“handle”: “@EliAschkenasy”}’); SELECT id FROM profile WHERE field_name REGEXP '"handle":"([^"]*)@EliAschkenasy([^"]*)"'; Full Table Scan (not cacheable just like full text search) REGEX
  • 60. MySQL 5.7 – JSON Eli Aschkenasy - @EliAschkenasy - #JDNL16 CREATE TABLE profile( id INT(11)…, profile JSON, PRIMARY KEY (`id`) ) ENGINE = InnoDB; INSERT INTO profile (id, profile) VALUES (1, ‘{“id”: 1, “handle”: “@EliAschkenasy”}’); SELECT JSON_EXTRACT(profile, ‘$.handle’) FROM profile WHERE id = 1; - @EliAschkenasy
  • 61. MySQL 5.7 – JSON Eli Aschkenasy - @EliAschkenasy - #JDNL16 CREATE TABLE profile( id INT(11)…, profile JSON, PRIMARY KEY (`id`) ) ENGINE = InnoDB; INSERT INTO profile (id, profile) VALUES (1, ‘{“id”: 1, “handle”: “@EliAschkenasy”}’); SELECT JSON_EXTRACT(profile, ‘$.handle’) FROM profile WHERE id = 1; - @EliAschkenasy  JSON data type consumes about 5% more memory  JSON columns can’t have a default value  JSON columns can’t be indexed  BETWEEN, IN(), GREATEST(), LEAST() Are NOT supported yet!