SlideShare a Scribd company logo
This is not a DB
optimization talk
It’s a talk about how doing things in a specific way leads to getting way
better results by default
“Preoptimization”
 Don’t denormalize by default
 Maintenance is King (by default)
 1 Thread Performance != Parallel
Performance
MyISAM vs InnoDB
Fast != Scalable
“Preoptimization”
I’ll just tune MySQL Parameters…
 Will get you out of SOME trouble
 But not a good “default” solution, specially if the
base is flawed
 Please do tune MySQLs defaults
 Not the theme for today 
Start with your Schema
 Your schema is probably the root cause of your
“My DB doesn’t scale” problems
 The solution is not “have a loose/no schema”
 How to fake a DB Design (Curtis Ovid Poe)
 https://www.youtube.com/watch?v=y1tcbhWLiUM
Data types: how (not) to bloat your DB
 Selecting data types with a bit of care is very
productive
 It makes more data fit in less space
 Optimizes use of InnoDB Buffer Pool, MyISAM Key
Buffer, Join Buffer, Sort Buffer, Smaller indexes
Your new best friend
 http://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html
 http://dev.mysql.com/doc/refman/5.5/en/storage-requirements.html
 …
DataTypes: NULL vs NOT NULL
 I don’t care about the NULL debate
 Saves Space
 Gives the DB hints
 Use NULLs wisely
Data Types: Integers
DataTypes: Integers
INT(1) == INT(10) == 4 bytes
That’s it!
DataTypes: Integer family
TINYINT: 1 byte
SMALLINT: 2 bytes
MEDIUMINT: 3 bytes
INT
BIGINT:8 bytes
UNSIGNED: “double”
the range
INTS and surrogate keys
 It’s good to have a surrogate key
 Just unique index the unique column
 Why?
 InnoDB stores the PK on the leafs of all indexes
 Indexes bloat if they’re “big keys”
Id columns for your tables
 INT by default for “things that can get big”
 Smaller INTs for smaller sets
 Do you really need a BIGINT?
 The magnitude comparison trick:
 Epochs in Unix have 4 bytes
 The epoch has been counting since 1970. Will run out in 2038
 An INT can identify ONE THING HAPPENING EVERY SECOND for 68 YEARS!
 Do you STILL need a BIGINT?
DataTypes: Integer family
TINYINT: 1 byte
SMALLINT: 2 bytes
MEDIUMINT: 3 bytes
INT
BIGINT:8 bytes
UNSIGNED: “double”
the range
DataTypes: Integer family
TINYINT: 1 byte
SMALLINT: 2 bytes
MEDIUMINT: 3 bytes
INT
BIGINT:8 bytes
UNSIGNED: “double”
the range
Data Types: Texts
DataTypes: Texts
 Texts are strings of bytes with a charset and a collation
BINARY and VARBINARY
CHAR and VARCHAR
DataTypes: Texts
BINARY(10) == 10 bytes
CHAR(10) ==? 10 bytes
DataTypes: Texts
BINARY(10) == 10 bytes
CHAR(10) ==? 10 bytes
CHAR(10) latin1: 10 bytes
CHAR(19) utf-8: 30 bytes
DataTypes: Texts
BINARY(10) == 10 bytes
CHAR(10) ==? 10 bytes
CHAR(10) latin1: 10 bytes
CHAR(19) utf-8: 30 bytes
VARBINARY(10) == 1 to 11 bytes
VARCHAR(10) ==? 1 to 11 bytes
DataTypes: Texts
BINARY(10) == 10 bytes
CHAR(10) ==? 10 bytes
CHAR(10) latin1: 10 bytes
CHAR(10) utf-8: 30 bytes
VARBINARY(10) == 1 to 11 bytes
VARCHAR(10) ==? 1 to 11 bytes
VARCHAR(10) latin 1: 1 to 11 bytes
VARCHAR(10) utf-8: 1 to 31 bytes
So I’ll just go for VARCHAR(255) on all
text columns
 “After all… I’ll just consume the number of bytes + 1”
So I’ll just go for VARCHAR(255) on all
text columns
 “After all… I’ll just consume the number of bytes + 1”
 When we need a temporary table
 https://dev.mysql.com/doc/refman/5.6/en/memory-storage-engine.html
 “MEMORY tables use a fixed-length row-storage format. Variable-length types such
as VARCHAR are stored using a fixed length”
So I’ll just go for VARCHAR(255) on all
text columns
Congrats! All your VARCHAR(255) are now
CHAR(255) in memory
That’s 255 bytes latin-1. Or 765 bytes utf-8
O_o
Note: do the maths on VARCHAR(65535)
DataTypes: BLOBS
 TEXT == BLOB == problems
 TEXT = BLOB + charset + collation
 TEXT fields are not unlimited!
 TINYTEXT and TINYBLOB (up to 256 bytes == x chars)
 TEXT / BLOB (up to 65KB == x chars)
 MEDIUMBLOB / MEDIUMTEXT (up to 16MB == x chars)
 LONGTEXT / LONGBLOB (up to 2GB == x chars)
SELECT * IS YOUR FRIEND?
 IF a SELECT contains a BLOB/TEXT column
Temporary tables go DIRECTLY to DISK
SELECT * IS YOUR FRIEND?
 IF a SELECT contains a BLOB/TEXT column
Temporary tables go DIRECTLY to DISK
Note: “Big” columns belong on a filesystem or an object store
DataTypes: Small Sets
 ENUM
 One value out of the possibilities (‘big’, ‘small’)
 SET
 A set of possible values (‘pool’,’terrace’,’fence’)
 SETS are NOT good for finding stuff
 FIND_IN_SET is a function. No indexes 
 Default to a separate table + relation
DataTypes: Dates
 YEAR = 1 byte (range from 1901 to 2155)
 DATE = 3 bytes
 TIME = 3 bytes
 DATETIME = 8 bytes
 TIMESTAMP = 4 bytes
Timestamp is an epoch. Ideal for “things that
happen now” (sold time, renewal date, etc)
Masked Data
Masked Types
An IP Address
234.34.123.92
CHAR(15)? VARCHAR(15)?
Masked Types
INT
INSERT INTO table (col) VALUES (INET_ATON(’10.10.10.10’));
SELECT INET_NTOA(col) FROM table;
Masked Types
MD5("The quick brown fox jumps over the lazy cat")
 71bd588d5ad9b6abe87b831b45f8fa95
 CHAR(32)
BINARY(16)
Masked Types
 UUIDs
 HASH functions
 Network masks
Indexes for starters
Indexes for starters
Index the two sides of relations
PK
Index the two sides of relations
Index this guy too!
Index the two sides of relations
Index this guy too!
And this guy!
PK is (contract_id, customer_id)
(Implied uniqueness)
Index “both ways”:
(customer_id, contract_id)
InnoDB optimization: Don’t index the full (customer_id, contract_id). The
index ALREADY HAS customer_id in it’s leafs. So just index (customer_id)
N-M relation: Junction tables
Don’t operate on fields
 Because they can’t use indexes
 WHERE column = ‘x’
WHERE column > 2000
WHERE column LIKE ‘prefix%’
 WHERE column + 2000 > 2013
WHERE FIND_IN_SET(column)
WHERE CONCAT(f1,f2) = “xxxx.com”
WHERE YEAR(date) = 2015
WHERE column LIKE ‘%.com’
Polish your maths: Algebra
Doesn’t use index
WHERE column + 2000 > 2013
WHERE FIND_IN_SET(‘pool’,column)
WHERE CONCAT(f1,’.’,f2) =
“xxxx.com”
WHERE YEAR(date) = 2015
WHERE column LIKE ‘%.com’
Uses index
WHERE column > 13
?
WHERE f1 = ‘xxxx’ AND f2 = ‘com’
WHERE date BETWEEN ’01-01-2015’
and ’31-12-2015’
?
The old switcheroo…
Doesn’t use index
WHERE column + 2000 > 2013
WHERE FIND_IN_SET(‘pool’,column)
WHERE CONCAT(f1,’.’,f2) =
“xxxx.com”
WHERE YEAR(date) = 2015
WHERE column LIKE ‘%.com’
Uses index
WHERE has_pool = 1
WHERE column_rev LIKE ‘moc.%’
The old switcheroo…
Doesn’t use index
WHERE column + 2000 > 2013
WHERE FIND_IN_SET(‘pool’,column)
WHERE CONCAT(f1,’.’,f2) =
“xxxx.com”
WHERE YEAR(date) = 2015
WHERE column LIKE ‘%.com’
Uses index
WHERE has_pool = 1
UPDATE t SET has_pool =
FIND_IN_SET(‘pool’,column);
WHERE column_rev LIKE ‘moc.%’
UPDATE t SET
column_rev=REVERSE(column)
CAT IS HERE
Wrap up
Schema
Data Types
How to select them
Masked Types
Indexes
Relations
Using them
Wrap up
 Smaller is better
VS
cpsd.es/SRE-job-team

More Related Content

Viewers also liked

NRD: Nagios Result Distributor
NRD: Nagios Result DistributorNRD: Nagios Result Distributor
NRD: Nagios Result Distributor
Jose Luis Martínez
 
Make Extra Income Online
Make Extra Income OnlineMake Extra Income Online
Make Extra Income Online
webwhisker
 
My music based magazine evaluation
My music based magazine evaluationMy music based magazine evaluation
My music based magazine evaluationkamar95
 
Алексей Левинсон Пространства протеста. Московские митинги и сообщество горожан
Алексей Левинсон Пространства протеста. Московские митинги и сообщество горожанАлексей Левинсон Пространства протеста. Московские митинги и сообщество горожан
Алексей Левинсон Пространства протеста. Московские митинги и сообщество горожан
Mélusine Enfaillite
 
iPad Game Design -- Develop Liverpool Dec' 2011
iPad Game Design -- Develop Liverpool Dec' 2011iPad Game Design -- Develop Liverpool Dec' 2011
iPad Game Design -- Develop Liverpool Dec' 2011
garethjenkins
 
Questionnaire results
Questionnaire resultsQuestionnaire results
Questionnaire resultskamar95
 
Infinity
InfinityInfinity
Infinityjs229
 
Sophiie compu
Sophiie compuSophiie compu
Sophiie compu
Sophiie Mendoza
 
Estilo apa 6aed_directrizes_gerais_mja
Estilo apa 6aed_directrizes_gerais_mjaEstilo apa 6aed_directrizes_gerais_mja
Estilo apa 6aed_directrizes_gerais_mjavitorneves79
 
Success factors in football
Success factors in footballSuccess factors in football
Success factors in football
Maksim Tsurichenko
 
Kutner and olsen
Kutner and olsenKutner and olsen
Kutner and olsen
kamar95
 
My fist drafts presnetation
My fist drafts presnetationMy fist drafts presnetation
My fist drafts presnetationkamar95
 
Daniel & Ernesto's presentation
Daniel & Ernesto's presentationDaniel & Ernesto's presentation
Daniel & Ernesto's presentationErnesto Sepulveda
 

Viewers also liked (17)

NRD: Nagios Result Distributor
NRD: Nagios Result DistributorNRD: Nagios Result Distributor
NRD: Nagios Result Distributor
 
Make Extra Income Online
Make Extra Income OnlineMake Extra Income Online
Make Extra Income Online
 
Pptplan morgenjuli2011 pptbehandelcoordinatoren
Pptplan morgenjuli2011 pptbehandelcoordinatorenPptplan morgenjuli2011 pptbehandelcoordinatoren
Pptplan morgenjuli2011 pptbehandelcoordinatoren
 
My music based magazine evaluation
My music based magazine evaluationMy music based magazine evaluation
My music based magazine evaluation
 
Алексей Левинсон Пространства протеста. Московские митинги и сообщество горожан
Алексей Левинсон Пространства протеста. Московские митинги и сообщество горожанАлексей Левинсон Пространства протеста. Московские митинги и сообщество горожан
Алексей Левинсон Пространства протеста. Московские митинги и сообщество горожан
 
iPad Game Design -- Develop Liverpool Dec' 2011
iPad Game Design -- Develop Liverpool Dec' 2011iPad Game Design -- Develop Liverpool Dec' 2011
iPad Game Design -- Develop Liverpool Dec' 2011
 
Questionnaire results
Questionnaire resultsQuestionnaire results
Questionnaire results
 
Infinity
InfinityInfinity
Infinity
 
Sophiie compu
Sophiie compuSophiie compu
Sophiie compu
 
Estilo apa 6aed_directrizes_gerais_mja
Estilo apa 6aed_directrizes_gerais_mjaEstilo apa 6aed_directrizes_gerais_mja
Estilo apa 6aed_directrizes_gerais_mja
 
Polifonia_6.18
Polifonia_6.18Polifonia_6.18
Polifonia_6.18
 
Success factors in football
Success factors in footballSuccess factors in football
Success factors in football
 
Kutner and olsen
Kutner and olsenKutner and olsen
Kutner and olsen
 
introduccion
introduccionintroduccion
introduccion
 
Polifonia_6.16
Polifonia_6.16Polifonia_6.16
Polifonia_6.16
 
My fist drafts presnetation
My fist drafts presnetationMy fist drafts presnetation
My fist drafts presnetation
 
Daniel & Ernesto's presentation
Daniel & Ernesto's presentationDaniel & Ernesto's presentation
Daniel & Ernesto's presentation
 

Similar to Boosting MySQL (for starters)

Database Sizing
Database SizingDatabase Sizing
Database Sizing
Amin Chowdhury
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
MYXPLAIN
 
Amazon DynamoDB 深入探討
Amazon DynamoDB 深入探討Amazon DynamoDB 深入探討
Amazon DynamoDB 深入探討
Amazon Web Services
 
Clustered Columnstore - Deep Dive
Clustered Columnstore - Deep DiveClustered Columnstore - Deep Dive
Clustered Columnstore - Deep Dive
Niko Neugebauer
 
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
 
Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)
Itamar Haber
 
Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"
Lviv Startup Club
 
Sql and mysql database concepts
Sql and mysql database conceptsSql and mysql database concepts
Sql and mysql database conceptsSelamawit Feleke
 
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
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++
Mike Acton
 
Lesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squencesLesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squences
Lexume1
 
Your backend architecture is what matters slideshare
Your backend architecture is what matters slideshareYour backend architecture is what matters slideshare
Your backend architecture is what matters slideshare
Colin Charles
 
Deep Dive on Amazon DynamoDB
Deep Dive on Amazon DynamoDBDeep Dive on Amazon DynamoDB
Deep Dive on Amazon DynamoDB
Amazon Web Services
 
Deep Dive: Amazon DynamoDB
Deep Dive: Amazon DynamoDBDeep Dive: Amazon DynamoDB
Deep Dive: Amazon DynamoDB
Amazon Web Services
 
SQL cheat sheet.pdf
SQL cheat sheet.pdfSQL cheat sheet.pdf
SQL cheat sheet.pdf
NiravPanchal50
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
Prof. Wim Van Criekinge
 
What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0
MariaDB plc
 
MySQL innoDB split and merge pages
MySQL innoDB split and merge pagesMySQL innoDB split and merge pages
MySQL innoDB split and merge pages
Marco Tusa
 
Deep Dive - DynamoDB
Deep Dive - DynamoDBDeep Dive - DynamoDB
Deep Dive - DynamoDB
Amazon Web Services
 
Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017
Bob Ward
 

Similar to Boosting MySQL (for starters) (20)

Database Sizing
Database SizingDatabase Sizing
Database Sizing
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
 
Amazon DynamoDB 深入探討
Amazon DynamoDB 深入探討Amazon DynamoDB 深入探討
Amazon DynamoDB 深入探討
 
Clustered Columnstore - Deep Dive
Clustered Columnstore - Deep DiveClustered Columnstore - Deep Dive
Clustered Columnstore - Deep Dive
 
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.
 
Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)
 
Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"Maryna Popova "Deep dive AWS Redshift"
Maryna Popova "Deep dive AWS Redshift"
 
Sql and mysql database concepts
Sql and mysql database conceptsSql and mysql database concepts
Sql and mysql database concepts
 
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
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++
 
Lesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squencesLesson4.2 u4 l1 binary squences
Lesson4.2 u4 l1 binary squences
 
Your backend architecture is what matters slideshare
Your backend architecture is what matters slideshareYour backend architecture is what matters slideshare
Your backend architecture is what matters slideshare
 
Deep Dive on Amazon DynamoDB
Deep Dive on Amazon DynamoDBDeep Dive on Amazon DynamoDB
Deep Dive on Amazon DynamoDB
 
Deep Dive: Amazon DynamoDB
Deep Dive: Amazon DynamoDBDeep Dive: Amazon DynamoDB
Deep Dive: Amazon DynamoDB
 
SQL cheat sheet.pdf
SQL cheat sheet.pdfSQL cheat sheet.pdf
SQL cheat sheet.pdf
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
 
What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0What's new in MariaDB TX 3.0
What's new in MariaDB TX 3.0
 
MySQL innoDB split and merge pages
MySQL innoDB split and merge pagesMySQL innoDB split and merge pages
MySQL innoDB split and merge pages
 
Deep Dive - DynamoDB
Deep Dive - DynamoDBDeep Dive - DynamoDB
Deep Dive - DynamoDB
 
Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017Inside sql server in memory oltp sql sat nyc 2017
Inside sql server in memory oltp sql sat nyc 2017
 

More from Jose Luis Martínez

Being cloudy with perl
Being cloudy with perlBeing cloudy with perl
Being cloudy with perl
Jose Luis Martínez
 
Modern Perl toolchain (help building microservices)
Modern Perl toolchain (help building microservices)Modern Perl toolchain (help building microservices)
Modern Perl toolchain (help building microservices)
Jose Luis Martínez
 
Escribir plugins para Nagios en Perl
Escribir plugins para Nagios en PerlEscribir plugins para Nagios en Perl
Escribir plugins para Nagios en Perl
Jose Luis Martínez
 
Perl and AWS
Perl and AWSPerl and AWS
Perl and AWS
Jose Luis Martínez
 
Writing nagios plugins in perl
Writing nagios plugins in perlWriting nagios plugins in perl
Writing nagios plugins in perl
Jose Luis Martínez
 
Ficheros y directorios
Ficheros y directoriosFicheros y directorios
Ficheros y directorios
Jose Luis Martínez
 
DBIx::Class
DBIx::ClassDBIx::Class
DBIx::Class
Jose Luis Martínez
 
DBI
DBIDBI
The modern perl toolchain
The modern perl toolchainThe modern perl toolchain
The modern perl toolchain
Jose Luis Martínez
 
Introducción a las Expresiones Regulares
Introducción a las Expresiones RegularesIntroducción a las Expresiones Regulares
Introducción a las Expresiones Regulares
Jose Luis Martínez
 

More from Jose Luis Martínez (10)

Being cloudy with perl
Being cloudy with perlBeing cloudy with perl
Being cloudy with perl
 
Modern Perl toolchain (help building microservices)
Modern Perl toolchain (help building microservices)Modern Perl toolchain (help building microservices)
Modern Perl toolchain (help building microservices)
 
Escribir plugins para Nagios en Perl
Escribir plugins para Nagios en PerlEscribir plugins para Nagios en Perl
Escribir plugins para Nagios en Perl
 
Perl and AWS
Perl and AWSPerl and AWS
Perl and AWS
 
Writing nagios plugins in perl
Writing nagios plugins in perlWriting nagios plugins in perl
Writing nagios plugins in perl
 
Ficheros y directorios
Ficheros y directoriosFicheros y directorios
Ficheros y directorios
 
DBIx::Class
DBIx::ClassDBIx::Class
DBIx::Class
 
DBI
DBIDBI
DBI
 
The modern perl toolchain
The modern perl toolchainThe modern perl toolchain
The modern perl toolchain
 
Introducción a las Expresiones Regulares
Introducción a las Expresiones RegularesIntroducción a las Expresiones Regulares
Introducción a las Expresiones Regulares
 

Recently uploaded

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 

Recently uploaded (20)

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 

Boosting MySQL (for starters)

  • 1.
  • 2. This is not a DB optimization talk It’s a talk about how doing things in a specific way leads to getting way better results by default
  • 3. “Preoptimization”  Don’t denormalize by default  Maintenance is King (by default)  1 Thread Performance != Parallel Performance MyISAM vs InnoDB Fast != Scalable
  • 5. I’ll just tune MySQL Parameters…  Will get you out of SOME trouble  But not a good “default” solution, specially if the base is flawed  Please do tune MySQLs defaults  Not the theme for today 
  • 6.
  • 7. Start with your Schema  Your schema is probably the root cause of your “My DB doesn’t scale” problems  The solution is not “have a loose/no schema”  How to fake a DB Design (Curtis Ovid Poe)  https://www.youtube.com/watch?v=y1tcbhWLiUM
  • 8. Data types: how (not) to bloat your DB  Selecting data types with a bit of care is very productive  It makes more data fit in less space  Optimizes use of InnoDB Buffer Pool, MyISAM Key Buffer, Join Buffer, Sort Buffer, Smaller indexes
  • 9. Your new best friend  http://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html  http://dev.mysql.com/doc/refman/5.5/en/storage-requirements.html  …
  • 10. DataTypes: NULL vs NOT NULL  I don’t care about the NULL debate  Saves Space  Gives the DB hints  Use NULLs wisely
  • 12. DataTypes: Integers INT(1) == INT(10) == 4 bytes That’s it!
  • 13. DataTypes: Integer family TINYINT: 1 byte SMALLINT: 2 bytes MEDIUMINT: 3 bytes INT BIGINT:8 bytes UNSIGNED: “double” the range
  • 14. INTS and surrogate keys  It’s good to have a surrogate key  Just unique index the unique column  Why?  InnoDB stores the PK on the leafs of all indexes  Indexes bloat if they’re “big keys”
  • 15. Id columns for your tables  INT by default for “things that can get big”  Smaller INTs for smaller sets  Do you really need a BIGINT?  The magnitude comparison trick:  Epochs in Unix have 4 bytes  The epoch has been counting since 1970. Will run out in 2038  An INT can identify ONE THING HAPPENING EVERY SECOND for 68 YEARS!  Do you STILL need a BIGINT?
  • 16. DataTypes: Integer family TINYINT: 1 byte SMALLINT: 2 bytes MEDIUMINT: 3 bytes INT BIGINT:8 bytes UNSIGNED: “double” the range
  • 17. DataTypes: Integer family TINYINT: 1 byte SMALLINT: 2 bytes MEDIUMINT: 3 bytes INT BIGINT:8 bytes UNSIGNED: “double” the range
  • 19. DataTypes: Texts  Texts are strings of bytes with a charset and a collation BINARY and VARBINARY CHAR and VARCHAR
  • 20. DataTypes: Texts BINARY(10) == 10 bytes CHAR(10) ==? 10 bytes
  • 21. DataTypes: Texts BINARY(10) == 10 bytes CHAR(10) ==? 10 bytes CHAR(10) latin1: 10 bytes CHAR(19) utf-8: 30 bytes
  • 22. DataTypes: Texts BINARY(10) == 10 bytes CHAR(10) ==? 10 bytes CHAR(10) latin1: 10 bytes CHAR(19) utf-8: 30 bytes VARBINARY(10) == 1 to 11 bytes VARCHAR(10) ==? 1 to 11 bytes
  • 23. DataTypes: Texts BINARY(10) == 10 bytes CHAR(10) ==? 10 bytes CHAR(10) latin1: 10 bytes CHAR(10) utf-8: 30 bytes VARBINARY(10) == 1 to 11 bytes VARCHAR(10) ==? 1 to 11 bytes VARCHAR(10) latin 1: 1 to 11 bytes VARCHAR(10) utf-8: 1 to 31 bytes
  • 24. So I’ll just go for VARCHAR(255) on all text columns  “After all… I’ll just consume the number of bytes + 1”
  • 25. So I’ll just go for VARCHAR(255) on all text columns  “After all… I’ll just consume the number of bytes + 1”  When we need a temporary table  https://dev.mysql.com/doc/refman/5.6/en/memory-storage-engine.html  “MEMORY tables use a fixed-length row-storage format. Variable-length types such as VARCHAR are stored using a fixed length”
  • 26.
  • 27. So I’ll just go for VARCHAR(255) on all text columns Congrats! All your VARCHAR(255) are now CHAR(255) in memory That’s 255 bytes latin-1. Or 765 bytes utf-8 O_o Note: do the maths on VARCHAR(65535)
  • 28. DataTypes: BLOBS  TEXT == BLOB == problems  TEXT = BLOB + charset + collation  TEXT fields are not unlimited!  TINYTEXT and TINYBLOB (up to 256 bytes == x chars)  TEXT / BLOB (up to 65KB == x chars)  MEDIUMBLOB / MEDIUMTEXT (up to 16MB == x chars)  LONGTEXT / LONGBLOB (up to 2GB == x chars)
  • 29. SELECT * IS YOUR FRIEND?  IF a SELECT contains a BLOB/TEXT column Temporary tables go DIRECTLY to DISK
  • 30.
  • 31. SELECT * IS YOUR FRIEND?  IF a SELECT contains a BLOB/TEXT column Temporary tables go DIRECTLY to DISK Note: “Big” columns belong on a filesystem or an object store
  • 32. DataTypes: Small Sets  ENUM  One value out of the possibilities (‘big’, ‘small’)  SET  A set of possible values (‘pool’,’terrace’,’fence’)  SETS are NOT good for finding stuff  FIND_IN_SET is a function. No indexes   Default to a separate table + relation
  • 33. DataTypes: Dates  YEAR = 1 byte (range from 1901 to 2155)  DATE = 3 bytes  TIME = 3 bytes  DATETIME = 8 bytes  TIMESTAMP = 4 bytes Timestamp is an epoch. Ideal for “things that happen now” (sold time, renewal date, etc)
  • 35. Masked Types An IP Address 234.34.123.92 CHAR(15)? VARCHAR(15)?
  • 37. INT
  • 38. INSERT INTO table (col) VALUES (INET_ATON(’10.10.10.10’)); SELECT INET_NTOA(col) FROM table;
  • 39. Masked Types MD5("The quick brown fox jumps over the lazy cat")  71bd588d5ad9b6abe87b831b45f8fa95  CHAR(32)
  • 41. Masked Types  UUIDs  HASH functions  Network masks
  • 44. Index the two sides of relations PK
  • 45. Index the two sides of relations Index this guy too!
  • 46. Index the two sides of relations Index this guy too! And this guy!
  • 47.
  • 48.
  • 49. PK is (contract_id, customer_id) (Implied uniqueness) Index “both ways”: (customer_id, contract_id) InnoDB optimization: Don’t index the full (customer_id, contract_id). The index ALREADY HAS customer_id in it’s leafs. So just index (customer_id) N-M relation: Junction tables
  • 50.
  • 51. Don’t operate on fields  Because they can’t use indexes  WHERE column = ‘x’ WHERE column > 2000 WHERE column LIKE ‘prefix%’  WHERE column + 2000 > 2013 WHERE FIND_IN_SET(column) WHERE CONCAT(f1,f2) = “xxxx.com” WHERE YEAR(date) = 2015 WHERE column LIKE ‘%.com’
  • 52. Polish your maths: Algebra Doesn’t use index WHERE column + 2000 > 2013 WHERE FIND_IN_SET(‘pool’,column) WHERE CONCAT(f1,’.’,f2) = “xxxx.com” WHERE YEAR(date) = 2015 WHERE column LIKE ‘%.com’ Uses index WHERE column > 13 ? WHERE f1 = ‘xxxx’ AND f2 = ‘com’ WHERE date BETWEEN ’01-01-2015’ and ’31-12-2015’ ?
  • 53. The old switcheroo… Doesn’t use index WHERE column + 2000 > 2013 WHERE FIND_IN_SET(‘pool’,column) WHERE CONCAT(f1,’.’,f2) = “xxxx.com” WHERE YEAR(date) = 2015 WHERE column LIKE ‘%.com’ Uses index WHERE has_pool = 1 WHERE column_rev LIKE ‘moc.%’
  • 54. The old switcheroo… Doesn’t use index WHERE column + 2000 > 2013 WHERE FIND_IN_SET(‘pool’,column) WHERE CONCAT(f1,’.’,f2) = “xxxx.com” WHERE YEAR(date) = 2015 WHERE column LIKE ‘%.com’ Uses index WHERE has_pool = 1 UPDATE t SET has_pool = FIND_IN_SET(‘pool’,column); WHERE column_rev LIKE ‘moc.%’ UPDATE t SET column_rev=REVERSE(column)
  • 55.
  • 57. Wrap up Schema Data Types How to select them Masked Types Indexes Relations Using them
  • 58. Wrap up  Smaller is better VS
  • 59.