SlideShare a Scribd company logo
www.fromdual.com 
1 / 29 
Need for Speed: 
MySQL Indexing 
Percona Live 2013, 
November 11 – 12, London 
Oli Sennhauser 
Senior MySQL Consultant at FromDual GmbH 
oli.sennhauser@fromdual.com
www.fromdual.com 
2 / 29 
About FromDual GmbH 
● FromDual provides neutral and 
independent: 
● Consulting for MySQL, Percona Server, MariaDB 
● Support for all MySQL and Galera Cluster 
● Remote-DBA Services 
● MySQL Training 
● Oracle Silver Partner (OPN) 
www.fromdual.com
www.fromdual.com 
3 / 29 
Our customers
www.fromdual.com 
4 / 29 
MySQL and Indexing 
● MySQL documentation says: 
The best way to improve the performance of SELECT operations is 
to create indexes on one or more of the columns that are tested in 
the query. 
● Great! But: 
Unnecessary indexes waste space and waste time to determine 
which indexes to use. You must find the right balance to achieve 
fast queries using the optimal set of indexes. 
● ... hmm so we have to think a bit... :-(
www.fromdual.com 
5 / 29 
What is an Index? 
● Adams, Douglas: 
The Hitchhiker's 
Guide to the 
Galaxy? 
● Sennhauser, Oli, 
Uster?
www.fromdual.com 
6 / 29 
What is an Index technically?
www.fromdual.com 
7 / 29 
MySQL uses indexes: 
● To enforce uniqueness (PRIMARY KEY, UNIQUE 
KEY) 
● To fast access and filter rows (WHERE) 
● To perform joins fast (JOIN) 
● To find MIN() and MAX() values 
● For sorting and grouping (ORDER BY, GROUP BY) 
● To avoid joins by using covering indexes 
● To enforce FOREIGN KEY Constraints (FOREIGN 
KEY)
www.fromdual.com 
8 / 29 
WHERE clause 1 
SELECT * 
FROM customers 
WHERE name = 'No ClSuHeO Wo fC RMEyASTQEL TLALBCL'E; customersG 
CREATE TABLE `customers` ( 
`customer_id` smallint(5) unsigned 
, `name` varchar(64) DEFAULT NULL 
, PRIMARY KEY (`customer_id`) 
EXPLAIN ) 
SELECT * 
FROM customers 
WHERE name = 'No Clue of MySQL LLC'; 
+-----------+------+---------------+------+-------+-------------+ 
| table | type | possible_keys | key | rows | Extra | 
+-----------+------+---------------+------+-------+-------------+ 
| customers | ALL | NULL | NULL | 31978 | Using where | 
+-----------+------+---------------+------+-------+-------------+
www.fromdual.com 
9 / 29 
How to create and Index? 
ALTER TABLE … 
● ADD PRIMARY KEY (id); 
● ADD UNIQUE KEY (uuid); 
● ADD FOREIGN KEY (customer_id) 
REFERENCES customers (customer_id); 
● ADD INDEX (last_name, first_name); 
● ADD INDEX pre_ind (hash(8)); 
● ADD FULLTEXT INDEX (last_name, 
first_name);
www.fromdual.com 
10 / 29 
WHERE clause 2 
ALTER TABLE customers 
ADD INDEX (name); 
CREATE TABLE `customers` ( 
`customer_id` smallint(5) unsigned 
Gain: 20 ms → 5 ms 
, `name` varchar(64) DEFAULT NULL 
, PRIMARY KEY (`customer_id`) 
, KEY `name` (`name`) 
) 
+-----------+------+---------------+------+---------+-------+------+ 
| table | type | possible_keys | key | key_len | ref | rows | 
+-----------+------+---------------+------+---------+-------+------+ 
| customers | ref | name | name | 67 | const | 1 | 
+-----------+------+---------------+------+---------+-------+------+
www.fromdual.com 
11 / 29 
JOIN clause 
EXPLAIN SELECT * 
FROM customers AS c 
JOIN orders AS o ON c.customer_id = o.customer_id 
WHERE c.name = 'No Clue of MySQL LLC'; 
+-------+------+---------------+------+---------+-------+---------+ 
| table | type | possible_keys | key | key_len | ref | rows | 
+-------+------+---------------+------+---------+-------+---------+ 
| c | ref | PRIMARY,Gain: name 450 | name ms | → 67 6 ms 
| const | 1 | 
| o | ALL | NULL | NULL | NULL | NULL | 1045105 | 
+-------+------+---------------+------+---------+-------+---------+ 
ALTER TABLE orders 
ADD INDEX (customer_id); 
+-------+------+---------------+-------------+---------+---------------+------+ 
| table | type | possible_keys | key | key_len | ref | rows | 
+-------+------+---------------+-------------+---------+---------------+------+ 
| c | ref | PRIMARY,name | name | 67 | const | 1 | 
| o | ref | customer_id | customer_id | 3 | c.customer_id | 8 | 
+-------+------+---------------+-------------+---------+---------------+------+
www.fromdual.com 
12 / 29 
For sorting/grouping tables 
ORDER BY, GROUP BY 
EXPLAIN SELECT * 
FROM contacts AS c 
WHERE last_name = 'Sennhauser' 
ORDER BY last_name, first_name; 
+-------+------+-----------+------+----------------------------------------------------+ 
| table | type | key | +-------+------+-----------+------+----------------------------------------------------+ 
Gain: rows | Extra 20 ms → 7 ms 
| 
| c | ref | last_name | 1561 | Using index condition; Using where; Using filesort | 
+-------+------+-----------+------+----------------------------------------------------+ 
ALTER TABLE contacts 
ADD INDEX (last_name, first_name); 
+----------+------+-------------+------+--------------------------+ 
| table | type | key | rows | Extra | 
+----------+------+-------------+------+--------------------------+ 
| contacts | ref | last_name_2 | 1561 | Using where; Using index | 
+----------+------+-------------+------+--------------------------+
www.fromdual.com 
13 / 29 
Covering Indexes 
EXPLAIN 
SELECT customer_id, amount 
FROM orders AS o 
WHERE customer_id = 59349; 
+-------+------+-------------+------+-------+ 
| table | type | key | rows | Extra | 
+-------+------+-------------+------+-------+ 
| o | ref | customer_id So | what? 
15 | NULL | 
+-------+------+-------------+------+-------+ 
ALTER TABLE orders 
ADD INDEX (customer_id, amount); 
+-------+------+---------------+------+-------------+ 
| table | type | key | rows | Extra | 
+-------+------+---------------+------+-------------+ 
| o | ref | customer_id_2 | 15 | Using index | 
+-------+------+---------------+------+-------------+
www.fromdual.com 
14 / 29 
Benefit of Covering Indexes 
● Why are Covering Indexes beneficial
www.fromdual.com 
15 / 29 
How-to find missing indexes? 
● ER Diagram? :-( 
● Most of them rely to you business logic... 
● How-to FIND? 
● Slow Query Log 
● MySQL Variables: 
● Since v5.1 
on-line! 
+-------------------------------+----------+ 
| Variable_name | Value | 
+-------------------------------+----------+ 
| log_queries_not_using_indexes | ON | 
| long_query_time | 0.250000 | 
| min_examined_row_limit | 100 | 
| slow_query_log | ON | 
| slow_query_log_file | slow.log | 
+-------------------------------+----------+
www.fromdual.com 
16 / 29 
Indexes are not only good 
● Indexes use space (Disk, hot data in RAM!) 
● Indexes use time to maintain (CPU, RAM, I/O) 
● Optimizer needs time to determine which indexes to 
use. 
● Sometimes optimizer is completely confused and 
does wrong decisions if too many (similar) indexes 
are there. 
→ You must find the right 
balance to achieve fast 
queries using the optimal 
set of indexes.
www.fromdual.com 
17 / 29 
Smaller indexes faster queries 
● Better fit into memory (less I/O) 
● Higher data density (rows/block) 
● Less CPU cycles (to crawl through) 
● Prefixed indexes: 
ADD INDEX pre_ind (hash(8));
www.fromdual.com 
18 / 29 
Avoid indexes 
● Avoid redundant (and thus unnecessary ) indexes 
● How does it happen? 
● Developer 1: Creates a Foreign Key constraint → done 
● Developer 2: Ouu! Query is slow → Oli told me to 
create an index! → done 
● Developer 3: Ouu! Query is slow → Developer 2 is 
stupid! → Create and index → done 
● Frameworks vs. Developer 
● Upgrade process vs. Developer 
● Avoid indexes which are not used / needed
www.fromdual.com 
19 / 29 
How to find such indexes? 
SHOW CREATE TABLE ...G 
mysqldump ­­no­data 
> structure_dump.sql 
● Since MySQL 5.6: PERFORMANCE_SCHEMA 
● Percona Server / MariaDB: Userstats 
● http://fromdual.com/mysql-performance-schema-hints 
SELECT object_schema, object_name, index_name 
FROM performance_schema.table_io_waits_summary_by_index_usage 
WHERE index_name IS NOT NULL 
AND count_star = 0 
ORDER BY object_schema, object_name;
www.fromdual.com 
20 / 29 
Avoid partial redundant indexes 
● INDEX (city, last_name, first_name) 
● INDEX (city, last_name) 
● INDEX (city) 
● INDEX (last_name, city) 
??? 
● INDEX (first_name, last_name) 
!!!
www.fromdual.com 
SELECT status, COUNT(*) 
21 / 29 
Bad selectivity 
● Remove indexes with bad selectivity (~= low cardinality) 
● Candidates are: 
● status 
● gender 
● active 
● How to find if field has bad 
selectivity? 
Indexes (and Joins) are expensive!!! 
● Break even between 15% and 66% 
FROM orders 
GROUP BY status; 
+--------+--------+ 
| status | cnt | 
+--------+--------+ 
| 0 | 393216 | 
| 1 | 262144 | 
| 2 | 12 | 
| 3 | 36 | 
| 4 | 24 | 
| 5 | 4 | 
| 6 | 8 | 
+--------+--------+ 
● Lets see if the MySQL optimizer knows about it... :-)
www.fromdual.com 
SELECT status, COUNT(*) 
+--------+--------+ 
| status | cnt | 
+--------+--------+ 
| 0 | 393216 | 
| 1 | 262144 | 
| 2 | 12 | 
| 3 | 36 | 
| 4 | 24 | 
| 5 | 4 | 
| 6 | 8 | 
+--------+--------+ 
22 / 29 
Optimizer is wrong! 
SELECT * FROM orders WHERE status = 2; 
+--------+------+---------------+--------+------+ 
| table | type | possible_keys | key | rows | 
+--------+------+---------------+--------+------+ 
| orders | ref | status | status | 12 | 
+--------+------+---------------+--------+------+ 
SELECT * FROM orders WHERE status = 0; 
1.43 s 
+--------+------+---------------+--------+--------+ 
| table | type | possible_keys | key | rows | 
+--------+------+---------------+--------+--------+ 
| orders | ref | status | status | 327469 | 
+--------+------+---------------+--------+--------+ 
FROM orders 
GROUP BY status; 
SELECT * FROM orders IGNORE INDEX (STATUS) WHERE status = 0; 
0.44 s 
+--------+------+---------------+------+--------+ 
| table | type | possible_5.6.12 keys (after | key analyze | rows table) 
| 
+--------+------+---------------+------+--------+ 
| orders | ALL | NULL | NULL | 654939 | 
+--------+------+---------------+------+--------+
www.fromdual.com 
23 / 29 
InnoDB PK and SK 
● InnoDB has 
● Primary Keys and 
● Secondary Keys
www.fromdual.com 
24 / 29 
Clustered Index 
● InnoDB: Data = Leaf of Primary Key 
● We call this an Index Clustered Table (IOT) 
→ Data are sorted like PK (key is sorted)! 
→ PK influences Locality of data (physical 
location) 
● AUTO_INCREMENT ~= sorting by time! 
● Good for many things 
● where hot data = recent data 
● Bad for time series 
● Where hot data = per item data
www.fromdual.com 
25 / 29 
Example: InnoDB 
A_I ts v_id xpos ypos ... 
1 17:30 #42 x, y, ... 
2 17:30 #43 x, y, ... 
3 17:30 #44 x, y, ... 
... 
2001 17:32 #42 x, y, ... 
2002 17:32 #43 x, y, ... 
2003 17:32 #44 x, y, ... 
#42 every 2' 
over the last 3 days 
~ 2000 rows 
~ 200 kbyte 
Q1: Δ in rows? 
A1: 1 row ~ 100 byte 
Q2: Δ in bytes? 
Q3: Default InnoDB block size? 
default: 16 kbyte 
Q4: Avg. # of rows of car #42 in 1 InnoDB block? 
A2: 3 d and 720 pt/d → ~2000 pt ~ 2000 rec ~ 2000 blk 
Q5: How long will this take and why (32 Mbyte)? 
~ 2000 IOPS ~ 10s random read!!! 
S: All in RAM or strong I/O system or …? 
~ 1 
2000 trucks
www.fromdual.com 
26 / 29 
InnoDB PK safes the day! 
ts v_id xpos ypos ... 
17:30 #42 x, y, ... 
17:32 #42 x, y, ... 
17:34 #42 x, y, ... 
... #42 every 2' 
17:30 #43 x, y, ... 
17:32 #43 x, y, ... 
17:34 #43 x, y, ... 
over the last 3 days 
Q1: Avg. # of rows of car #42 in 1 InnoDB block? 
A1: 3 d and 720 pt/d → ~2000 pt ~ 2000 rec ~ 20 blk 
Q2: How long will this take and why (320 kbyte)? 
~ 1-2 IOPS ~ 10-20 ms sequential read! 
S: Wow f=50 faster! Any drawbacks? 
~ 120 
2000 trucks 
17:30 #44 x, y, ... ...
www.fromdual.com 
27 / 29 
Index hints 
● MySQL optimizer is sometimes wrong! 
● We have to help (= hint) him... 
● Index hints are: 
● USE INDEX (ind1, ind2) 
● Only consider these indexes 
● FORCE INDEX (ind3) 
● Use this index without considering anything else 
● IGNORE INDEX (ind1, ind3) 
● Do NOT consider these indexes but everything else 
● Hints should be used only as a last resort
www.fromdual.com 
28 / 29 
MySQL Variables 
● MySQL variables influencing index use 
● MyISAM: key_buffer_size 
● InnoDB: innodb_buffer_pool_size / 
innodb_buffer_pool_instances 
● InnoDB Change Buffer 
● innodb_change_buffer_max_size 
● innodb_change_buffering 
● Adaptive Hash Index (AHI) 
● MySQL 5.6.3 / 5.5.14 index length 767 → 3072 bytes 
● innodb_large_prefix
www.fromdual.com 
29 / 29 
Q & A 
Questions ? 
Discussion? 
We have time for some face-to-face talks... 
● FromDual provides neutral and independent: 
● Consulting 
● Remote-DBA 
● Support for MySQL, Galera, Percona Server and MariaDB 
● Training 
www.fromdual.com/presentations
Ad

More Related Content

What's hot (19)

Fosdem2012 mariadb-5.3-query-optimizer-r2
Fosdem2012 mariadb-5.3-query-optimizer-r2Fosdem2012 mariadb-5.3-query-optimizer-r2
Fosdem2012 mariadb-5.3-query-optimizer-r2
Sergey Petrunya
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, Really
MYXPLAIN
 
MySQL Performance for DevOps
MySQL Performance for DevOpsMySQL Performance for DevOps
MySQL Performance for DevOps
Sveta Smirnova
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database Administrators
Sveta Smirnova
 
Explain
ExplainExplain
Explain
Ligaya Turmelle
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
Sveta Smirnova
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
Sveta Smirnova
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
Evan Weaver
 
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
Sergey Petrunya
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova
 
Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...
Sveta Smirnova
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
Sveta Smirnova
 
Introducing new SQL syntax and improving performance with preparse Query Rewr...
Introducing new SQL syntax and improving performance with preparse Query Rewr...Introducing new SQL syntax and improving performance with preparse Query Rewr...
Introducing new SQL syntax and improving performance with preparse Query Rewr...
Sveta Smirnova
 
Optimizer Trace Walkthrough
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace Walkthrough
Sergey Petrunya
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL Performance
Sveta Smirnova
 
Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?
Sveta Smirnova
 
Performance Schema for MySQL Troubleshooting
 Performance Schema for MySQL Troubleshooting Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova
 
Percona live-2012-optimizer-tuning
Percona live-2012-optimizer-tuningPercona live-2012-optimizer-tuning
Percona live-2012-optimizer-tuning
Sergey Petrunya
 
Fosdem2012 mariadb-5.3-query-optimizer-r2
Fosdem2012 mariadb-5.3-query-optimizer-r2Fosdem2012 mariadb-5.3-query-optimizer-r2
Fosdem2012 mariadb-5.3-query-optimizer-r2
Sergey Petrunya
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, Really
MYXPLAIN
 
MySQL Performance for DevOps
MySQL Performance for DevOpsMySQL Performance for DevOps
MySQL Performance for DevOps
Sveta Smirnova
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database Administrators
Sveta Smirnova
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
Sveta Smirnova
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
Sveta Smirnova
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
Evan Weaver
 
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...ANALYZE for executable statements - a new way to do optimizer troubleshooting...
ANALYZE for executable statements - a new way to do optimizer troubleshooting...
Sergey Petrunya
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova
 
Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...Modern solutions for modern database load: improvements in the latest MariaDB...
Modern solutions for modern database load: improvements in the latest MariaDB...
Sveta Smirnova
 
MySQL Performance Schema in Action
MySQL Performance Schema in ActionMySQL Performance Schema in Action
MySQL Performance Schema in Action
Sveta Smirnova
 
Introducing new SQL syntax and improving performance with preparse Query Rewr...
Introducing new SQL syntax and improving performance with preparse Query Rewr...Introducing new SQL syntax and improving performance with preparse Query Rewr...
Introducing new SQL syntax and improving performance with preparse Query Rewr...
Sveta Smirnova
 
Optimizer Trace Walkthrough
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace Walkthrough
Sergey Petrunya
 
Troubleshooting MySQL Performance
Troubleshooting MySQL PerformanceTroubleshooting MySQL Performance
Troubleshooting MySQL Performance
Sveta Smirnova
 
Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?Billion Goods in Few Categories: how Histograms Save a Life?
Billion Goods in Few Categories: how Histograms Save a Life?
Sveta Smirnova
 
Performance Schema for MySQL Troubleshooting
 Performance Schema for MySQL Troubleshooting Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova
 
Percona live-2012-optimizer-tuning
Percona live-2012-optimizer-tuningPercona live-2012-optimizer-tuning
Percona live-2012-optimizer-tuning
Sergey Petrunya
 

Similar to Need for Speed: MySQL Indexing (20)

Need for Speed: Mysql indexing
Need for Speed: Mysql indexingNeed for Speed: Mysql indexing
Need for Speed: Mysql indexing
FromDual GmbH
 
MySQL Indexierung CeBIT 2014
MySQL Indexierung CeBIT 2014MySQL Indexierung CeBIT 2014
MySQL Indexierung CeBIT 2014
FromDual GmbH
 
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
Valeriy Kravchuk
 
MariaDB 10.0 Query Optimizer
MariaDB 10.0 Query OptimizerMariaDB 10.0 Query Optimizer
MariaDB 10.0 Query Optimizer
Sergey Petrunya
 
Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
Richard Crowley
 
New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12
Sergey Petrunya
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
KISHOYIANKISH
 
Advanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and Analysis
MYXPLAIN
 
MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015
Dave Stokes
 
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015MySQL 5.7. Tutorial - Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
Dave Stokes
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova
 
MySQLinsanity
MySQLinsanityMySQLinsanity
MySQLinsanity
Stanley Huang
 
Window functions in MySQL 8.0
Window functions in MySQL 8.0Window functions in MySQL 8.0
Window functions in MySQL 8.0
Mydbops
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queries
Damien Seguy
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.
Mydbops
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
Roland Bouman
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
Roland Bouman
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
MYXPLAIN
 
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
Gabriela Ferrara
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
oysteing
 
Need for Speed: Mysql indexing
Need for Speed: Mysql indexingNeed for Speed: Mysql indexing
Need for Speed: Mysql indexing
FromDual GmbH
 
MySQL Indexierung CeBIT 2014
MySQL Indexierung CeBIT 2014MySQL Indexierung CeBIT 2014
MySQL Indexierung CeBIT 2014
FromDual GmbH
 
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
Valeriy Kravchuk
 
MariaDB 10.0 Query Optimizer
MariaDB 10.0 Query OptimizerMariaDB 10.0 Query Optimizer
MariaDB 10.0 Query Optimizer
Sergey Petrunya
 
Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
Richard Crowley
 
New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12
Sergey Petrunya
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
KISHOYIANKISH
 
Advanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and Analysis
MYXPLAIN
 
MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015
Dave Stokes
 
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015MySQL 5.7. Tutorial - Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
Dave Stokes
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova
 
Window functions in MySQL 8.0
Window functions in MySQL 8.0Window functions in MySQL 8.0
Window functions in MySQL 8.0
Mydbops
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queries
Damien Seguy
 
Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.Modern query optimisation features in MySQL 8.
Modern query optimisation features in MySQL 8.
Mydbops
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
Roland Bouman
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
Roland Bouman
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
MYXPLAIN
 
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
Gabriela Ferrara
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
oysteing
 
Ad

More from MYXPLAIN (13)

MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
MYXPLAIN
 
Are You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesAre You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL Indexes
MYXPLAIN
 
MySQL 5.6 Performance
MySQL 5.6 PerformanceMySQL 5.6 Performance
MySQL 5.6 Performance
MYXPLAIN
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
MYXPLAIN
 
Tools and Techniques for Index Design
Tools and Techniques for Index DesignTools and Techniques for Index Design
Tools and Techniques for Index Design
MYXPLAIN
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
MYXPLAIN
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with Explain
MYXPLAIN
 
The Power of MySQL Explain
The Power of MySQL ExplainThe Power of MySQL Explain
The Power of MySQL Explain
MYXPLAIN
 
Improving Performance with Better Indexes
Improving Performance with Better IndexesImproving Performance with Better Indexes
Improving Performance with Better Indexes
MYXPLAIN
 
Explaining the MySQL Explain
Explaining the MySQL ExplainExplaining the MySQL Explain
Explaining the MySQL Explain
MYXPLAIN
 
Covering indexes
Covering indexesCovering indexes
Covering indexes
MYXPLAIN
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
MYXPLAIN
 
Advanced query optimization
Advanced query optimizationAdvanced query optimization
Advanced query optimization
MYXPLAIN
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
MYXPLAIN
 
Are You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL IndexesAre You Getting the Best of your MySQL Indexes
Are You Getting the Best of your MySQL Indexes
MYXPLAIN
 
MySQL 5.6 Performance
MySQL 5.6 PerformanceMySQL 5.6 Performance
MySQL 5.6 Performance
MYXPLAIN
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
MYXPLAIN
 
Tools and Techniques for Index Design
Tools and Techniques for Index DesignTools and Techniques for Index Design
Tools and Techniques for Index Design
MYXPLAIN
 
Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6Powerful Explain in MySQL 5.6
Powerful Explain in MySQL 5.6
MYXPLAIN
 
Optimizing Queries with Explain
Optimizing Queries with ExplainOptimizing Queries with Explain
Optimizing Queries with Explain
MYXPLAIN
 
The Power of MySQL Explain
The Power of MySQL ExplainThe Power of MySQL Explain
The Power of MySQL Explain
MYXPLAIN
 
Improving Performance with Better Indexes
Improving Performance with Better IndexesImproving Performance with Better Indexes
Improving Performance with Better Indexes
MYXPLAIN
 
Explaining the MySQL Explain
Explaining the MySQL ExplainExplaining the MySQL Explain
Explaining the MySQL Explain
MYXPLAIN
 
Covering indexes
Covering indexesCovering indexes
Covering indexes
MYXPLAIN
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
MYXPLAIN
 
Advanced query optimization
Advanced query optimizationAdvanced query optimization
Advanced query optimization
MYXPLAIN
 
Ad

Recently uploaded (20)

Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 

Need for Speed: MySQL Indexing

  • 1. www.fromdual.com 1 / 29 Need for Speed: MySQL Indexing Percona Live 2013, November 11 – 12, London Oli Sennhauser Senior MySQL Consultant at FromDual GmbH [email protected]
  • 2. www.fromdual.com 2 / 29 About FromDual GmbH ● FromDual provides neutral and independent: ● Consulting for MySQL, Percona Server, MariaDB ● Support for all MySQL and Galera Cluster ● Remote-DBA Services ● MySQL Training ● Oracle Silver Partner (OPN) www.fromdual.com
  • 3. www.fromdual.com 3 / 29 Our customers
  • 4. www.fromdual.com 4 / 29 MySQL and Indexing ● MySQL documentation says: The best way to improve the performance of SELECT operations is to create indexes on one or more of the columns that are tested in the query. ● Great! But: Unnecessary indexes waste space and waste time to determine which indexes to use. You must find the right balance to achieve fast queries using the optimal set of indexes. ● ... hmm so we have to think a bit... :-(
  • 5. www.fromdual.com 5 / 29 What is an Index? ● Adams, Douglas: The Hitchhiker's Guide to the Galaxy? ● Sennhauser, Oli, Uster?
  • 6. www.fromdual.com 6 / 29 What is an Index technically?
  • 7. www.fromdual.com 7 / 29 MySQL uses indexes: ● To enforce uniqueness (PRIMARY KEY, UNIQUE KEY) ● To fast access and filter rows (WHERE) ● To perform joins fast (JOIN) ● To find MIN() and MAX() values ● For sorting and grouping (ORDER BY, GROUP BY) ● To avoid joins by using covering indexes ● To enforce FOREIGN KEY Constraints (FOREIGN KEY)
  • 8. www.fromdual.com 8 / 29 WHERE clause 1 SELECT * FROM customers WHERE name = 'No ClSuHeO Wo fC RMEyASTQEL TLALBCL'E; customersG CREATE TABLE `customers` ( `customer_id` smallint(5) unsigned , `name` varchar(64) DEFAULT NULL , PRIMARY KEY (`customer_id`) EXPLAIN ) SELECT * FROM customers WHERE name = 'No Clue of MySQL LLC'; +-----------+------+---------------+------+-------+-------------+ | table | type | possible_keys | key | rows | Extra | +-----------+------+---------------+------+-------+-------------+ | customers | ALL | NULL | NULL | 31978 | Using where | +-----------+------+---------------+------+-------+-------------+
  • 9. www.fromdual.com 9 / 29 How to create and Index? ALTER TABLE … ● ADD PRIMARY KEY (id); ● ADD UNIQUE KEY (uuid); ● ADD FOREIGN KEY (customer_id) REFERENCES customers (customer_id); ● ADD INDEX (last_name, first_name); ● ADD INDEX pre_ind (hash(8)); ● ADD FULLTEXT INDEX (last_name, first_name);
  • 10. www.fromdual.com 10 / 29 WHERE clause 2 ALTER TABLE customers ADD INDEX (name); CREATE TABLE `customers` ( `customer_id` smallint(5) unsigned Gain: 20 ms → 5 ms , `name` varchar(64) DEFAULT NULL , PRIMARY KEY (`customer_id`) , KEY `name` (`name`) ) +-----------+------+---------------+------+---------+-------+------+ | table | type | possible_keys | key | key_len | ref | rows | +-----------+------+---------------+------+---------+-------+------+ | customers | ref | name | name | 67 | const | 1 | +-----------+------+---------------+------+---------+-------+------+
  • 11. www.fromdual.com 11 / 29 JOIN clause EXPLAIN SELECT * FROM customers AS c JOIN orders AS o ON c.customer_id = o.customer_id WHERE c.name = 'No Clue of MySQL LLC'; +-------+------+---------------+------+---------+-------+---------+ | table | type | possible_keys | key | key_len | ref | rows | +-------+------+---------------+------+---------+-------+---------+ | c | ref | PRIMARY,Gain: name 450 | name ms | → 67 6 ms | const | 1 | | o | ALL | NULL | NULL | NULL | NULL | 1045105 | +-------+------+---------------+------+---------+-------+---------+ ALTER TABLE orders ADD INDEX (customer_id); +-------+------+---------------+-------------+---------+---------------+------+ | table | type | possible_keys | key | key_len | ref | rows | +-------+------+---------------+-------------+---------+---------------+------+ | c | ref | PRIMARY,name | name | 67 | const | 1 | | o | ref | customer_id | customer_id | 3 | c.customer_id | 8 | +-------+------+---------------+-------------+---------+---------------+------+
  • 12. www.fromdual.com 12 / 29 For sorting/grouping tables ORDER BY, GROUP BY EXPLAIN SELECT * FROM contacts AS c WHERE last_name = 'Sennhauser' ORDER BY last_name, first_name; +-------+------+-----------+------+----------------------------------------------------+ | table | type | key | +-------+------+-----------+------+----------------------------------------------------+ Gain: rows | Extra 20 ms → 7 ms | | c | ref | last_name | 1561 | Using index condition; Using where; Using filesort | +-------+------+-----------+------+----------------------------------------------------+ ALTER TABLE contacts ADD INDEX (last_name, first_name); +----------+------+-------------+------+--------------------------+ | table | type | key | rows | Extra | +----------+------+-------------+------+--------------------------+ | contacts | ref | last_name_2 | 1561 | Using where; Using index | +----------+------+-------------+------+--------------------------+
  • 13. www.fromdual.com 13 / 29 Covering Indexes EXPLAIN SELECT customer_id, amount FROM orders AS o WHERE customer_id = 59349; +-------+------+-------------+------+-------+ | table | type | key | rows | Extra | +-------+------+-------------+------+-------+ | o | ref | customer_id So | what? 15 | NULL | +-------+------+-------------+------+-------+ ALTER TABLE orders ADD INDEX (customer_id, amount); +-------+------+---------------+------+-------------+ | table | type | key | rows | Extra | +-------+------+---------------+------+-------------+ | o | ref | customer_id_2 | 15 | Using index | +-------+------+---------------+------+-------------+
  • 14. www.fromdual.com 14 / 29 Benefit of Covering Indexes ● Why are Covering Indexes beneficial
  • 15. www.fromdual.com 15 / 29 How-to find missing indexes? ● ER Diagram? :-( ● Most of them rely to you business logic... ● How-to FIND? ● Slow Query Log ● MySQL Variables: ● Since v5.1 on-line! +-------------------------------+----------+ | Variable_name | Value | +-------------------------------+----------+ | log_queries_not_using_indexes | ON | | long_query_time | 0.250000 | | min_examined_row_limit | 100 | | slow_query_log | ON | | slow_query_log_file | slow.log | +-------------------------------+----------+
  • 16. www.fromdual.com 16 / 29 Indexes are not only good ● Indexes use space (Disk, hot data in RAM!) ● Indexes use time to maintain (CPU, RAM, I/O) ● Optimizer needs time to determine which indexes to use. ● Sometimes optimizer is completely confused and does wrong decisions if too many (similar) indexes are there. → You must find the right balance to achieve fast queries using the optimal set of indexes.
  • 17. www.fromdual.com 17 / 29 Smaller indexes faster queries ● Better fit into memory (less I/O) ● Higher data density (rows/block) ● Less CPU cycles (to crawl through) ● Prefixed indexes: ADD INDEX pre_ind (hash(8));
  • 18. www.fromdual.com 18 / 29 Avoid indexes ● Avoid redundant (and thus unnecessary ) indexes ● How does it happen? ● Developer 1: Creates a Foreign Key constraint → done ● Developer 2: Ouu! Query is slow → Oli told me to create an index! → done ● Developer 3: Ouu! Query is slow → Developer 2 is stupid! → Create and index → done ● Frameworks vs. Developer ● Upgrade process vs. Developer ● Avoid indexes which are not used / needed
  • 19. www.fromdual.com 19 / 29 How to find such indexes? SHOW CREATE TABLE ...G mysqldump ­­no­data > structure_dump.sql ● Since MySQL 5.6: PERFORMANCE_SCHEMA ● Percona Server / MariaDB: Userstats ● http://fromdual.com/mysql-performance-schema-hints SELECT object_schema, object_name, index_name FROM performance_schema.table_io_waits_summary_by_index_usage WHERE index_name IS NOT NULL AND count_star = 0 ORDER BY object_schema, object_name;
  • 20. www.fromdual.com 20 / 29 Avoid partial redundant indexes ● INDEX (city, last_name, first_name) ● INDEX (city, last_name) ● INDEX (city) ● INDEX (last_name, city) ??? ● INDEX (first_name, last_name) !!!
  • 21. www.fromdual.com SELECT status, COUNT(*) 21 / 29 Bad selectivity ● Remove indexes with bad selectivity (~= low cardinality) ● Candidates are: ● status ● gender ● active ● How to find if field has bad selectivity? Indexes (and Joins) are expensive!!! ● Break even between 15% and 66% FROM orders GROUP BY status; +--------+--------+ | status | cnt | +--------+--------+ | 0 | 393216 | | 1 | 262144 | | 2 | 12 | | 3 | 36 | | 4 | 24 | | 5 | 4 | | 6 | 8 | +--------+--------+ ● Lets see if the MySQL optimizer knows about it... :-)
  • 22. www.fromdual.com SELECT status, COUNT(*) +--------+--------+ | status | cnt | +--------+--------+ | 0 | 393216 | | 1 | 262144 | | 2 | 12 | | 3 | 36 | | 4 | 24 | | 5 | 4 | | 6 | 8 | +--------+--------+ 22 / 29 Optimizer is wrong! SELECT * FROM orders WHERE status = 2; +--------+------+---------------+--------+------+ | table | type | possible_keys | key | rows | +--------+------+---------------+--------+------+ | orders | ref | status | status | 12 | +--------+------+---------------+--------+------+ SELECT * FROM orders WHERE status = 0; 1.43 s +--------+------+---------------+--------+--------+ | table | type | possible_keys | key | rows | +--------+------+---------------+--------+--------+ | orders | ref | status | status | 327469 | +--------+------+---------------+--------+--------+ FROM orders GROUP BY status; SELECT * FROM orders IGNORE INDEX (STATUS) WHERE status = 0; 0.44 s +--------+------+---------------+------+--------+ | table | type | possible_5.6.12 keys (after | key analyze | rows table) | +--------+------+---------------+------+--------+ | orders | ALL | NULL | NULL | 654939 | +--------+------+---------------+------+--------+
  • 23. www.fromdual.com 23 / 29 InnoDB PK and SK ● InnoDB has ● Primary Keys and ● Secondary Keys
  • 24. www.fromdual.com 24 / 29 Clustered Index ● InnoDB: Data = Leaf of Primary Key ● We call this an Index Clustered Table (IOT) → Data are sorted like PK (key is sorted)! → PK influences Locality of data (physical location) ● AUTO_INCREMENT ~= sorting by time! ● Good for many things ● where hot data = recent data ● Bad for time series ● Where hot data = per item data
  • 25. www.fromdual.com 25 / 29 Example: InnoDB A_I ts v_id xpos ypos ... 1 17:30 #42 x, y, ... 2 17:30 #43 x, y, ... 3 17:30 #44 x, y, ... ... 2001 17:32 #42 x, y, ... 2002 17:32 #43 x, y, ... 2003 17:32 #44 x, y, ... #42 every 2' over the last 3 days ~ 2000 rows ~ 200 kbyte Q1: Δ in rows? A1: 1 row ~ 100 byte Q2: Δ in bytes? Q3: Default InnoDB block size? default: 16 kbyte Q4: Avg. # of rows of car #42 in 1 InnoDB block? A2: 3 d and 720 pt/d → ~2000 pt ~ 2000 rec ~ 2000 blk Q5: How long will this take and why (32 Mbyte)? ~ 2000 IOPS ~ 10s random read!!! S: All in RAM or strong I/O system or …? ~ 1 2000 trucks
  • 26. www.fromdual.com 26 / 29 InnoDB PK safes the day! ts v_id xpos ypos ... 17:30 #42 x, y, ... 17:32 #42 x, y, ... 17:34 #42 x, y, ... ... #42 every 2' 17:30 #43 x, y, ... 17:32 #43 x, y, ... 17:34 #43 x, y, ... over the last 3 days Q1: Avg. # of rows of car #42 in 1 InnoDB block? A1: 3 d and 720 pt/d → ~2000 pt ~ 2000 rec ~ 20 blk Q2: How long will this take and why (320 kbyte)? ~ 1-2 IOPS ~ 10-20 ms sequential read! S: Wow f=50 faster! Any drawbacks? ~ 120 2000 trucks 17:30 #44 x, y, ... ...
  • 27. www.fromdual.com 27 / 29 Index hints ● MySQL optimizer is sometimes wrong! ● We have to help (= hint) him... ● Index hints are: ● USE INDEX (ind1, ind2) ● Only consider these indexes ● FORCE INDEX (ind3) ● Use this index without considering anything else ● IGNORE INDEX (ind1, ind3) ● Do NOT consider these indexes but everything else ● Hints should be used only as a last resort
  • 28. www.fromdual.com 28 / 29 MySQL Variables ● MySQL variables influencing index use ● MyISAM: key_buffer_size ● InnoDB: innodb_buffer_pool_size / innodb_buffer_pool_instances ● InnoDB Change Buffer ● innodb_change_buffer_max_size ● innodb_change_buffering ● Adaptive Hash Index (AHI) ● MySQL 5.6.3 / 5.5.14 index length 767 → 3072 bytes ● innodb_large_prefix
  • 29. www.fromdual.com 29 / 29 Q & A Questions ? Discussion? We have time for some face-to-face talks... ● FromDual provides neutral and independent: ● Consulting ● Remote-DBA ● Support for MySQL, Galera, Percona Server and MariaDB ● Training www.fromdual.com/presentations