MariaDB 10.0 Query Optimizer

Sergey Petrunya
Sergey PetrunyaQuery Optimizer Developer at MariaDB Corporation Ab
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
1 of 33

Recommended

New features-in-mariadb-and-mysql-optimizers by
New features-in-mariadb-and-mysql-optimizersNew features-in-mariadb-and-mysql-optimizers
New features-in-mariadb-and-mysql-optimizersSergey Petrunya
1.6K views36 slides
MariaDB: Engine Independent Table Statistics, including histograms by
MariaDB: Engine Independent Table Statistics, including histogramsMariaDB: Engine Independent Table Statistics, including histograms
MariaDB: Engine Independent Table Statistics, including histogramsSergey Petrunya
871 views16 slides
ANALYZE for executable statements - a new way to do optimizer troubleshooting... by
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
1.8K views41 slides
Fosdem2012 replication-features-of-2011 by
Fosdem2012 replication-features-of-2011Fosdem2012 replication-features-of-2011
Fosdem2012 replication-features-of-2011Sergey Petrunya
511 views24 slides
Fosdem2012 mariadb-5.3-query-optimizer-r2 by
Fosdem2012 mariadb-5.3-query-optimizer-r2Fosdem2012 mariadb-5.3-query-optimizer-r2
Fosdem2012 mariadb-5.3-query-optimizer-r2Sergey Petrunya
434 views25 slides
Percona live-2012-optimizer-tuning by
Percona live-2012-optimizer-tuningPercona live-2012-optimizer-tuning
Percona live-2012-optimizer-tuningSergey Petrunya
971 views97 slides

More Related Content

What's hot

Optimizer features in recent releases of other databases by
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesSergey Petrunya
215 views19 slides
Basic MySQL Troubleshooting for Oracle Database Administrators by
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsSveta Smirnova
1.1K views71 slides
Introduction into MySQL Query Tuning for Dev[Op]s by
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]sSveta Smirnova
161 views121 slides
Mysqlconf2013 mariadb-cassandra-interoperability by
Mysqlconf2013 mariadb-cassandra-interoperabilityMysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperabilitySergey Petrunya
3.6K views34 slides
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013 by
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 2013Sergey Petrunya
6.6K views102 slides
Optimizer Trace Walkthrough by
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace WalkthroughSergey Petrunya
287 views42 slides

What's hot(20)

Optimizer features in recent releases of other databases by Sergey Petrunya
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databases
Sergey Petrunya215 views
Basic MySQL Troubleshooting for Oracle Database Administrators by Sveta Smirnova
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database Administrators
Sveta Smirnova1.1K views
Introduction into MySQL Query Tuning for Dev[Op]s by Sveta Smirnova
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
Sveta Smirnova161 views
Mysqlconf2013 mariadb-cassandra-interoperability by Sergey Petrunya
Mysqlconf2013 mariadb-cassandra-interoperabilityMysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperability
Sergey Petrunya3.6K views
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013 by Sergey Petrunya
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 Petrunya6.6K views
Using histograms to get better performance by Sergey Petrunya
Using histograms to get better performanceUsing histograms to get better performance
Using histograms to get better performance
Sergey Petrunya324 views
Character Encoding - MySQL DevRoom - FOSDEM 2015 by mushupl
Character Encoding - MySQL DevRoom - FOSDEM 2015Character Encoding - MySQL DevRoom - FOSDEM 2015
Character Encoding - MySQL DevRoom - FOSDEM 2015
mushupl2.7K views
Performance Schema for MySQL Troubleshooting by Sveta Smirnova
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova1.6K views
Need for Speed: Mysql indexing by FromDual GmbH
Need for Speed: Mysql indexingNeed for Speed: Mysql indexing
Need for Speed: Mysql indexing
FromDual GmbH1.3K views
0888 learning-mysql by sabir18
0888 learning-mysql0888 learning-mysql
0888 learning-mysql
sabir1887 views
Performance Schema for MySQL Troubleshooting by Sveta Smirnova
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
Sveta Smirnova638 views
New features in Performance Schema 5.7 in action by Sveta Smirnova
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 Smirnova610 views
Common Table Expressions in MariaDB 10.2 by Sergey Petrunya
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2
Sergey Petrunya1.5K views
Playing with the CONNECT storage engine by Federico Razzoli
Playing with the CONNECT storage enginePlaying with the CONNECT storage engine
Playing with the CONNECT storage engine
Federico Razzoli126 views
Introducing new SQL syntax and improving performance with preparse Query Rewr... by 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...
Sveta Smirnova1.1K views
ANALYZE for Statements - MariaDB's hidden gem by Sergey Petrunya
ANALYZE for Statements - MariaDB's hidden gemANALYZE for Statements - MariaDB's hidden gem
ANALYZE for Statements - MariaDB's hidden gem
Sergey Petrunya293 views

Similar to MariaDB 10.0 Query Optimizer

Advanced Query Optimizer Tuning and Analysis by
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and AnalysisMYXPLAIN
1.1K views102 slides
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020) by
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
275 views28 slides
Need for Speed: MySQL Indexing by
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL IndexingMYXPLAIN
1.4K views29 slides
Evolution of Performance Management: Oracle 12c adaptive optimizations - ukou... by
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...Nelson Calero
1.2K views64 slides
MySQLinsanity by
MySQLinsanityMySQLinsanity
MySQLinsanityStanley Huang
470 views30 slides
MySQL 5.7 Tutorial Dutch PHP Conference 2015 by
MySQL 5.7 Tutorial Dutch PHP Conference 2015MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015Dave Stokes
864 views76 slides

Similar to MariaDB 10.0 Query Optimizer(20)

Advanced Query Optimizer Tuning and Analysis by MYXPLAIN
Advanced Query Optimizer Tuning and AnalysisAdvanced Query Optimizer Tuning and Analysis
Advanced Query Optimizer Tuning and Analysis
MYXPLAIN1.1K views
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020) by Valeriy Kravchuk
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 Kravchuk275 views
Need for Speed: MySQL Indexing by MYXPLAIN
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
MYXPLAIN1.4K views
Evolution of Performance Management: Oracle 12c adaptive optimizations - ukou... by Nelson Calero
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...
Nelson Calero1.2K views
MySQL 5.7 Tutorial Dutch PHP Conference 2015 by Dave Stokes
MySQL 5.7 Tutorial Dutch PHP Conference 2015MySQL 5.7 Tutorial Dutch PHP Conference 2015
MySQL 5.7 Tutorial Dutch PHP Conference 2015
Dave Stokes864 views
MySQL 5.7. Tutorial - Dutch PHP Conference 2015 by Dave Stokes
MySQL 5.7. Tutorial - Dutch PHP Conference 2015MySQL 5.7. Tutorial - Dutch PHP Conference 2015
MySQL 5.7. Tutorial - Dutch PHP Conference 2015
Dave Stokes2.1K views
MySQL Performance Schema in 20 Minutes by Sveta Smirnova
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 Minutes
Sveta Smirnova1.5K views
Query Optimization with MySQL 5.6: Old and New Tricks by MYXPLAIN
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
MYXPLAIN2.1K views
Macy's: Changing Engines in Mid-Flight by DataStax Academy
Macy's: Changing Engines in Mid-FlightMacy's: Changing Engines in Mid-Flight
Macy's: Changing Engines in Mid-Flight
DataStax Academy1.4K views
My sql 5.7-upcoming-changes-v2 by Morgan Tocker
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2
Morgan Tocker1.1K views
Performance schema and sys schema by Mark Leith
Performance schema and sys schemaPerformance schema and sys schema
Performance schema and sys schema
Mark Leith12.6K views
Common schema my sql uc 2012 by Roland Bouman
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
Roland Bouman519 views
Common schema my sql uc 2012 by Roland Bouman
Common schema   my sql uc 2012Common schema   my sql uc 2012
Common schema my sql uc 2012
Roland Bouman600 views
sveta smirnova - my sql performance schema in action by Dariia Seimova
sveta smirnova - my sql performance schema in actionsveta smirnova - my sql performance schema in action
sveta smirnova - my sql performance schema in action
Dariia Seimova85 views
Performance Schema in Action: demo by Sveta Smirnova
Performance Schema in Action: demoPerformance Schema in Action: demo
Performance Schema in Action: demo
Sveta Smirnova257 views
Top 10 tips for Oracle performance by Guy Harrison
Top 10 tips for Oracle performanceTop 10 tips for Oracle performance
Top 10 tips for Oracle performance
Guy Harrison10.8K views
Adaptive Query Optimization by Anju Garg
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
Anju Garg2.1K views
How Database Convergence Impacts the Coming Decades of Data Management by SingleStore
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
SingleStore628 views

More from Sergey Petrunya

New optimizer features in MariaDB releases before 10.12 by
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.12Sergey Petrunya
112 views27 slides
MariaDB's join optimizer: how it works and current fixes by
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 fixesSergey Petrunya
819 views32 slides
Improved histograms in MariaDB 10.8 by
Improved histograms in MariaDB 10.8Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8Sergey Petrunya
55 views27 slides
Improving MariaDB’s Query Optimizer with better selectivity estimates by
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 estimatesSergey Petrunya
1.6K views50 slides
JSON Support in MariaDB: News, non-news and the bigger picture by
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 pictureSergey Petrunya
2K views33 slides
MariaDB 10.4 - что нового by
MariaDB 10.4 - что новогоMariaDB 10.4 - что нового
MariaDB 10.4 - что новогоSergey Petrunya
679 views45 slides

More from Sergey Petrunya(20)

New optimizer features in MariaDB releases before 10.12 by Sergey Petrunya
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 Petrunya112 views
MariaDB's join optimizer: how it works and current fixes by Sergey Petrunya
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
Sergey Petrunya819 views
Improved histograms in MariaDB 10.8 by Sergey Petrunya
Improved histograms in MariaDB 10.8Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8
Sergey Petrunya55 views
Improving MariaDB’s Query Optimizer with better selectivity estimates by Sergey Petrunya
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
Sergey Petrunya1.6K views
JSON Support in MariaDB: News, non-news and the bigger picture by Sergey Petrunya
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
Sergey Petrunya2K views
MariaDB 10.4 - что нового by Sergey Petrunya
MariaDB 10.4 - что новогоMariaDB 10.4 - что нового
MariaDB 10.4 - что нового
Sergey Petrunya679 views
MariaDB Optimizer - further down the rabbit hole by Sergey Petrunya
MariaDB Optimizer - further down the rabbit holeMariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit hole
Sergey Petrunya241 views
Lessons for the optimizer from running the TPC-DS benchmark by Sergey Petrunya
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
Sergey Petrunya728 views
MariaDB 10.3 Optimizer - where does it stand by Sergey Petrunya
MariaDB 10.3 Optimizer - where does it standMariaDB 10.3 Optimizer - where does it stand
MariaDB 10.3 Optimizer - where does it stand
Sergey Petrunya860 views
New Query Optimizer features in MariaDB 10.3 by Sergey Petrunya
New Query Optimizer features in MariaDB 10.3New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3
Sergey Petrunya691 views
Histograms in MariaDB, MySQL and PostgreSQL by Sergey Petrunya
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQL
Sergey Petrunya1.6K views
MyRocks in MariaDB: why and how by Sergey Petrunya
MyRocks in MariaDB: why and howMyRocks in MariaDB: why and how
MyRocks in MariaDB: why and how
Sergey Petrunya1.3K views
Эволюция репликации в MySQL и MariaDB by Sergey Petrunya
Эволюция репликации в MySQL и MariaDBЭволюция репликации в MySQL и MariaDB
Эволюция репликации в MySQL и MariaDB
Sergey Petrunya1K views
MariaDB 10.1 - что нового. by Sergey Petrunya
MariaDB 10.1 - что нового.MariaDB 10.1 - что нового.
MariaDB 10.1 - что нового.
Sergey Petrunya355 views
MyRocks: табличный движок для MySQL на основе RocksDB by Sergey Petrunya
MyRocks: табличный движок для MySQL на основе RocksDBMyRocks: табличный движок для MySQL на основе RocksDB
MyRocks: табличный движок для MySQL на основе RocksDB
Sergey Petrunya1.4K views
MariaDB: ANALYZE for statements (lightning talk) by Sergey Petrunya
MariaDB:  ANALYZE for statements (lightning talk)MariaDB:  ANALYZE for statements (lightning talk)
MariaDB: ANALYZE for statements (lightning talk)
Sergey Petrunya982 views
How mysql handles ORDER BY, GROUP BY, and DISTINCT by Sergey Petrunya
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 Petrunya4.8K views

Recently uploaded

Discover Aura Workshop (12.5.23).pdf by
Discover Aura Workshop (12.5.23).pdfDiscover Aura Workshop (12.5.23).pdf
Discover Aura Workshop (12.5.23).pdfNeo4j
15 views55 slides
GDSC GLAU Info Session.pptx by
GDSC GLAU Info Session.pptxGDSC GLAU Info Session.pptx
GDSC GLAU Info Session.pptxgauriverrma4
15 views28 slides
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023 by
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023BookNet Canada
44 views19 slides
Business Analyst Series 2023 - Week 4 Session 8 by
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8DianaGray10
145 views13 slides
Digital Personal Data Protection (DPDP) Practical Approach For CISOs by
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOsPriyanka Aash
162 views59 slides
"Node.js Development in 2024: trends and tools", Nikita Galkin by
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin Fwdays
33 views38 slides

Recently uploaded(20)

Discover Aura Workshop (12.5.23).pdf by Neo4j
Discover Aura Workshop (12.5.23).pdfDiscover Aura Workshop (12.5.23).pdf
Discover Aura Workshop (12.5.23).pdf
Neo4j15 views
GDSC GLAU Info Session.pptx by gauriverrma4
GDSC GLAU Info Session.pptxGDSC GLAU Info Session.pptx
GDSC GLAU Info Session.pptx
gauriverrma415 views
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023 by BookNet Canada
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
Redefining the book supply chain: A glimpse into the future - Tech Forum 2023
BookNet Canada44 views
Business Analyst Series 2023 - Week 4 Session 8 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8
DianaGray10145 views
Digital Personal Data Protection (DPDP) Practical Approach For CISOs by Priyanka Aash
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOs
Priyanka Aash162 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays33 views
AI + Memoori = AIM by Memoori
AI + Memoori = AIMAI + Memoori = AIM
AI + Memoori = AIM
Memoori14 views
PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」 by PC Cluster Consortium
PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」
PCCC23:日本AMD株式会社 テーマ1「AMD Instinct™ アクセラレーターの概要」
PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」 by PC Cluster Consortium
PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」
PCCC23:日本AMD株式会社 テーマ2「AMD EPYC™ プロセッサーを用いたAIソリューション」
The Power of Generative AI in Accelerating No Code Adoption.pdf by Saeed Al Dhaheri
The Power of Generative AI in Accelerating No Code Adoption.pdfThe Power of Generative AI in Accelerating No Code Adoption.pdf
The Power of Generative AI in Accelerating No Code Adoption.pdf
Saeed Al Dhaheri39 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray10146 views
Cocktail of Environments. How to Mix Test and Development Environments and St... by Aleksandr Tarasov
Cocktail of Environments. How to Mix Test and Development Environments and St...Cocktail of Environments. How to Mix Test and Development Environments and St...
Cocktail of Environments. How to Mix Test and Development Environments and St...
This talk was not generated with ChatGPT: how AI is changing science by Elena Simperl
This talk was not generated with ChatGPT: how AI is changing scienceThis talk was not generated with ChatGPT: how AI is changing science
This talk was not generated with ChatGPT: how AI is changing science
Elena Simperl32 views
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell by Fwdays
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
Fwdays14 views

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