SlideShare a Scribd company logo
Practical MySQL Practical MySQL 
Performance Performance 
OptimizationOptimization
Marian HackMan MarinovMarian HackMan Marinov
<mm@siteground.com><mm@siteground.com>
Chief System ArchitectChief System Architect
SiteGroundSiteGround
Who am I?Who am I?Who am I?Who am I?
❖ Chief System Architect of Siteground.com
❖ Sysadmin since 1996
❖ Organizer of OpenFest, BG Perl Workshops,
LUG-BG and similar :)
❖ Teaching Network Security and Linux System
Administration at Sofia University
❖ MySQL 5.7
❖ Or its equivalent from MariaDB or Percona
Requirements...Requirements...
❖ Why am I making this presentation?
❖ Where is the information coming from?
MySQL Optimization Documentation
❖ I have skipped the SQL examples as they
made the presentation too big ☹
DISCLAMERDISCLAMER
❖ Manual monitoring
❖ Using Monyog
MonitoringMonitoring
How MySQL uses indexesHow MySQL uses indexes
❖ What type of indexes are there?
How MySQL uses indexesHow MySQL uses indexes
❖ What type of indexes are there?
 B-Tree
 Hash
FULLTEXT
Spatial
❖ What type of indexes are there?
❖ Which is used for what ?
 hash
  used for = or <=>
  can not be used in ORDER BY
  only whole keys can be used for search
How MySQL uses indexesHow MySQL uses indexes
❖ What type of indexes are there?
❖ Which is used for what ?
 hash
How MySQL uses indexesHow MySQL uses indexes
❖ What type of indexes are there?
❖ Which is used for what ?
 b-tree
  can be used for =, >, >=, <, <=, or
BETWEEN comparisons
  can be used with LIKE, only if it doesn't start
with %
  can not be used in the form
column1 LIKE column2
  can limit the size of the index (prefix)
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
when the index does not cover all the
columns in the where clause
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
when the index does not cover all the
columns in the where clause
when the types don't match: integer used
for string index(column)
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
when the index does not cover all the
columns in the where clause
when the types don't match: integer used
for string index(column)
when != is used
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
when the index does not cover all the
columns in the where clause
when the types don't match: integer used
for string index(column)
when != is used
when you have a multicolumn index but
the column you are referencing is not the
first one and you don't have a separate index
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
 when you have a Btree index but you use
distinct on the column
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
 when you have a Btree index but you use
distinct on the column
 when you have a single column indexes
but you are actually referencing multiple
columns in your WHERE/JOIN
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
 when you have a Btree index but you use
distinct on the column
 when you have a single column indexes
but you are actually referencing multiple
columns in your WHERE/JOIN
 when LIKE is used with % in the begining
e.g. LIKE '%name'
How MySQL uses indexesHow MySQL uses indexes
❖ When is index NOT used
 when you have a Btree index but you use
distinct on the column
 when you have a single column indexes
but you are actually referencing multiple
columns in your WHERE/JOIN
 when LIKE is used with % in the begining
e.g. LIKE '%name'
 order of columns in indexes MATTERS
How MySQL uses indexesHow MySQL uses indexes
1410864 rows
1 row in set (10.46 sec)
1 row in set (8.33 sec)
1 row in set (6.66 sec)
Index hintsIndex hints
❖ IGNORE INDEX
Index hintsIndex hints
❖ IGNORE INDEX
USE INDEX❖
Index hintsIndex hints
❖ IGNORE INDEX
❖ USE INDEX
❖ FORCE INDEX
Index hintsIndex hints
❖ "Waiting for query cache lock"
Abusing query cacheAbusing query cache
❖ "Waiting for query cache lock"
❖ SELECT *,NOW() FROM TABLE
Abusing query cacheAbusing query cache
❖ "Waiting for query cache lock"
❖ SELECT *,NOW() FROM TABLE
❖ SQL_NO_CACHE
Abusing query cacheAbusing query cache
❖ "Waiting for query cache lock"
❖ SELECT *,NOW() FROM TABLE
❖ SQL_NO_CACHE
❖ SET SESSION query_cache_type = OFF;
Abusing query cacheAbusing query cache
❖ "Waiting for query cache lock"
❖ SELECT *,NOW() FROM TABLE
❖ SQL_NO_CACHE
❖ SET SESSION query_cache_type = OFF;
❖ Аs of MySQL 5.6.8 query cache is now
disabled by default
Abusing query cacheAbusing query cache
❖ Large result sets hammer the network...
Abusing query cacheAbusing query cache
❖ Large result sets hammer the network...
❖ For caching large results move the cache,
closer to the application by caching the results
in ProxySQL
Abusing query cacheAbusing query cache
❖ InooDB uses only a single index per query
Optimizing InnoDB queriesOptimizing InnoDB queries
❖ InooDB uses only a single index per query
❖ If an indexed column cannot contain any
NULL values, declare it as NOT NULL
Optimizing InnoDB queriesOptimizing InnoDB queries
❖ InooDB uses only a single index per query
❖ If an indexed column cannot contain any
NULL values, declare it as NOT NULL
❖ START TRANSACTION READ ONLY
Optimizing InnoDB queriesOptimizing InnoDB queries
❖ use ANALYZE TABLE or myisamchk
--analyze
Optimizing MyISAM queriesOptimizing MyISAM queries
❖ use ANALYZE TABLE or myisamchk
--analyze
❖ myisamchk --sort-index --sort-records=1
Optimizing MyISAM queriesOptimizing MyISAM queries
❖ use ANALYZE TABLE or myisamchk
--analyze
❖ myisamchk --sort-index --sort-records=1
❖ avoid complex SELECT queries on
MyISAM tables that are updated frequently
Optimizing MyISAM queriesOptimizing MyISAM queries
❖ use ANALYZE TABLE or myisamchk
--analyze
❖ myisamchk --sort-index --sort-records=1
❖ avoid complex SELECT queries on
MyISAM tables that are updated frequently
❖ MyISAM supports concurrent inserts
Optimizing MyISAM queriesOptimizing MyISAM queries
❖ use ANALYZE TABLE or myisamchk
--analyze
❖ myisamchk --sort-index --sort-records=1
❖ avoid complex SELECT queries on MyISAM
tables that are updated frequently
❖ MyISAM supports concurrent inserts
concurrent_insert = 0 (disabled)
concurrent_insert = 1 (auto)
concurrent_insert = 1 (always, even with
deleted rows)
Optimizing MyISAM queriesOptimizing MyISAM queries
❖ use ANALYZE TABLE or myisamchk
--analyze
❖ myisamchk --sort-index --sort-records=1
❖ avoid complex SELECT queries on
MyISAM tables that are updated frequently
❖ MyISAM supports concurrent inserts
❖ try to avoid VARCHAR, BLOB, and TEXT in
MyISAM tables
Optimizing MyISAM queriesOptimizing MyISAM queries
❖ use ANALYZE TABLE or myisamchk
--analyze
❖ myisamchk --sort-index --sort-records=1
❖ avoid complex SELECT queries on
MyISAM tables that are updated frequently
❖ MyISAM supports concurrent inserts
❖ try to avoid VARCHAR, BLOB, and TEXT in
MyISAM tables
❖ Don't split large(columns) tables
Optimizing MyISAM queriesOptimizing MyISAM queries
❖ use ANALYZE TABLE or myisamchk
--analyze
❖ myisamchk --sort-index --sort-records=1
❖ avoid complex SELECT queries on MyISAM
tables that are updated frequently
❖ MyISAM supports concurrent inserts
❖ try to avoid VARCHAR, BLOB, and TEXT in
MyISAM tables
❖ Don't split large(columns) tables
❖ use DELAY_KEY_WRITE=1
Optimizing MyISAM queriesOptimizing MyISAM queries
mysql> SELECT @@optimizer_switchG
*************************** 1. row ***************************
@@optimizer_switch: index_merge=on,index_merge_union=on,
index_merge_sort_union=on,
index_merge_intersection=on,
engine_condition_pushdown=on,
index_condition_pushdown=on,
mrr=on,mrr_cost_based=on,
block_nested_loop=on,batched_key_access=off,
materialization=on,semijoin=on,loosescan=on,
firstmatch=on,duplicateweedout=on,
subquery_materialization_cost_based=on,
use_index_extensions=on,
condition_fanout_filter=on,derived_merge=on
Controlling the OptimizerControlling the Optimizer
MySQL 5.7
only
❖ Merging multiple index scans on a SINGLE
table
index_merge=off (default on)
index_merge_union=on
index_merge_sort_union=on
index_merge_intersection=on
low selectivity indexes are very slow with
intersect
Controlling the OptimizerControlling the Optimizer
❖ Selecting different merge algorithms per
table (only for 5.7)
Batched Key Access join /*+ BKA(t1) */
Block Nested-Loop join /*+ BNL(t1, t2) */
Controlling the OptimizerControlling the Optimizer
❖ Optimizing subqueries
Controlling the OptimizerControlling the Optimizer
❖ Optimizing subqueries
Materialization strategy
Controlling the OptimizerControlling the Optimizer
❖ Optimizing subqueries
Materialization strategy
Exists strategy
Controlling the OptimizerControlling the Optimizer
❖ Optimizing subqueries
Materialization strategy
Exists strategy
  (usually preferred when the sub queries
can produce NULL results)
Controlling the OptimizerControlling the Optimizer
❖ Optimizing subqueries
Materialization strategy
Exists strategy
  (usually preferred when the sub queries
can produce NULL results)
subquery_materialization_cost_based
Controlling the OptimizerControlling the Optimizer
❖ use SQL_SMALL_RESUL
❖ If all columns in the index are numeric MySQL will
read only the INDEX
❖ Avoid large amounts of values in IN statements
 values in an IN() list count as a predicates
combined with OR
use eq_range_index_dive_limit to control the index
dives in range comparisons(IN) 5.7
range optimization can't be used with NOT IN()
 /*+ NO_RANGE_OPTIMIZATION(t4 PRIMARY) */
❖ Index Condition Pushdown 5.7
General optimizationGeneral optimization
❖ Avoid full table scans
--max-seeks-for-key=1000
SSD or NVMe
General optimizationGeneral optimization
# Turn tracing on (it's off by default):
SET optimizer_trace="enabled=on";
SELECT ...; # your query here
SELECT * FROM
INFORMATION_SCHEMA.OPTIMIZER_TRACE;
# possibly more queries...
# When done with tracing, disable it:
SET optimizer_trace="enabled=off";
Using the tracerUsing the tracer
"filesort_summary": {
"rows": 100,
"examined_rows": 100,
"number_of_tmp_files": 0,
"sort_buffer_size": 25192,
"sort_mode": "<sort_key,
packed_additional_fields>"
}
Using the tracerUsing the tracer
❖ Consider using INSERT with multiple VALUES
lists
❖ Updates on tables with many indexes may
become slow
❖ Updates with selects in them may block
because of query cache lock contention
❖ Deletes also suffer from the above problems
Changing dataChanging data
❖ Splitting less frequently used columns from
large(column) tables using foreign keys
Optimizing architectureOptimizing architecture
ACID RainACID Rain
Concurrency-Related Attacks onConcurrency-Related Attacks on
Database-Backed Web ApplicationsDatabase-Backed Web Applications
ACID RainACID Rain
Concurrency-Related Attacks onConcurrency-Related Attacks on
Database-Backed Web ApplicationsDatabase-Backed Web Applications
Marian HackMan Marinov
<mm@siteground.com>
THANK YOUTHANK YOUTHANK YOUTHANK YOU
Marian HackMan Marinov
<mm@siteground.com>

More Related Content

ODP
Protecting your data when entering the US
PDF
Introduction to python
ODP
Home assistant
ODP
How to setup your linux server
PDF
Make your internship "worth it"
PPTX
LUG-BG - Kostadin Slavkov - PostgreSQL 10
PDF
How penetration testing techniques can help you improve your qa skills
PDF
Comparison of foss distributed storage
Protecting your data when entering the US
Introduction to python
Home assistant
How to setup your linux server
Make your internship "worth it"
LUG-BG - Kostadin Slavkov - PostgreSQL 10
How penetration testing techniques can help you improve your qa skills
Comparison of foss distributed storage

Viewers also liked (12)

PDF
Io t introduction to electronics
ODP
Securing the network for VMs or Containers
ODP
Computer vision for your projects
PDF
Lxd the proper way of runing containers
PDF
Moving your router inside container
PDF
Gluster.community.day.2013
PDF
LUG-BG 2017 - Rangel Ivanov - Spread some butter - BTRFS
PDF
4 Sessions
PDF
Protecting your home and office in the era of IoT
PDF
Why we are migrating to Slackware
PDF
Performance comparison of Distributed File Systems on 1Gbit networks
ODP
nftables - the evolution of Linux Firewall
Io t introduction to electronics
Securing the network for VMs or Containers
Computer vision for your projects
Lxd the proper way of runing containers
Moving your router inside container
Gluster.community.day.2013
LUG-BG 2017 - Rangel Ivanov - Spread some butter - BTRFS
4 Sessions
Protecting your home and office in the era of IoT
Why we are migrating to Slackware
Performance comparison of Distributed File Systems on 1Gbit networks
nftables - the evolution of Linux Firewall
Ad

Similar to Practical my sql performance optimization (20)

PDF
Mysql query optimization
PPTX
Optimizing MySQL queries
PPTX
Query Optimization in SQL Server
PDF
MySQL Query And Index Tuning
PPTX
02 database oprimization - improving sql performance - ent-db
PPTX
Optimizing MySQL Queries
PPTX
How oracle query works (The SQL Optimizers)
PPTX
Sql performance tuning
PDF
Basics on SQL queries
ODP
Mysql For Developers
PDF
Indexes overview
PPTX
MySQL JOIN & UNION
PDF
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
PPTX
Search and analyze your data with elasticsearch
PPTX
MySQL: Know more about open Source Database
PDF
Optimized cluster index generation
PDF
Sphinx new
PDF
Sql db optimization
PDF
Building better SQL Server Databases
PDF
Introduction to MySQL Query Tuning for Dev[Op]s
Mysql query optimization
Optimizing MySQL queries
Query Optimization in SQL Server
MySQL Query And Index Tuning
02 database oprimization - improving sql performance - ent-db
Optimizing MySQL Queries
How oracle query works (The SQL Optimizers)
Sql performance tuning
Basics on SQL queries
Mysql For Developers
Indexes overview
MySQL JOIN & UNION
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Search and analyze your data with elasticsearch
MySQL: Know more about open Source Database
Optimized cluster index generation
Sphinx new
Sql db optimization
Building better SQL Server Databases
Introduction to MySQL Query Tuning for Dev[Op]s
Ad

More from Marian Marinov (20)

PDF
How to start and then move forward in IT
PDF
Thinking about highly-available systems and their setup
PDF
Understanding your memory usage under Linux
PDF
How to implement PassKeys in your application
PDF
Dev.bg DevOps March 2024 Monitoring & Logging
PDF
Basic presentation of cryptography mechanisms
PDF
Microservices: Benefits, drawbacks and are they for me?
PDF
Introduction and replication to DragonflyDB
PDF
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQ
PDF
How to successfully migrate to DevOps .pdf
PDF
How to survive in the work from home era
PDF
Managing sysadmins
PDF
Improve your storage with bcachefs
PDF
Control your service resources with systemd
PDF
Comparison of-foss-distributed-storage
PDF
Защо и как да обогатяваме знанията си?
PDF
Securing your MySQL server
PDF
Sysadmin vs. dev ops
PDF
DoS and DDoS mitigations with eBPF, XDP and DPDK
PDF
Challenges with high density networks
How to start and then move forward in IT
Thinking about highly-available systems and their setup
Understanding your memory usage under Linux
How to implement PassKeys in your application
Dev.bg DevOps March 2024 Monitoring & Logging
Basic presentation of cryptography mechanisms
Microservices: Benefits, drawbacks and are they for me?
Introduction and replication to DragonflyDB
Message Queuing - Gearman, Mosquitto, Kafka and RabbitMQ
How to successfully migrate to DevOps .pdf
How to survive in the work from home era
Managing sysadmins
Improve your storage with bcachefs
Control your service resources with systemd
Comparison of-foss-distributed-storage
Защо и как да обогатяваме знанията си?
Securing your MySQL server
Sysadmin vs. dev ops
DoS and DDoS mitigations with eBPF, XDP and DPDK
Challenges with high density networks

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
web development for engineering and engineering
PPTX
Welding lecture in detail for understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Sustainable Sites - Green Building Construction
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Mechanical Engineering MATERIALS Selection
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
UNIT 4 Total Quality Management .pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Geodesy 1.pptx...............................................
additive manufacturing of ss316l using mig welding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
web development for engineering and engineering
Welding lecture in detail for understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Sustainable Sites - Green Building Construction
R24 SURVEYING LAB MANUAL for civil enggi
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mechanical Engineering MATERIALS Selection
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
573137875-Attendance-Management-System-original
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx

Practical my sql performance optimization

  • 2. Who am I?Who am I?Who am I?Who am I? ❖ Chief System Architect of Siteground.com ❖ Sysadmin since 1996 ❖ Organizer of OpenFest, BG Perl Workshops, LUG-BG and similar :) ❖ Teaching Network Security and Linux System Administration at Sofia University
  • 3. ❖ MySQL 5.7 ❖ Or its equivalent from MariaDB or Percona Requirements...Requirements...
  • 4. ❖ Why am I making this presentation? ❖ Where is the information coming from? MySQL Optimization Documentation ❖ I have skipped the SQL examples as they made the presentation too big ☹ DISCLAMERDISCLAMER
  • 5. ❖ Manual monitoring ❖ Using Monyog MonitoringMonitoring
  • 6. How MySQL uses indexesHow MySQL uses indexes ❖ What type of indexes are there?
  • 7. How MySQL uses indexesHow MySQL uses indexes ❖ What type of indexes are there?  B-Tree  Hash FULLTEXT Spatial
  • 8. ❖ What type of indexes are there? ❖ Which is used for what ?  hash   used for = or <=>   can not be used in ORDER BY   only whole keys can be used for search How MySQL uses indexesHow MySQL uses indexes
  • 9. ❖ What type of indexes are there? ❖ Which is used for what ?  hash How MySQL uses indexesHow MySQL uses indexes
  • 10. ❖ What type of indexes are there? ❖ Which is used for what ?  b-tree   can be used for =, >, >=, <, <=, or BETWEEN comparisons   can be used with LIKE, only if it doesn't start with %   can not be used in the form column1 LIKE column2   can limit the size of the index (prefix) How MySQL uses indexesHow MySQL uses indexes
  • 11. ❖ When is index NOT used How MySQL uses indexesHow MySQL uses indexes
  • 12. ❖ When is index NOT used when the index does not cover all the columns in the where clause How MySQL uses indexesHow MySQL uses indexes
  • 13. ❖ When is index NOT used when the index does not cover all the columns in the where clause when the types don't match: integer used for string index(column) How MySQL uses indexesHow MySQL uses indexes
  • 14. ❖ When is index NOT used when the index does not cover all the columns in the where clause when the types don't match: integer used for string index(column) when != is used How MySQL uses indexesHow MySQL uses indexes
  • 15. ❖ When is index NOT used when the index does not cover all the columns in the where clause when the types don't match: integer used for string index(column) when != is used when you have a multicolumn index but the column you are referencing is not the first one and you don't have a separate index How MySQL uses indexesHow MySQL uses indexes
  • 16. ❖ When is index NOT used  when you have a Btree index but you use distinct on the column How MySQL uses indexesHow MySQL uses indexes
  • 17. ❖ When is index NOT used  when you have a Btree index but you use distinct on the column  when you have a single column indexes but you are actually referencing multiple columns in your WHERE/JOIN How MySQL uses indexesHow MySQL uses indexes
  • 18. ❖ When is index NOT used  when you have a Btree index but you use distinct on the column  when you have a single column indexes but you are actually referencing multiple columns in your WHERE/JOIN  when LIKE is used with % in the begining e.g. LIKE '%name' How MySQL uses indexesHow MySQL uses indexes
  • 19. ❖ When is index NOT used  when you have a Btree index but you use distinct on the column  when you have a single column indexes but you are actually referencing multiple columns in your WHERE/JOIN  when LIKE is used with % in the begining e.g. LIKE '%name'  order of columns in indexes MATTERS How MySQL uses indexesHow MySQL uses indexes
  • 20. 1410864 rows 1 row in set (10.46 sec) 1 row in set (8.33 sec) 1 row in set (6.66 sec) Index hintsIndex hints
  • 21. ❖ IGNORE INDEX Index hintsIndex hints
  • 22. ❖ IGNORE INDEX USE INDEX❖ Index hintsIndex hints
  • 23. ❖ IGNORE INDEX ❖ USE INDEX ❖ FORCE INDEX Index hintsIndex hints
  • 24. ❖ "Waiting for query cache lock" Abusing query cacheAbusing query cache
  • 25. ❖ "Waiting for query cache lock" ❖ SELECT *,NOW() FROM TABLE Abusing query cacheAbusing query cache
  • 26. ❖ "Waiting for query cache lock" ❖ SELECT *,NOW() FROM TABLE ❖ SQL_NO_CACHE Abusing query cacheAbusing query cache
  • 27. ❖ "Waiting for query cache lock" ❖ SELECT *,NOW() FROM TABLE ❖ SQL_NO_CACHE ❖ SET SESSION query_cache_type = OFF; Abusing query cacheAbusing query cache
  • 28. ❖ "Waiting for query cache lock" ❖ SELECT *,NOW() FROM TABLE ❖ SQL_NO_CACHE ❖ SET SESSION query_cache_type = OFF; ❖ Аs of MySQL 5.6.8 query cache is now disabled by default Abusing query cacheAbusing query cache
  • 29. ❖ Large result sets hammer the network... Abusing query cacheAbusing query cache
  • 30. ❖ Large result sets hammer the network... ❖ For caching large results move the cache, closer to the application by caching the results in ProxySQL Abusing query cacheAbusing query cache
  • 31. ❖ InooDB uses only a single index per query Optimizing InnoDB queriesOptimizing InnoDB queries
  • 32. ❖ InooDB uses only a single index per query ❖ If an indexed column cannot contain any NULL values, declare it as NOT NULL Optimizing InnoDB queriesOptimizing InnoDB queries
  • 33. ❖ InooDB uses only a single index per query ❖ If an indexed column cannot contain any NULL values, declare it as NOT NULL ❖ START TRANSACTION READ ONLY Optimizing InnoDB queriesOptimizing InnoDB queries
  • 34. ❖ use ANALYZE TABLE or myisamchk --analyze Optimizing MyISAM queriesOptimizing MyISAM queries
  • 35. ❖ use ANALYZE TABLE or myisamchk --analyze ❖ myisamchk --sort-index --sort-records=1 Optimizing MyISAM queriesOptimizing MyISAM queries
  • 36. ❖ use ANALYZE TABLE or myisamchk --analyze ❖ myisamchk --sort-index --sort-records=1 ❖ avoid complex SELECT queries on MyISAM tables that are updated frequently Optimizing MyISAM queriesOptimizing MyISAM queries
  • 37. ❖ use ANALYZE TABLE or myisamchk --analyze ❖ myisamchk --sort-index --sort-records=1 ❖ avoid complex SELECT queries on MyISAM tables that are updated frequently ❖ MyISAM supports concurrent inserts Optimizing MyISAM queriesOptimizing MyISAM queries
  • 38. ❖ use ANALYZE TABLE or myisamchk --analyze ❖ myisamchk --sort-index --sort-records=1 ❖ avoid complex SELECT queries on MyISAM tables that are updated frequently ❖ MyISAM supports concurrent inserts concurrent_insert = 0 (disabled) concurrent_insert = 1 (auto) concurrent_insert = 1 (always, even with deleted rows) Optimizing MyISAM queriesOptimizing MyISAM queries
  • 39. ❖ use ANALYZE TABLE or myisamchk --analyze ❖ myisamchk --sort-index --sort-records=1 ❖ avoid complex SELECT queries on MyISAM tables that are updated frequently ❖ MyISAM supports concurrent inserts ❖ try to avoid VARCHAR, BLOB, and TEXT in MyISAM tables Optimizing MyISAM queriesOptimizing MyISAM queries
  • 40. ❖ use ANALYZE TABLE or myisamchk --analyze ❖ myisamchk --sort-index --sort-records=1 ❖ avoid complex SELECT queries on MyISAM tables that are updated frequently ❖ MyISAM supports concurrent inserts ❖ try to avoid VARCHAR, BLOB, and TEXT in MyISAM tables ❖ Don't split large(columns) tables Optimizing MyISAM queriesOptimizing MyISAM queries
  • 41. ❖ use ANALYZE TABLE or myisamchk --analyze ❖ myisamchk --sort-index --sort-records=1 ❖ avoid complex SELECT queries on MyISAM tables that are updated frequently ❖ MyISAM supports concurrent inserts ❖ try to avoid VARCHAR, BLOB, and TEXT in MyISAM tables ❖ Don't split large(columns) tables ❖ use DELAY_KEY_WRITE=1 Optimizing MyISAM queriesOptimizing MyISAM queries
  • 42. mysql> SELECT @@optimizer_switchG *************************** 1. row *************************** @@optimizer_switch: index_merge=on,index_merge_union=on, index_merge_sort_union=on, index_merge_intersection=on, engine_condition_pushdown=on, index_condition_pushdown=on, mrr=on,mrr_cost_based=on, block_nested_loop=on,batched_key_access=off, materialization=on,semijoin=on,loosescan=on, firstmatch=on,duplicateweedout=on, subquery_materialization_cost_based=on, use_index_extensions=on, condition_fanout_filter=on,derived_merge=on Controlling the OptimizerControlling the Optimizer MySQL 5.7 only
  • 43. ❖ Merging multiple index scans on a SINGLE table index_merge=off (default on) index_merge_union=on index_merge_sort_union=on index_merge_intersection=on low selectivity indexes are very slow with intersect Controlling the OptimizerControlling the Optimizer
  • 44. ❖ Selecting different merge algorithms per table (only for 5.7) Batched Key Access join /*+ BKA(t1) */ Block Nested-Loop join /*+ BNL(t1, t2) */ Controlling the OptimizerControlling the Optimizer
  • 45. ❖ Optimizing subqueries Controlling the OptimizerControlling the Optimizer
  • 46. ❖ Optimizing subqueries Materialization strategy Controlling the OptimizerControlling the Optimizer
  • 47. ❖ Optimizing subqueries Materialization strategy Exists strategy Controlling the OptimizerControlling the Optimizer
  • 48. ❖ Optimizing subqueries Materialization strategy Exists strategy   (usually preferred when the sub queries can produce NULL results) Controlling the OptimizerControlling the Optimizer
  • 49. ❖ Optimizing subqueries Materialization strategy Exists strategy   (usually preferred when the sub queries can produce NULL results) subquery_materialization_cost_based Controlling the OptimizerControlling the Optimizer
  • 50. ❖ use SQL_SMALL_RESUL ❖ If all columns in the index are numeric MySQL will read only the INDEX ❖ Avoid large amounts of values in IN statements  values in an IN() list count as a predicates combined with OR use eq_range_index_dive_limit to control the index dives in range comparisons(IN) 5.7 range optimization can't be used with NOT IN()  /*+ NO_RANGE_OPTIMIZATION(t4 PRIMARY) */ ❖ Index Condition Pushdown 5.7 General optimizationGeneral optimization
  • 51. ❖ Avoid full table scans --max-seeks-for-key=1000 SSD or NVMe General optimizationGeneral optimization
  • 52. # Turn tracing on (it's off by default): SET optimizer_trace="enabled=on"; SELECT ...; # your query here SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE; # possibly more queries... # When done with tracing, disable it: SET optimizer_trace="enabled=off"; Using the tracerUsing the tracer
  • 53. "filesort_summary": { "rows": 100, "examined_rows": 100, "number_of_tmp_files": 0, "sort_buffer_size": 25192, "sort_mode": "<sort_key, packed_additional_fields>" } Using the tracerUsing the tracer
  • 54. ❖ Consider using INSERT with multiple VALUES lists ❖ Updates on tables with many indexes may become slow ❖ Updates with selects in them may block because of query cache lock contention ❖ Deletes also suffer from the above problems Changing dataChanging data
  • 55. ❖ Splitting less frequently used columns from large(column) tables using foreign keys Optimizing architectureOptimizing architecture
  • 56. ACID RainACID Rain Concurrency-Related Attacks onConcurrency-Related Attacks on Database-Backed Web ApplicationsDatabase-Backed Web Applications ACID RainACID Rain Concurrency-Related Attacks onConcurrency-Related Attacks on Database-Backed Web ApplicationsDatabase-Backed Web Applications Marian HackMan Marinov <[email protected]>
  • 57. THANK YOUTHANK YOUTHANK YOUTHANK YOU Marian HackMan Marinov <[email protected]>