SlideShare a Scribd company logo
Optimizer Cost Model MySQL 5.7
Optimizer Cost Model MySQL 5.7
Optimizer Cost Model MySQL 5.7
http://dev.mysql.com/doc/refman/5.7/en/cost-model.html
Optimizer Cost Model MySQL 5.7
Optimizer Cost Model MySQL 5.7
Optimizer Cost Model MySQL 5.7
Optimizer Cost Model MySQL 5.7
Optimizer Cost Model MySQL 5.7
DECISION_CONST = 0.2
if( (INDEX_SIZE/BUFFER_POOL_SIZE) < DECISION_CONST ){
in_memory_estimation = 1.0
}else if( (INDEX_SIZE/BUFFER_POOL_SIZE) > 1.0 ){
in_memory_estimation = 0.0
}else{
in_memory_estimation = 1.0 –
(((INDEX_SIZE/BUFFER_POOL_SIZE) - DECISION_CONST) / (1.0 - DECISION_CONST))
}
Optimizer Cost Model MySQL 5.7
Optimizer Cost Model MySQL 5.7
mysql> EXPLAIN FORMAT=JSON
select * from emp where name between 'a' and 'b';
...
"table": {
"table_name": "emp",
"access_type": "range",
"possible_keys": [
"ix_name"
],
...
"key_length": "53",
"rows_examined_per_scan": 169,
"rows_produced_per_join": 169,
"filtered": "100.00",
"index_condition": "(`test`.`emp`.`name` between 'a' and 'b‘..
"cost_info": {
"read_cost": "203.81",
"eval_cost": "33.80",
"prefix_cost": "237.61",
"data_read_per_join": "10K"
}...
mysql> EXPLAIN FORMAT=JSON
select * from emp;
...
"table": {
"table_name": "emp",
"access_type": "ALL",
"rows_examined_per_scan": 3083,
"rows_produced_per_join": 3083,
"filtered": "100.00",
"cost_info": {
"read_cost": "10.00",
"eval_cost": "616.60",
"prefix_cost": "626.60",
"data_read_per_join": "192K"
},
...
set optimizer_trace="enabled=on";
select * from emp where name in ('matt', 'smith') and dept_no=2;
select * from information_schema.optimizer_trace;
set optimizer_trace="enabled=off";
"rows_estimation": [
{
"table": "`emp`",
"range_analysis": {
"table_scan": {
"rows": 3083,
"cost": 628.7
},
"potential_range_indexes": [
{
"index": "PRIMARY",
"usable": false,
"cause": "not_applicable"
},
{
"index": "ix_name",
"usable": true, ...
},
{
"index": "ix_deptno",
"usable": true, ...
}
],
"range_scan_alternatives": [
{
"index": "ix_name",
"ranges": [...],
"index_dives_for_eq_ranges": true,
"rowid_ordered": false,
"using_mrr": false,
"index_only": false,
"rows": 2,
"cost": 4.41,
"chosen": true
},
{
"index": "ix_deptno",
"ranges": [...],
"index_dives_for_eq_ranges": true,
"rowid_ordered": true,
"using_mrr": false,
"index_only": false,
"rows": 276,
"cost": 332.21,
"chosen": false,
"cause": "cost"
}
],...
"chosen_range_access_summary": {
"range_access_plan": {
"type": "range_scan",
"index": "ix_name",
"rows": 2,
"ranges": [...]
},
"rows_for_plan": 2,
"cost_for_plan": 4.41,
"chosen": true
}
Optimizer Cost Model MySQL 5.7
Access Path
of
Driving Table
Driving
Table
Driven
Table
Join
Condition
Filtering
Access Path
of
Driving Table
JoinCondition
Filtering
Driving
Table
Driven
Table
SELECT *
FROM t1 AS t1a JOIN t1 AS t1b ON t1a.idx_col=t1b.idx_col
WHERE t1b.non_idx_col=5;
---+-------+------+---------+---------+-------------+------+------------
id | table | type | key | key_len | ref | rows | Extra
---+-------+------+---------+---------+-------------+------+------------
1 | t1a | ALL | NULL | NULL | NULL | 1000 | Using where
1 | t1b | ref | idx_col | 5 | t1a.idx_col | 8 | Using where
---+-------+------+---------+---------+-------------+------+------------
Optimizer Cost Model MySQL 5.7
Operation | Fanout-ratio
----------+----------------------------------------
= | MAX(0.005, 1/rowcount)
<=> | SEL(=)
<,<= | MAX(1/3, 1/rowcount)
>, >= | MAX(1/3, 1/rowcount)
BETWEEN | MAX(1/9, 1/rowcount)
LIKE | SEL(BETWEEN)
IS NULL | SEL(=)
NOT OP | 1 - SEL(OP)
Fulltext | SEL(=)
AND | P(A and B) = P(A) * P(B)
OR | P(A or B) = P(A) + P(B) - P(A and B)
<col> IN | MIN(#items_in_list * SEL(=), 1/2)
<list> IN | SEL(col_1 IN) * ... * SEL(col_n IN)
XOR | P(A) + P(B) - 2*P(A and B)
mysql> EXPLAIN SELECT * FROM employee
-> WHERE hire_date BETWEEN "2012-01-01" AND "2012-06-01" AND
-> first_name="John";
+----+----------+------+---------------+------+-------+------+----------+
| id | table | type | possible_keys | key | ref | rows | filtered |
+----+----------+------+---------------+------+-------+------+----------+
| 1 | employee | ref | name,h_date | name | const | 8 | 16.31 |
+----+----------+------+---------------+------+-------+------+----------+
mysql> EXPLAIN
-> SELECT *
-> FROM employee JOIN department ON employee.dept_no=department.dept_no
-> WHERE employee.first_name="John" AND
-> employee.hire_date BETWEEN "2012-01-01" AND "2012-06-01";
+----+------------+--------+------------------+---------+---------+------+----------+
| id | table | type | possible_keys | key | ref | rows | filtered |
+----+------------+--------+------------------+---------+---------+------+----------+
| 1 | employee | ref | name,h_date,dept | name | const | 8 | 16.31 |
| 1 | department | eq_ref | PRIMARY | PRIMARY | dept_no | 1 | 100.00 |
+----+------------+--------+------------------+---------+---------+------+----------+
Optimizer Cost Model MySQL 5.7
SELECT fd1, fd2, fd3 FROM tab WHERE fd4=‘MATT';
-> After rewrite
SELECT fd1, fd2, fd3 FROM tab USE INDEX (ix_fd4) WHERE fd4=‘MATT';
Optimizer Cost Model MySQL 5.7
SELECT * FROM table_a UNION ALL SELECT * FROM table_b;
SELECT * FROM tab WHERE (fd1, fd2) IN ( (1,2), (2,3), (3,4) );
-- // MySQL 5.6
-- // -> Optimizer have to check
SELECT * FROM tab WHERE (fd1=1 AND fd2=2) OR (fd1=2 AND fd2=3) OR (fd1=3 AND fd2=4);
-- // c.f. even MySQL 5.7
mysql> EXPLAIN SELECT * FROM emp FORCE INDEX(ix_name_deptno) WHERE (name, dept_no) > ('z', 10000);
+----+-------------+-------+------+------+------+------+----------+-------------+
| id | select_type | table | type | key | ref | rows | filtered | Extra |
+----+-------------+-------+------+------+------+------+----------+-------------+
| 1 | SIMPLE | emp | ALL | NULL | NULL | 3083 | 100.00 | Using where |
+----+-------------+-------+------+------+------+------+----------+-------------+
mysql> SELECT COUNT(*) FROM emp FORCE INDEX(ix_name_deptno) WHERE (name, dept_no) > ('z', 10000);
-> 2
SELECT *
FROM (SELECT * FROM t1 WHERE fd1=1) AS derived
WHERE ...
-- // -> Merge to outer query
SELECT * FROM t1 WHERE fd1=1 AND ...
performance_schema_events_stages_history_size=10
performance_schema_events_statements_history_size=10
performance_schema_events_waits_history_size=10
These are not NOT index hints, Optimizer hints
Optimizer Cost Model MySQL 5.7

More Related Content

What's hot (20)

PDF
How to create a pluggable database by cloning an existing local pdb
Marco Vigelini
 
PDF
16 MySQL Optimization #burningkeyboards
Denis Ristic
 
PDF
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
maclean liu
 
PDF
Introduction to MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
PPTX
MySQLinsanity
Stanley Huang
 
PDF
Troubleshooting MySQL Performance
Sveta Smirnova
 
PDF
Let's scale-out PostgreSQL using Citus (English)
Noriyoshi Shinoda
 
PPTX
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Dave Stokes
 
PDF
你所不知道的Oracle后台进程Smon功能
maclean liu
 
PDF
Why Use EXPLAIN FORMAT=JSON?
Sveta Smirnova
 
PDF
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
PDF
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
PPTX
SCALE 15x Minimizing PostgreSQL Major Version Upgrade Downtime
Jeff Frost
 
PDF
MySQL Query tuning 101
Sveta Smirnova
 
PDF
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
PostgresOpen
 
PDF
了解Oracle rac brain split resolution
maclean liu
 
PDF
FIXING BLOCK CORRUPTION (RMAN) on 11G
N/A
 
PPT
My sql with querys
NIRMAL FELIX
 
PDF
Introduction into MySQL Query Tuning
Sveta Smirnova
 
PDF
Basic - Oracle Edition Based Redefinition Presentation
N/A
 
How to create a pluggable database by cloning an existing local pdb
Marco Vigelini
 
16 MySQL Optimization #burningkeyboards
Denis Ristic
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
maclean liu
 
Introduction to MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
MySQLinsanity
Stanley Huang
 
Troubleshooting MySQL Performance
Sveta Smirnova
 
Let's scale-out PostgreSQL using Citus (English)
Noriyoshi Shinoda
 
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Dave Stokes
 
你所不知道的Oracle后台进程Smon功能
maclean liu
 
Why Use EXPLAIN FORMAT=JSON?
Sveta Smirnova
 
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
SCALE 15x Minimizing PostgreSQL Major Version Upgrade Downtime
Jeff Frost
 
MySQL Query tuning 101
Sveta Smirnova
 
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
PostgresOpen
 
了解Oracle rac brain split resolution
maclean liu
 
FIXING BLOCK CORRUPTION (RMAN) on 11G
N/A
 
My sql with querys
NIRMAL FELIX
 
Introduction into MySQL Query Tuning
Sveta Smirnova
 
Basic - Oracle Edition Based Redefinition Presentation
N/A
 

Similar to Optimizer Cost Model MySQL 5.7 (20)

PDF
dbms.pdf
walter brand
 
PDF
MySQL Indexing Crash Course
Aaron Silverman
 
PDF
PPT -The MySQL Query optimizer trace .pdf
ssuserf469dc1
 
PDF
The MySQL Query Optimizer Explained Through Optimizer Trace
oysteing
 
PDF
database application using SQL DML statements: all types of Join, Sub-Query ...
bhavesh lande
 
PDF
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
bhavesh lande
 
PDF
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Hemant Kumar Singh
 
PPTX
Sangam 18 - Great Applications with Great SQL
Connor McDonald
 
PDF
database application using SQL DML statements: Insert, Select, Update, Delet...
bhavesh lande
 
PPTX
Perth APAC Groundbreakers tour - SQL Techniques
Connor McDonald
 
TXT
Empresa completo
Lorraine Cuesta
 
PPTX
Optimizing queries MySQL
Georgi Sotirov
 
PDF
Advance MySQL Training by Pratyush Majumdar
Pratyush Majumdar
 
PDF
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
Enkitec
 
PPTX
SQL techniques for faster applications
Connor McDonald
 
PDF
Introduction into MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
PPT
Explain that explain
Fabrizio Parrella
 
PPT
Les06
Sudharsan S
 
PPTX
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
aidenreji
 
PDF
Troubleshooting MySQL Performance add-ons
Sveta Smirnova
 
dbms.pdf
walter brand
 
MySQL Indexing Crash Course
Aaron Silverman
 
PPT -The MySQL Query optimizer trace .pdf
ssuserf469dc1
 
The MySQL Query Optimizer Explained Through Optimizer Trace
oysteing
 
database application using SQL DML statements: all types of Join, Sub-Query ...
bhavesh lande
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
bhavesh lande
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
Hemant Kumar Singh
 
Sangam 18 - Great Applications with Great SQL
Connor McDonald
 
database application using SQL DML statements: Insert, Select, Update, Delet...
bhavesh lande
 
Perth APAC Groundbreakers tour - SQL Techniques
Connor McDonald
 
Empresa completo
Lorraine Cuesta
 
Optimizing queries MySQL
Georgi Sotirov
 
Advance MySQL Training by Pratyush Majumdar
Pratyush Majumdar
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
Enkitec
 
SQL techniques for faster applications
Connor McDonald
 
Introduction into MySQL Query Tuning for Dev[Op]s
Sveta Smirnova
 
Explain that explain
Fabrizio Parrella
 
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
aidenreji
 
Troubleshooting MySQL Performance add-ons
Sveta Smirnova
 
Ad

More from I Goo Lee (20)

PDF
MySQL_Fabric_운영시유의사항
I Goo Lee
 
PDF
MySQL Deep dive with FusionIO
I Goo Lee
 
PDF
From MSSQL to MySQL
I Goo Lee
 
PDF
From MSSQL to MariaDB
I Goo Lee
 
PDF
AWS Aurora 100% 활용하기
I Goo Lee
 
PDF
Backup automation in KAKAO
I Goo Lee
 
PDF
텔레그램을 이용한 양방향 모니터링 시스템 구축
I Goo Lee
 
PDF
Federated Engine 실무적용사례
I Goo Lee
 
PDF
MySQL 상태 메시지 분석 및 활용
I Goo Lee
 
PDF
MySQL 5.7 NF – Optimizer Improvement
I Goo Lee
 
PDF
MySQL 5.7 NF – JSON Datatype 활용
I Goo Lee
 
PDF
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
I Goo Lee
 
PDF
MS 빅데이터 서비스 및 게임사 PoC 사례 소개
I Goo Lee
 
PDF
AWS 환경에서 MySQL Infra 설계하기-2본론
I Goo Lee
 
PDF
AWS 환경에서 MySQL Infra 설계하기-1도입부분
I Goo Lee
 
PDF
AWS 환경에서 MySQL BMT
I Goo Lee
 
PDF
MySQL Slow Query log Monitoring using Beats & ELK
I Goo Lee
 
PDF
MySQL Audit using Percona audit plugin and ELK
I Goo Lee
 
PDF
PostgreSQL 이야기
I Goo Lee
 
PDF
Intro KaKao ADT (Almighty Data Transmitter)
I Goo Lee
 
MySQL_Fabric_운영시유의사항
I Goo Lee
 
MySQL Deep dive with FusionIO
I Goo Lee
 
From MSSQL to MySQL
I Goo Lee
 
From MSSQL to MariaDB
I Goo Lee
 
AWS Aurora 100% 활용하기
I Goo Lee
 
Backup automation in KAKAO
I Goo Lee
 
텔레그램을 이용한 양방향 모니터링 시스템 구축
I Goo Lee
 
Federated Engine 실무적용사례
I Goo Lee
 
MySQL 상태 메시지 분석 및 활용
I Goo Lee
 
MySQL 5.7 NF – Optimizer Improvement
I Goo Lee
 
MySQL 5.7 NF – JSON Datatype 활용
I Goo Lee
 
Intro KaKao MRTE (MySQL Realtime Traffic Emulator)
I Goo Lee
 
MS 빅데이터 서비스 및 게임사 PoC 사례 소개
I Goo Lee
 
AWS 환경에서 MySQL Infra 설계하기-2본론
I Goo Lee
 
AWS 환경에서 MySQL Infra 설계하기-1도입부분
I Goo Lee
 
AWS 환경에서 MySQL BMT
I Goo Lee
 
MySQL Slow Query log Monitoring using Beats & ELK
I Goo Lee
 
MySQL Audit using Percona audit plugin and ELK
I Goo Lee
 
PostgreSQL 이야기
I Goo Lee
 
Intro KaKao ADT (Almighty Data Transmitter)
I Goo Lee
 
Ad

Recently uploaded (20)

PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PPTX
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PPTX
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
PPT
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
PPTX
Cost_of_Quality_Presentation_Software_Engineering.pptx
farispalayi
 
PPTX
Template Timeplan & Roadmap Product.pptx
ImeldaYulistya
 
PPTX
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
PDF
DevOps Design for different deployment options
henrymails
 
PPTX
Powerpoint Slides: Eco Economic Epochs.pptx
Steven McGee
 
PDF
How to Fix Error Code 16 in Adobe Photoshop A Step-by-Step Guide.pdf
Becky Lean
 
PPTX
ZARA-Case.pptx djdkkdjnddkdoodkdxjidjdnhdjjdjx
RonnelPineda2
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PDF
Internet Governance and its role in Global economy presentation By Shreedeep ...
Shreedeep Rayamajhi
 
PPTX
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
PDF
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
PPT
introductio to computers by arthur janry
RamananMuthukrishnan
 
PPTX
英国假毕业证诺森比亚大学成绩单GPA修改UNN学生卡网上可查学历成绩单
Taqyea
 
PDF
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
Cost_of_Quality_Presentation_Software_Engineering.pptx
farispalayi
 
Template Timeplan & Roadmap Product.pptx
ImeldaYulistya
 
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
DevOps Design for different deployment options
henrymails
 
Powerpoint Slides: Eco Economic Epochs.pptx
Steven McGee
 
How to Fix Error Code 16 in Adobe Photoshop A Step-by-Step Guide.pdf
Becky Lean
 
ZARA-Case.pptx djdkkdjnddkdoodkdxjidjdnhdjjdjx
RonnelPineda2
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
Internet Governance and its role in Global economy presentation By Shreedeep ...
Shreedeep Rayamajhi
 
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
introductio to computers by arthur janry
RamananMuthukrishnan
 
英国假毕业证诺森比亚大学成绩单GPA修改UNN学生卡网上可查学历成绩单
Taqyea
 
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 

Optimizer Cost Model MySQL 5.7

  • 10. DECISION_CONST = 0.2 if( (INDEX_SIZE/BUFFER_POOL_SIZE) < DECISION_CONST ){ in_memory_estimation = 1.0 }else if( (INDEX_SIZE/BUFFER_POOL_SIZE) > 1.0 ){ in_memory_estimation = 0.0 }else{ in_memory_estimation = 1.0 – (((INDEX_SIZE/BUFFER_POOL_SIZE) - DECISION_CONST) / (1.0 - DECISION_CONST)) }
  • 13. mysql> EXPLAIN FORMAT=JSON select * from emp where name between 'a' and 'b'; ... "table": { "table_name": "emp", "access_type": "range", "possible_keys": [ "ix_name" ], ... "key_length": "53", "rows_examined_per_scan": 169, "rows_produced_per_join": 169, "filtered": "100.00", "index_condition": "(`test`.`emp`.`name` between 'a' and 'b‘.. "cost_info": { "read_cost": "203.81", "eval_cost": "33.80", "prefix_cost": "237.61", "data_read_per_join": "10K" }... mysql> EXPLAIN FORMAT=JSON select * from emp; ... "table": { "table_name": "emp", "access_type": "ALL", "rows_examined_per_scan": 3083, "rows_produced_per_join": 3083, "filtered": "100.00", "cost_info": { "read_cost": "10.00", "eval_cost": "616.60", "prefix_cost": "626.60", "data_read_per_join": "192K" }, ...
  • 14. set optimizer_trace="enabled=on"; select * from emp where name in ('matt', 'smith') and dept_no=2; select * from information_schema.optimizer_trace; set optimizer_trace="enabled=off";
  • 15. "rows_estimation": [ { "table": "`emp`", "range_analysis": { "table_scan": { "rows": 3083, "cost": 628.7 }, "potential_range_indexes": [ { "index": "PRIMARY", "usable": false, "cause": "not_applicable" }, { "index": "ix_name", "usable": true, ... }, { "index": "ix_deptno", "usable": true, ... } ],
  • 16. "range_scan_alternatives": [ { "index": "ix_name", "ranges": [...], "index_dives_for_eq_ranges": true, "rowid_ordered": false, "using_mrr": false, "index_only": false, "rows": 2, "cost": 4.41, "chosen": true }, { "index": "ix_deptno", "ranges": [...], "index_dives_for_eq_ranges": true, "rowid_ordered": true, "using_mrr": false, "index_only": false, "rows": 276, "cost": 332.21, "chosen": false, "cause": "cost" } ],... "chosen_range_access_summary": { "range_access_plan": { "type": "range_scan", "index": "ix_name", "rows": 2, "ranges": [...] }, "rows_for_plan": 2, "cost_for_plan": 4.41, "chosen": true }
  • 20. SELECT * FROM t1 AS t1a JOIN t1 AS t1b ON t1a.idx_col=t1b.idx_col WHERE t1b.non_idx_col=5; ---+-------+------+---------+---------+-------------+------+------------ id | table | type | key | key_len | ref | rows | Extra ---+-------+------+---------+---------+-------------+------+------------ 1 | t1a | ALL | NULL | NULL | NULL | 1000 | Using where 1 | t1b | ref | idx_col | 5 | t1a.idx_col | 8 | Using where ---+-------+------+---------+---------+-------------+------+------------
  • 22. Operation | Fanout-ratio ----------+---------------------------------------- = | MAX(0.005, 1/rowcount) <=> | SEL(=) <,<= | MAX(1/3, 1/rowcount) >, >= | MAX(1/3, 1/rowcount) BETWEEN | MAX(1/9, 1/rowcount) LIKE | SEL(BETWEEN) IS NULL | SEL(=) NOT OP | 1 - SEL(OP) Fulltext | SEL(=) AND | P(A and B) = P(A) * P(B) OR | P(A or B) = P(A) + P(B) - P(A and B) <col> IN | MIN(#items_in_list * SEL(=), 1/2) <list> IN | SEL(col_1 IN) * ... * SEL(col_n IN) XOR | P(A) + P(B) - 2*P(A and B)
  • 23. mysql> EXPLAIN SELECT * FROM employee -> WHERE hire_date BETWEEN "2012-01-01" AND "2012-06-01" AND -> first_name="John"; +----+----------+------+---------------+------+-------+------+----------+ | id | table | type | possible_keys | key | ref | rows | filtered | +----+----------+------+---------------+------+-------+------+----------+ | 1 | employee | ref | name,h_date | name | const | 8 | 16.31 | +----+----------+------+---------------+------+-------+------+----------+ mysql> EXPLAIN -> SELECT * -> FROM employee JOIN department ON employee.dept_no=department.dept_no -> WHERE employee.first_name="John" AND -> employee.hire_date BETWEEN "2012-01-01" AND "2012-06-01"; +----+------------+--------+------------------+---------+---------+------+----------+ | id | table | type | possible_keys | key | ref | rows | filtered | +----+------------+--------+------------------+---------+---------+------+----------+ | 1 | employee | ref | name,h_date,dept | name | const | 8 | 16.31 | | 1 | department | eq_ref | PRIMARY | PRIMARY | dept_no | 1 | 100.00 | +----+------------+--------+------------------+---------+---------+------+----------+
  • 25. SELECT fd1, fd2, fd3 FROM tab WHERE fd4=‘MATT'; -> After rewrite SELECT fd1, fd2, fd3 FROM tab USE INDEX (ix_fd4) WHERE fd4=‘MATT';
  • 27. SELECT * FROM table_a UNION ALL SELECT * FROM table_b;
  • 28. SELECT * FROM tab WHERE (fd1, fd2) IN ( (1,2), (2,3), (3,4) ); -- // MySQL 5.6 -- // -> Optimizer have to check SELECT * FROM tab WHERE (fd1=1 AND fd2=2) OR (fd1=2 AND fd2=3) OR (fd1=3 AND fd2=4); -- // c.f. even MySQL 5.7 mysql> EXPLAIN SELECT * FROM emp FORCE INDEX(ix_name_deptno) WHERE (name, dept_no) > ('z', 10000); +----+-------------+-------+------+------+------+------+----------+-------------+ | id | select_type | table | type | key | ref | rows | filtered | Extra | +----+-------------+-------+------+------+------+------+----------+-------------+ | 1 | SIMPLE | emp | ALL | NULL | NULL | 3083 | 100.00 | Using where | +----+-------------+-------+------+------+------+------+----------+-------------+ mysql> SELECT COUNT(*) FROM emp FORCE INDEX(ix_name_deptno) WHERE (name, dept_no) > ('z', 10000); -> 2
  • 29. SELECT * FROM (SELECT * FROM t1 WHERE fd1=1) AS derived WHERE ... -- // -> Merge to outer query SELECT * FROM t1 WHERE fd1=1 AND ...
  • 31. These are not NOT index hints, Optimizer hints