SlideShare a Scribd company logo
1 of 33
Download to read offline
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
Sergei Petrunia
Sergei Golubchik
MariaDB & MySQL community event,
Santa Clara, April 2014
MariaDB 10.0 Optimizer
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:282
MySQL/MariaDB Optimizer history
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:283
MariaDB 5.5
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:284
MySQL 5.6
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:285
MariaDB 10.0
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:286
New features in MariaDB 10.0 Optimizer
● Statistics
●Engine-independent statistics (Histograms)
● Algorithms
●Subquery optimizations: EXISTS-to-IN
● EXPLAIN improvements
●SHOW EXPLAIN
●EXPLAIN in the slow query log
●EXPLAIN for UPDATE/DELETE
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:287
New features in MariaDB 10.0 Optimizer
● Statistics
●Engine-independent statistics (Histograms)
● Algorithms
●Subquery optimizations: EXISTS-to-IN
● EXPLAIN improvements
●SHOW EXPLAIN
●EXPLAIN in the slow query log
●EXPLAIN for UPDATE/DELETE
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:288
Query optimizer needs statistics
select *
from
lineitem, orders
where
o_orderkey=l_orderkey and
o_orderdate between '1990-01-01' and '1998-12-06' and
l_extendedprice > 1000000
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:289
Sources of statistics
● Total number of rows in table
● orders - …
● lineitem -...
● Number of rows that match parts of WHERE
select *
from
lineitem, orders
where
o_orderkey=l_orderkey and
o_orderdate between '1990-01-01' and '1998-12-06' and
l_extendedprice > 1000000;
― “key CMP const” - range condition
―Precise estimates
―Requires an index
―B-Tree index dives
―Expensive to get.
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2810
Sources of statistics (2)
where
o_orderkey=l_orderkey and
o_orderdate between '1990-01-01' and '1998-12-06' and
l_extendedprice > 1000000;
● orders.o_orderkey=...
lineitem.l_orderkey=...
● “how many lineitem-s for some $orderkey”?
● Index statistics
● InnoDB
● Random sampling (innodb_stats_sample_pages)
● At “random” times
● MySQL 5.6: persistent statistics
● Still random sampling.
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2811
Problems with statistics
● Index statistics: randomness
●Collected via sampling
●Different query plans
●Even for DBT-3 benchmark
●InnoDB Persistent Statistics is a partial solution
● Not index? no statistics
● not_indexed_column= 'foo'
● key_column2= 'bar'.
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2812
MariaDB 10.0: Engine independent statistics
● No random sampling
●ANALYZE TABLE makes full table scan
● No auto-updates
●Statistics is deterministic
● More statistics data
●#rows in the table (same)
●Index statistics: cardinality (same)
+Statistics on non-indexed columns
+MIN/MAX values
+Frequency
+#NULL values
+Histograms.
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2813
New statistics test run
select *
from
lineitem, orders
where
o_orderkey=l_orderkey and
o_orderdate between '1990-01-01' and '1998-12-06' and
l_extendedprice > 1000000
+--+-----------+--------+----+-------------+-------+-------+-----------------+-------+--------+-----------+
|id|select_type|table |type|possible_keys|key |key_len|ref |rows |filtered|Extra |
+--+-----------+--------+----+-------------+-------+-------+-----------------+-------+--------+-----------+
|1 |SIMPLE |orders |ALL |PRIMARY |NULL |NULL |NULL |1494230| 100.00 |Using where|
|1 |SIMPLE |lineitem|ref |PRIMARY,i_...|PRIMARY|4 |orders.o_orderkey|2 | 100.00 |Using where|
+--+-----------+--------+----+-------------+-------+-------+-----------------+-------+--------+-----------+
● 4.2 seconds
● filtered=100%
● Close to truth for o_orderdate between ...
● Far from truth for l_extendedprice > 1000000.
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2814
New statistics test run (2)
set histogram_size=200;
set use_stat_tables='preferably'
analyze table lineitem, orders;
+------------------+---------+----------+-----------------------------------------+
| Table | Op | Msg_type | Msg_text |
+------------------+---------+----------+-----------------------------------------+
| dbt3sf1.lineitem | analyze | status | Engine-independent statistics collected |
| dbt3sf1.lineitem | analyze | status | OK |
| dbt3sf1.orders | analyze | status | Engine-independent statistics collected |
| dbt3sf1.orders | analyze | status | OK |
+------------------+---------+----------+-----------------------------------------+
set optimizer_use_condition_selectivity=4; .
● Collect table statistics
● Make the optimizer use it
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2815
New statistics test run (3)
+--+-----------+--------+------+-------------+-------+-------+-------------------+-------+--------+-----------+
|id|select_type|table |type |possible_keys|key |key_len|ref |rows |filtered|Extra |
+--+-----------+--------+------+-------------+-------+-------+-------------------+-------+--------+-----------+
|1 |SIMPLE |lineitem|ALL |PRIMARY,i_...|NULL |NULL |NULL |6001215| 0.50 |Using where|
|1 |SIMPLE |orders |eq_ref|PRIMARY |PRIMARY|4 |lineitem.l_orderkey|1 | 99.50 |Using where|
+--+-----------+--------+------+-------------+-------+-------+-------------------+-------+--------+-----------+
select *
from
lineitem, orders
where
o_orderkey=l_orderkey and
o_orderdate between '1990-01-01' and '1998-12-06' and
l_extendedprice > 1000000
● Re-run the query
● lineitem.filtered=0.5% -
● 1.5 sec (from 4.2 sec)
● Can be more for many-table joins.
l_extendedprice > 1000000
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2816
New statistics summary
● Persistent, stable statistics
● Statistics for
● Indexes
● Columns (Histograms)
● Need to enable it and collect manually
1.Collect statistics
2.Make optimizer use it
3.Watch EXPLAIN EXTENDED, filtered%.
set histogram_size=...;
set use_stat_tables='preferably' -- 'complementary'
analyze table tbl [persistent for columns (...) indexes (...)]
set optimizer_use_condition_selectivity = 4;
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2817
Further details
● Documentation update pending
●Will blog
● Percona Live talk today:
●MariaDB Optimizer: See? No indexes!
●4:30PM - 5:20PM @ Ballroom E.
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2818
New features in MariaDB 10.0 Optimizer
● Statistics
●Engine-independent statistics (Histograms)
● Algorithms
●Subquery optimizations: EXISTS-to-IN
● EXPLAIN improvements
●SHOW EXPLAIN
●EXPLAIN in the slow query log
●EXPLAIN for UPDATE/DELETE
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2819
Subquery optimizations background
● Traditional way: “straightforward” subquery execution
select * from customer
where
customer.c_acctbal < 0 and
exists (select 1
from orders
where
orders.o_custkey= customer.c_custkey AND
orders.o_orderDATE between $DAY1 and $DAY2)
+--+------------------+--------+----+-------------------------------+-----------+-------+------------------+------+-----------+
|id|select_type |table |type|possible_keys |key |key_len|ref |rows |Extra |
+--+------------------+--------+----+-------------------------------+-----------+-------+------------------+------+-----------+
|1 |PRIMARY |customer|ALL |c_acctbal,i_c_acctbal_nationkey|NULL |NULL |NULL |150303|Using where|
|2 |DEPENDENT SUBQUERY|orders |ref |i_o_orderdate,i_o_custkey |i_o_custkey|5 |customer.c_custkey|7 |Using where|
+--+------------------+--------+----+-------------------------------+-----------+-------+------------------+------+-----------+
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2820
Subquery optimizations background
● Traditional way: “straightforward” subquery execution
select * from customer
where
customer.c_acctbal < 0 and
exists (select 1
from orders
where
orders.o_custkey= customer.c_custkey AND
orders.o_orderDATE between $DAY1 and $DAY2)
● Outer-to-inner execution
● Every subquery re-execution re-runs it from the start
● MariaDB 5.5 added subquery predicate cache.
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2821
MariaDB 5.5, MySQL 5.6
Lots of optimizations for IN (SELECT ...)
● Materialization
● Subquery must be uncorrelated
● Semi-join optimizations
● Both inner-to-outer and outer-to-inner execution
● Materialization
● Join-like execution methods
select * from customer
where
customer.c_acctbal < 0 and
customer.c_custkey IN (select orders.o_custkey
from orders
where
orders.o_orderDATE between ...)
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2822
MariaDB 10.0 – EXISTS subqueries
1.Convert EXISTS into semi-join
select * from customer
where
customer.c_acctbal < 0 and
exists (select 1
from orders
where
orders.o_custkey= customer.c_custkey AND
orders.o_orderDATE between $DAY1 and $DAY2)
Semi-join optimizations
● Both inner-to-outer and outer-to-inner execution
● Join-like execution methods
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2823
MariaDB 10.0 – EXISTS subqueries
2. Convert trivially-correlated EXISTS into IN
EXISTS (select 1
from orders
where
orders.o_custkey=customer.c_custkey AND
orders.o_orderDATE between $DAY1 and $DAY2)
Uncorrelated subquery => can use Materialization
orders.o_custkey IN (select customer.c_custkey
from orders
where
orders.o_orderDATE between $DAY1 and $DAY2)
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2824
EXISTS-to-IN summary
● Enables new subquery optimizations for EXISTS
●WHERE EXISTS(...) gets semi-joins
●Inner-to-outer execution
●Trivially-correlated EXISTS get Materialization
● How to use
● set optimizer_switch='exists_to_in=on'
●Enjoy
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2825
New features in MariaDB 10.0 Optimizer
● Statistics
●Engine-independent statistics (Histograms)
● Algorithms
●Subquery optimizations: EXISTS-to-IN
● EXPLAIN improvements
●SHOW EXPLAIN
●EXPLAIN in the slow query log
●EXPLAIN for UPDATE/DELETE
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2826
SHOW EXPLAIN
● Shows EXPLAIN of a running query
client1> select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey ...
client2> show processlist;
+----+------+-----------+------+---------+------+--------------+--------------------------------------------------------
| Id | User | Host | db | Command | Time | State | Info
+----+------+-----------+------+---------+------+--------------+--------------------------------------------------------
| 2 | root | localhost | test | Query | 0 | init | show processlist
| 3 | root | localhost | db | Query | 4 | Sending data | select count(*) from customer, orders, nation where ...
+----+------+-----------+------+---------+------+--------------+--------------------------------------------------------
client2> show explain for 3;
+------+-------------+----------+------+---------------+---------------+---------+-----------------------+-------+-------------
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
+------+-------------+----------+------+---------------+---------------+---------+-----------------------+-------+-------------
| 1 | SIMPLE | nation | ALL | PRIMARY | NULL | NULL | NULL | 25 | Using where
| 1 | SIMPLE | customer | ref | i_c_nationkey | i_c_nationkey | 5 | db.nation.n_nationkey | 83512 | Using index
| 1 | SIMPLE | orders | ref | i_o_custkey | i_o_custkey | 5 | db.customer.c_custkey | 8 | Using index
+------+-------------+----------+------+---------------+---------------+---------+-----------------------+-------+-------------
3 rows in set, 1 warning (0.00 sec)
client2> show warnings;
+-------+------+---------------------------------------------------------------------------------------------------------------
| Level | Code | Message
+-------+------+---------------------------------------------------------------------------------------------------------------
| Note | 1003 | select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey and n_name
+-------+------+---------------------------------------------------------------------------------------------------------------
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2827
SHOW EXPLAIN (2)
● SHOW EXPLAIN vs EXPLAIN $query_str
●Can see exactly what query plan is running
●EXPLAIN $query_str can be difficult
● @session_vars
● Per-connection settings (@@optimizer_switch?)
● temporary tables
● Uncommitted changes in a transaction
● Etc, etc
● MySQL 5.7: EXPLAIN FOR CONNECTION $id
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2828
EXPLAIN in the slow query log
● log_slow_verbosity=explain
# Time: 140402 6:55:35
# User@Host: root[root] @ localhost []
# Thread_id: 2 Schema: dbt3sf10 QC_hit: No
# Query_time: 15.532745 Lock_time: 0.378632 Rows_sent: 1 Rows_examined: 661743
# Full_scan: Yes Full_join: No Tmp_table: No Tmp_table_on_disk: No
# Filesort: No Filesort_on_disk: No Merge_passes: 0
#
# explain: id select_type table type possible_keys key key_len ref rows Extra
# explain: 1 SIMPLE nation ALL PRIMARY NULL NULL NULL 25 Using where
# explain: 1 SIMPLE customer ref i_c_nationkey i_c_nationkey 5 db.nation.n_nationkey 82649 Us
# explain: 1 SIMPLE orders ref i_o_custkey i_o_custkey 5 db.customer.c_custkey 7 Using
#
use dbt3sf10;
SET timestamp=1396446935;
select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey and n_name='GERMAN
...
slow-query-log
long-query-time=1
log-slow-verbosity=query_plan,explain
my.cnf
$datadir/hostname-slow.log
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2829
EXPLAIN in the slow query log (2)
● Machine-parseable
● Lines start with “# explain: “
● Cells are tab-delimited
● Logs with explains are parsed by pt-query-digest
● Would like to make it report query plan stats
# Time: 140402 6:55:35
# User@Host: root[root] @ localhost []
# Thread_id: 2 Schema: dbt3sf10 QC_hit: No
# Query_time: 15.532745 Lock_time: 0.378632 Rows_sent: 1 Rows_examined: 661743
# Full_scan: Yes Full_join: No Tmp_table: No Tmp_table_on_disk: No
# Filesort: No Filesort_on_disk: No Merge_passes: 0
#
# explain: id select_type table type possible_keys key key_len ref rows Extra
# explain: 1 SIMPLE nation ALL PRIMARY NULL NULL NULL 25 Using where
# explain: 1 SIMPLE customer ref i_c_nationkey i_c_nationkey 5 db.nation.n_nationkey 82649 Us
# explain: 1 SIMPLE orders ref i_o_custkey i_o_custkey 5 db.customer.c_custkey 7 Using
#
use dbt3sf10;
SET timestamp=1396446935;
select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey and n_name='GERMAN
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2830
Other EXPLAIN improvements
● EXPLAIN UPDATE/DELETE/INSERT
●MySQL 5.6 → MariaDB 10.0
● Not ported from MySQL 5.6 so far:
●EXPLAIN FORMAT=JSON
●Optimizer trace
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2831
MariaDB 10.0 summary
● Porting features from
MySQL 5.6
● Also adding our own
new features
● Actually leading the
development
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2832
Beyond MariaDB 10.0
● EXPLAIN FORMAT=JSON
● EXPLAIN ANALYZE [in the slow query log]
● More statistics
● More histogram types
● Observability
● Better use in the optimizer
● ORDER/GROUP BY … LIMIT: fix the mess
● Your input
● ...
Notice: MySQL is a registered trademark of Sun Microsystems, Inc.
17:41:2833
Thanks!
Q&A

More Related Content

What's hot

Mysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperabilityMysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperability
Sergey Petrunya
 
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
Sergey Petrunya
 

What's hot (20)

Optimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databases
 
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
 
Introduction into MySQL Query Tuning for Dev[Op]s
Introduction into MySQL Query Tuning for Dev[Op]sIntroduction into MySQL Query Tuning for Dev[Op]s
Introduction into MySQL Query Tuning for Dev[Op]s
 
Mysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperabilityMysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperability
 
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
 
Optimizer Trace Walkthrough
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace Walkthrough
 
Using histograms to get better performance
Using histograms to get better performanceUsing histograms to get better performance
Using histograms to get better performance
 
Window functions in MariaDB 10.2
Window functions in MariaDB 10.2Window functions in MariaDB 10.2
Window functions in MariaDB 10.2
 
Character Encoding - MySQL DevRoom - FOSDEM 2015
Character Encoding - MySQL DevRoom - FOSDEM 2015Character Encoding - MySQL DevRoom - FOSDEM 2015
Character Encoding - MySQL DevRoom - FOSDEM 2015
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
 
Need for Speed: Mysql indexing
Need for Speed: Mysql indexingNeed for Speed: Mysql indexing
Need for Speed: Mysql indexing
 
0888 learning-mysql
0888 learning-mysql0888 learning-mysql
0888 learning-mysql
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
 
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
 
Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Playing with the CONNECT storage engine
Playing with the CONNECT storage enginePlaying with the CONNECT storage engine
Playing with the CONNECT storage engine
 
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...
 
ANALYZE for Statements - MariaDB's hidden gem
ANALYZE for Statements - MariaDB's hidden gemANALYZE for Statements - MariaDB's hidden gem
ANALYZE for Statements - MariaDB's hidden gem
 

Similar to MariaDB 10.0 Query Optimizer

MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 Minutes
Sveta Smirnova
 
My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2
Morgan Tocker
 
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
 

Similar to MariaDB 10.0 Query Optimizer (20)

Advanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and Analysis
 
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)
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
 
Evolution of Performance Management: Oracle 12c adaptive optimizations - ukou...
Evolution of Performance Management: Oracle 12c adaptive optimizations - ukou...Evolution of Performance Management: Oracle 12c adaptive optimizations - ukou...
Evolution of Performance Management: Oracle 12c adaptive optimizations - ukou...
 
MySQLinsanity
MySQLinsanityMySQLinsanity
MySQLinsanity
 
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
 
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
 
MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 Minutes
 
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
 
Perf Tuning Short
Perf Tuning ShortPerf Tuning Short
Perf Tuning Short
 
Macy's: Changing Engines in Mid-Flight
Macy's: Changing Engines in Mid-FlightMacy's: Changing Engines in Mid-Flight
Macy's: Changing Engines in Mid-Flight
 
My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2
 
Performance schema and sys schema
Performance schema and sys schemaPerformance schema and sys schema
Performance schema and sys schema
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
 
Common schema my sql uc 2012
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
 
sveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in actionsveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in action
 
Performance Schema in Action: demo
Performance Schema in Action: demoPerformance Schema in Action: demo
Performance Schema in Action: demo
 
Top 10 tips for Oracle performance
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
 
Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
 
How Database Convergence Impacts the Coming Decades of Data Management
How Database Convergence Impacts the Coming Decades of Data ManagementHow Database Convergence Impacts the Coming Decades of Data Management
How Database Convergence Impacts the Coming Decades of Data Management
 

More from Sergey Petrunya

How mysql handles ORDER BY, GROUP BY, and DISTINCT
How mysql handles ORDER BY, GROUP BY, and DISTINCTHow mysql handles ORDER BY, GROUP BY, and DISTINCT
How mysql handles ORDER BY, GROUP BY, and DISTINCT
Sergey Petrunya
 

More from Sergey Petrunya (20)

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
 
MariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixesMariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixes
 
Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8
 
Improving MariaDB’s Query Optimizer with better selectivity estimates
Improving MariaDB’s Query Optimizer with better selectivity estimatesImproving MariaDB’s Query Optimizer with better selectivity estimates
Improving MariaDB’s Query Optimizer with better selectivity estimates
 
JSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger pictureJSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger picture
 
MariaDB 10.4 - что нового
MariaDB 10.4 - что новогоMariaDB 10.4 - что нового
MariaDB 10.4 - что нового
 
MariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit holeMariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit hole
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmark
 
MariaDB 10.3 Optimizer - where does it stand
MariaDB 10.3 Optimizer - where does it standMariaDB 10.3 Optimizer - where does it stand
MariaDB 10.3 Optimizer - where does it stand
 
MyRocks in MariaDB | M18
MyRocks in MariaDB | M18MyRocks in MariaDB | M18
MyRocks in MariaDB | M18
 
New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3
 
MyRocks in MariaDB
MyRocks in MariaDBMyRocks in MariaDB
MyRocks in MariaDB
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQL
 
Say Hello to MyRocks
Say Hello to MyRocksSay Hello to MyRocks
Say Hello to MyRocks
 
MyRocks in MariaDB: why and how
MyRocks in MariaDB: why and howMyRocks in MariaDB: why and how
MyRocks in MariaDB: why and how
 
Эволюция репликации в MySQL и MariaDB
Эволюция репликации в MySQL и MariaDBЭволюция репликации в MySQL и MariaDB
Эволюция репликации в MySQL и MariaDB
 
MariaDB 10.1 - что нового.
MariaDB 10.1 - что нового.MariaDB 10.1 - что нового.
MariaDB 10.1 - что нового.
 
MyRocks: табличный движок для MySQL на основе RocksDB
MyRocks: табличный движок для MySQL на основе RocksDBMyRocks: табличный движок для MySQL на основе RocksDB
MyRocks: табличный движок для MySQL на основе RocksDB
 
MariaDB: ANALYZE for statements (lightning talk)
MariaDB:  ANALYZE for statements (lightning talk)MariaDB:  ANALYZE for statements (lightning talk)
MariaDB: ANALYZE for statements (lightning talk)
 
How mysql handles ORDER BY, GROUP BY, and DISTINCT
How mysql handles ORDER BY, GROUP BY, and DISTINCTHow mysql handles ORDER BY, GROUP BY, and DISTINCT
How mysql handles ORDER BY, GROUP BY, and DISTINCT
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

MariaDB 10.0 Query Optimizer

  • 1. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. Sergei Petrunia Sergei Golubchik MariaDB & MySQL community event, Santa Clara, April 2014 MariaDB 10.0 Optimizer
  • 2. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:282 MySQL/MariaDB Optimizer history
  • 3. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:283 MariaDB 5.5
  • 4. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:284 MySQL 5.6
  • 5. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:285 MariaDB 10.0
  • 6. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:286 New features in MariaDB 10.0 Optimizer ● Statistics ●Engine-independent statistics (Histograms) ● Algorithms ●Subquery optimizations: EXISTS-to-IN ● EXPLAIN improvements ●SHOW EXPLAIN ●EXPLAIN in the slow query log ●EXPLAIN for UPDATE/DELETE
  • 7. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:287 New features in MariaDB 10.0 Optimizer ● Statistics ●Engine-independent statistics (Histograms) ● Algorithms ●Subquery optimizations: EXISTS-to-IN ● EXPLAIN improvements ●SHOW EXPLAIN ●EXPLAIN in the slow query log ●EXPLAIN for UPDATE/DELETE
  • 8. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:288 Query optimizer needs statistics select * from lineitem, orders where o_orderkey=l_orderkey and o_orderdate between '1990-01-01' and '1998-12-06' and l_extendedprice > 1000000
  • 9. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:289 Sources of statistics ● Total number of rows in table ● orders - … ● lineitem -... ● Number of rows that match parts of WHERE select * from lineitem, orders where o_orderkey=l_orderkey and o_orderdate between '1990-01-01' and '1998-12-06' and l_extendedprice > 1000000; ― “key CMP const” - range condition ―Precise estimates ―Requires an index ―B-Tree index dives ―Expensive to get.
  • 10. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2810 Sources of statistics (2) where o_orderkey=l_orderkey and o_orderdate between '1990-01-01' and '1998-12-06' and l_extendedprice > 1000000; ● orders.o_orderkey=... lineitem.l_orderkey=... ● “how many lineitem-s for some $orderkey”? ● Index statistics ● InnoDB ● Random sampling (innodb_stats_sample_pages) ● At “random” times ● MySQL 5.6: persistent statistics ● Still random sampling.
  • 11. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2811 Problems with statistics ● Index statistics: randomness ●Collected via sampling ●Different query plans ●Even for DBT-3 benchmark ●InnoDB Persistent Statistics is a partial solution ● Not index? no statistics ● not_indexed_column= 'foo' ● key_column2= 'bar'.
  • 12. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2812 MariaDB 10.0: Engine independent statistics ● No random sampling ●ANALYZE TABLE makes full table scan ● No auto-updates ●Statistics is deterministic ● More statistics data ●#rows in the table (same) ●Index statistics: cardinality (same) +Statistics on non-indexed columns +MIN/MAX values +Frequency +#NULL values +Histograms.
  • 13. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2813 New statistics test run select * from lineitem, orders where o_orderkey=l_orderkey and o_orderdate between '1990-01-01' and '1998-12-06' and l_extendedprice > 1000000 +--+-----------+--------+----+-------------+-------+-------+-----------------+-------+--------+-----------+ |id|select_type|table |type|possible_keys|key |key_len|ref |rows |filtered|Extra | +--+-----------+--------+----+-------------+-------+-------+-----------------+-------+--------+-----------+ |1 |SIMPLE |orders |ALL |PRIMARY |NULL |NULL |NULL |1494230| 100.00 |Using where| |1 |SIMPLE |lineitem|ref |PRIMARY,i_...|PRIMARY|4 |orders.o_orderkey|2 | 100.00 |Using where| +--+-----------+--------+----+-------------+-------+-------+-----------------+-------+--------+-----------+ ● 4.2 seconds ● filtered=100% ● Close to truth for o_orderdate between ... ● Far from truth for l_extendedprice > 1000000.
  • 14. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2814 New statistics test run (2) set histogram_size=200; set use_stat_tables='preferably' analyze table lineitem, orders; +------------------+---------+----------+-----------------------------------------+ | Table | Op | Msg_type | Msg_text | +------------------+---------+----------+-----------------------------------------+ | dbt3sf1.lineitem | analyze | status | Engine-independent statistics collected | | dbt3sf1.lineitem | analyze | status | OK | | dbt3sf1.orders | analyze | status | Engine-independent statistics collected | | dbt3sf1.orders | analyze | status | OK | +------------------+---------+----------+-----------------------------------------+ set optimizer_use_condition_selectivity=4; . ● Collect table statistics ● Make the optimizer use it
  • 15. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2815 New statistics test run (3) +--+-----------+--------+------+-------------+-------+-------+-------------------+-------+--------+-----------+ |id|select_type|table |type |possible_keys|key |key_len|ref |rows |filtered|Extra | +--+-----------+--------+------+-------------+-------+-------+-------------------+-------+--------+-----------+ |1 |SIMPLE |lineitem|ALL |PRIMARY,i_...|NULL |NULL |NULL |6001215| 0.50 |Using where| |1 |SIMPLE |orders |eq_ref|PRIMARY |PRIMARY|4 |lineitem.l_orderkey|1 | 99.50 |Using where| +--+-----------+--------+------+-------------+-------+-------+-------------------+-------+--------+-----------+ select * from lineitem, orders where o_orderkey=l_orderkey and o_orderdate between '1990-01-01' and '1998-12-06' and l_extendedprice > 1000000 ● Re-run the query ● lineitem.filtered=0.5% - ● 1.5 sec (from 4.2 sec) ● Can be more for many-table joins. l_extendedprice > 1000000
  • 16. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2816 New statistics summary ● Persistent, stable statistics ● Statistics for ● Indexes ● Columns (Histograms) ● Need to enable it and collect manually 1.Collect statistics 2.Make optimizer use it 3.Watch EXPLAIN EXTENDED, filtered%. set histogram_size=...; set use_stat_tables='preferably' -- 'complementary' analyze table tbl [persistent for columns (...) indexes (...)] set optimizer_use_condition_selectivity = 4;
  • 17. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2817 Further details ● Documentation update pending ●Will blog ● Percona Live talk today: ●MariaDB Optimizer: See? No indexes! ●4:30PM - 5:20PM @ Ballroom E.
  • 18. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2818 New features in MariaDB 10.0 Optimizer ● Statistics ●Engine-independent statistics (Histograms) ● Algorithms ●Subquery optimizations: EXISTS-to-IN ● EXPLAIN improvements ●SHOW EXPLAIN ●EXPLAIN in the slow query log ●EXPLAIN for UPDATE/DELETE
  • 19. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2819 Subquery optimizations background ● Traditional way: “straightforward” subquery execution select * from customer where customer.c_acctbal < 0 and exists (select 1 from orders where orders.o_custkey= customer.c_custkey AND orders.o_orderDATE between $DAY1 and $DAY2) +--+------------------+--------+----+-------------------------------+-----------+-------+------------------+------+-----------+ |id|select_type |table |type|possible_keys |key |key_len|ref |rows |Extra | +--+------------------+--------+----+-------------------------------+-----------+-------+------------------+------+-----------+ |1 |PRIMARY |customer|ALL |c_acctbal,i_c_acctbal_nationkey|NULL |NULL |NULL |150303|Using where| |2 |DEPENDENT SUBQUERY|orders |ref |i_o_orderdate,i_o_custkey |i_o_custkey|5 |customer.c_custkey|7 |Using where| +--+------------------+--------+----+-------------------------------+-----------+-------+------------------+------+-----------+
  • 20. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2820 Subquery optimizations background ● Traditional way: “straightforward” subquery execution select * from customer where customer.c_acctbal < 0 and exists (select 1 from orders where orders.o_custkey= customer.c_custkey AND orders.o_orderDATE between $DAY1 and $DAY2) ● Outer-to-inner execution ● Every subquery re-execution re-runs it from the start ● MariaDB 5.5 added subquery predicate cache.
  • 21. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2821 MariaDB 5.5, MySQL 5.6 Lots of optimizations for IN (SELECT ...) ● Materialization ● Subquery must be uncorrelated ● Semi-join optimizations ● Both inner-to-outer and outer-to-inner execution ● Materialization ● Join-like execution methods select * from customer where customer.c_acctbal < 0 and customer.c_custkey IN (select orders.o_custkey from orders where orders.o_orderDATE between ...)
  • 22. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2822 MariaDB 10.0 – EXISTS subqueries 1.Convert EXISTS into semi-join select * from customer where customer.c_acctbal < 0 and exists (select 1 from orders where orders.o_custkey= customer.c_custkey AND orders.o_orderDATE between $DAY1 and $DAY2) Semi-join optimizations ● Both inner-to-outer and outer-to-inner execution ● Join-like execution methods
  • 23. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2823 MariaDB 10.0 – EXISTS subqueries 2. Convert trivially-correlated EXISTS into IN EXISTS (select 1 from orders where orders.o_custkey=customer.c_custkey AND orders.o_orderDATE between $DAY1 and $DAY2) Uncorrelated subquery => can use Materialization orders.o_custkey IN (select customer.c_custkey from orders where orders.o_orderDATE between $DAY1 and $DAY2)
  • 24. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2824 EXISTS-to-IN summary ● Enables new subquery optimizations for EXISTS ●WHERE EXISTS(...) gets semi-joins ●Inner-to-outer execution ●Trivially-correlated EXISTS get Materialization ● How to use ● set optimizer_switch='exists_to_in=on' ●Enjoy
  • 25. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2825 New features in MariaDB 10.0 Optimizer ● Statistics ●Engine-independent statistics (Histograms) ● Algorithms ●Subquery optimizations: EXISTS-to-IN ● EXPLAIN improvements ●SHOW EXPLAIN ●EXPLAIN in the slow query log ●EXPLAIN for UPDATE/DELETE
  • 26. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2826 SHOW EXPLAIN ● Shows EXPLAIN of a running query client1> select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey ... client2> show processlist; +----+------+-----------+------+---------+------+--------------+-------------------------------------------------------- | Id | User | Host | db | Command | Time | State | Info +----+------+-----------+------+---------+------+--------------+-------------------------------------------------------- | 2 | root | localhost | test | Query | 0 | init | show processlist | 3 | root | localhost | db | Query | 4 | Sending data | select count(*) from customer, orders, nation where ... +----+------+-----------+------+---------+------+--------------+-------------------------------------------------------- client2> show explain for 3; +------+-------------+----------+------+---------------+---------------+---------+-----------------------+-------+------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra +------+-------------+----------+------+---------------+---------------+---------+-----------------------+-------+------------- | 1 | SIMPLE | nation | ALL | PRIMARY | NULL | NULL | NULL | 25 | Using where | 1 | SIMPLE | customer | ref | i_c_nationkey | i_c_nationkey | 5 | db.nation.n_nationkey | 83512 | Using index | 1 | SIMPLE | orders | ref | i_o_custkey | i_o_custkey | 5 | db.customer.c_custkey | 8 | Using index +------+-------------+----------+------+---------------+---------------+---------+-----------------------+-------+------------- 3 rows in set, 1 warning (0.00 sec) client2> show warnings; +-------+------+--------------------------------------------------------------------------------------------------------------- | Level | Code | Message +-------+------+--------------------------------------------------------------------------------------------------------------- | Note | 1003 | select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey and n_name +-------+------+---------------------------------------------------------------------------------------------------------------
  • 27. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2827 SHOW EXPLAIN (2) ● SHOW EXPLAIN vs EXPLAIN $query_str ●Can see exactly what query plan is running ●EXPLAIN $query_str can be difficult ● @session_vars ● Per-connection settings (@@optimizer_switch?) ● temporary tables ● Uncommitted changes in a transaction ● Etc, etc ● MySQL 5.7: EXPLAIN FOR CONNECTION $id
  • 28. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2828 EXPLAIN in the slow query log ● log_slow_verbosity=explain # Time: 140402 6:55:35 # User@Host: root[root] @ localhost [] # Thread_id: 2 Schema: dbt3sf10 QC_hit: No # Query_time: 15.532745 Lock_time: 0.378632 Rows_sent: 1 Rows_examined: 661743 # Full_scan: Yes Full_join: No Tmp_table: No Tmp_table_on_disk: No # Filesort: No Filesort_on_disk: No Merge_passes: 0 # # explain: id select_type table type possible_keys key key_len ref rows Extra # explain: 1 SIMPLE nation ALL PRIMARY NULL NULL NULL 25 Using where # explain: 1 SIMPLE customer ref i_c_nationkey i_c_nationkey 5 db.nation.n_nationkey 82649 Us # explain: 1 SIMPLE orders ref i_o_custkey i_o_custkey 5 db.customer.c_custkey 7 Using # use dbt3sf10; SET timestamp=1396446935; select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey and n_name='GERMAN ... slow-query-log long-query-time=1 log-slow-verbosity=query_plan,explain my.cnf $datadir/hostname-slow.log
  • 29. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2829 EXPLAIN in the slow query log (2) ● Machine-parseable ● Lines start with “# explain: “ ● Cells are tab-delimited ● Logs with explains are parsed by pt-query-digest ● Would like to make it report query plan stats # Time: 140402 6:55:35 # User@Host: root[root] @ localhost [] # Thread_id: 2 Schema: dbt3sf10 QC_hit: No # Query_time: 15.532745 Lock_time: 0.378632 Rows_sent: 1 Rows_examined: 661743 # Full_scan: Yes Full_join: No Tmp_table: No Tmp_table_on_disk: No # Filesort: No Filesort_on_disk: No Merge_passes: 0 # # explain: id select_type table type possible_keys key key_len ref rows Extra # explain: 1 SIMPLE nation ALL PRIMARY NULL NULL NULL 25 Using where # explain: 1 SIMPLE customer ref i_c_nationkey i_c_nationkey 5 db.nation.n_nationkey 82649 Us # explain: 1 SIMPLE orders ref i_o_custkey i_o_custkey 5 db.customer.c_custkey 7 Using # use dbt3sf10; SET timestamp=1396446935; select count(*) from customer, orders, nation where c_custkey=o_custkey and c_nationkey=n_nationkey and n_name='GERMAN
  • 30. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2830 Other EXPLAIN improvements ● EXPLAIN UPDATE/DELETE/INSERT ●MySQL 5.6 → MariaDB 10.0 ● Not ported from MySQL 5.6 so far: ●EXPLAIN FORMAT=JSON ●Optimizer trace
  • 31. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2831 MariaDB 10.0 summary ● Porting features from MySQL 5.6 ● Also adding our own new features ● Actually leading the development
  • 32. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2832 Beyond MariaDB 10.0 ● EXPLAIN FORMAT=JSON ● EXPLAIN ANALYZE [in the slow query log] ● More statistics ● More histogram types ● Observability ● Better use in the optimizer ● ORDER/GROUP BY … LIMIT: fix the mess ● Your input ● ...
  • 33. Notice: MySQL is a registered trademark of Sun Microsystems, Inc. 17:41:2833 Thanks! Q&A