SlideShare a Scribd company logo
Bend SQL to Your Will With EXPLAIN Ligaya Turmelle MySQL Support Engineer [email_address]
MySQL is the world's most popular open source database software, with over 100 million copies of its software downloaded or distributed throughout its history.
a key part of LAMP (Linux, Apache, MySQL, PHP / Perl / Python), the fast-growing open source enterprise software stack.
Site:  http://www.mysql.com/
Download: http://dev.mysql.com/downloads/
Online Manual: http://dev.mysql.com/doc/refman/5.1/en/index.html MySQL Basics
Objectives So what are we hoping you will learn How to get EXPLAIN output
How to read the output of EXPLAIN
What does it mean? What can we do to speed things up?
Briefly discuss indexing techniques
Briefly discuss query optimization techniques
Start at the beginning Syntax: EXPLAIN [EXTENDED] SELECT  select_options Basically you just put an EXPLAIN  before a SELECT statement. What does EXPLAIN do? displays information from the optimizer about the query execution plan. EXTENDED with SHOW WARNINGS Works only with SELECT statements Some statements can be converted to SELECTS – but the statement has to “touch” all the same columns
Output mysql> EXPLAIN SELECT c.Name FROM City c WHERE District='Florida'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: c type: ref possible_keys: District key: District key_len: 20 ref: const rows: 15 Extra: Using where 1 row in set (0.00 sec)
EXPLAIN output Each row provides information about one table
EXPLAIN output columns id key_len select_type ref table rows type filtered (new to 5.1) possible_keys Extra key
id The SELECT identifier.
Only if there are subqueries, derived tables or unions is this incremented Derived table is a subquery in the FROM clause that created a temporary table  Number reflects the order that the SELECT was done in mysql> EXPLAIN SELECT * FROM (SELECT * FROM COUNTRY) AS Country1 LIMIT 5; +----+-------------+------------+------+---------------+------+--------- | id | select_type | table  | type | possible_keys | key  | key_len  +----+-------------+------------+------+---------------+------+--------- |  1 | PRIMARY  | <derived2> | ALL  | NULL  | NULL | NULL  |  2 | DERIVED  | COUNTRY  | ALL  | NULL  | NULL | NULL  +----+-------------+------------+------+---------------+------+---------
select_type The type of SELECT
Can have lots of values and each means something different SIMPLE – normal SELECT
PRIMARY – outermost SELECT
DERIVED -  subquery in the FROM clause that is placed in a temporary table (table is derived from the subquery).
UNION – second or more SELECT statement in a UNION
SUBQUERY – a SELECT that is not in the FROM clause UNION and SUBQUERY can have DEPENDANT if they use the outer SELECT.
table Displays the name or alias of the table used
Read it down and you see the order of the tables in a JOIN which may or may not be the same as the order given in the query.
Derived tables will be noted and numbered in the listing mysql> EXPLAIN SELECT * FROM (SELECT * FROM COUNTRY) AS Country1 LIMIT 5; +----+-------------+------------+------+---------------+------+--------- | id | select_type | table  | type | possible_keys | key  | key_len  +----+-------------+------------+------+---------------+------+--------- |  1 | PRIMARY  | <derived2> | ALL  | NULL  | NULL | NULL  |  2 | DERIVED  | COUNTRY  | ALL  | NULL  | NULL | NULL  +----+-------------+------------+------+---------------+------+--------- 2 rows in set (0.02 sec)
type The access type for the SELECT query
Various methods:
best  system/const
|  eq_ref
|  ref

More Related Content

PDF
Optimizing Queries with Explain
PDF
My SQL Idiosyncrasies That Bite OTN
PDF
56 Query Optimization
PDF
Understanding Query Execution
PDF
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
PDF
0888 learning-mysql
PDF
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
PDF
Explaining the MySQL Explain
Optimizing Queries with Explain
My SQL Idiosyncrasies That Bite OTN
56 Query Optimization
Understanding Query Execution
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
0888 learning-mysql
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
Explaining the MySQL Explain

What's hot (18)

PDF
Need for Speed: Mysql indexing
PPT
Applied Partitioning And Scaling Your Database System Presentation
PDF
Advanced MySQL Query Tuning
PDF
Why Use EXPLAIN FORMAT=JSON?
PDF
Using Optimizer Hints to Improve MySQL Query Performance
PDF
Efficient Pagination Using MySQL
PPT
Intro to my sql
PDF
介绍 MySQL
PDF
ODP
Beyond php - it's not (just) about the code
PPTX
Lecture3 mysql gui by okello erick
PDF
Moving to the NoSQL side: MySQL JSON functions
PDF
Basic MySQL Troubleshooting for Oracle Database Administrators
PDF
PDF
The MySQL Query Optimizer Explained Through Optimizer Trace
PPT
My sql statements by okello erick
PDF
Mysql Explain Explained
PDF
MySQL partitions tutorial
Need for Speed: Mysql indexing
Applied Partitioning And Scaling Your Database System Presentation
Advanced MySQL Query Tuning
Why Use EXPLAIN FORMAT=JSON?
Using Optimizer Hints to Improve MySQL Query Performance
Efficient Pagination Using MySQL
Intro to my sql
介绍 MySQL
Beyond php - it's not (just) about the code
Lecture3 mysql gui by okello erick
Moving to the NoSQL side: MySQL JSON functions
Basic MySQL Troubleshooting for Oracle Database Administrators
The MySQL Query Optimizer Explained Through Optimizer Trace
My sql statements by okello erick
Mysql Explain Explained
MySQL partitions tutorial
Ad

Viewers also liked (20)

PDF
MySQL Group Replication
PPTX
High Availability Using MySQL Group Replication
PDF
Reducing Risk When Upgrading MySQL
PDF
Everything You Need to Know About MySQL Group Replication
PPTX
2010丹臣的思考
PDF
Mix ‘n’ Match Async and Group Replication for Advanced Replication Setups
PDF
Online MySQL Backups with Percona XtraBackup
PPT
Mysql展示功能与源码对应
PDF
10x Performance Improvements - A Case Study
PDF
Advanced Percona XtraDB Cluster in a nutshell... la suite
PDF
MySQL High Availability Deep Dive
PDF
Lessons Learned: Troubleshooting Replication
PPTX
Redis介绍
PDF
Java MySQL Connector & Connection Pool Features & Optimization
PDF
MySQL InnoDB Cluster - Group Replication
PDF
A New Architecture for Group Replication in Data Grid
PDF
SQL Outer Joins for Fun and Profit
PDF
Why MySQL Replication Fails, and How to Get it Back
PDF
Requirements the Last Bottleneck
PDF
MySQL - checklist для новичка в Highload
MySQL Group Replication
High Availability Using MySQL Group Replication
Reducing Risk When Upgrading MySQL
Everything You Need to Know About MySQL Group Replication
2010丹臣的思考
Mix ‘n’ Match Async and Group Replication for Advanced Replication Setups
Online MySQL Backups with Percona XtraBackup
Mysql展示功能与源码对应
10x Performance Improvements - A Case Study
Advanced Percona XtraDB Cluster in a nutshell... la suite
MySQL High Availability Deep Dive
Lessons Learned: Troubleshooting Replication
Redis介绍
Java MySQL Connector & Connection Pool Features & Optimization
MySQL InnoDB Cluster - Group Replication
A New Architecture for Group Replication in Data Grid
SQL Outer Joins for Fun and Profit
Why MySQL Replication Fails, and How to Get it Back
Requirements the Last Bottleneck
MySQL - checklist для новичка в Highload
Ad

Similar to Explain (20)

PDF
Explain2
PDF
Advanced MySQL Query and Schema Tuning
PDF
MySQL Query And Index Tuning
PDF
MySQL Query tuning 101
PDF
Troubleshooting MySQL Performance add-ons
PDF
Webinar 2013 advanced_query_tuning
PDF
Advanced MySQL Query Optimizations
PDF
Introduction into MySQL Query Tuning
PPTX
Optimizing MySQL queries
PDF
MySQL Query Optimization
PDF
Understanding query-execution806
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
PDF
Goldilocks and the Three MySQL Queries
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
PDF
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
PPT
Indexing
PPT
Explain that explain
PPTX
Optimizando MySQL
PPTX
Optimizing MySQL Queries
Explain2
Advanced MySQL Query and Schema Tuning
MySQL Query And Index Tuning
MySQL Query tuning 101
Troubleshooting MySQL Performance add-ons
Webinar 2013 advanced_query_tuning
Advanced MySQL Query Optimizations
Introduction into MySQL Query Tuning
Optimizing MySQL queries
MySQL Query Optimization
Understanding query-execution806
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
Goldilocks and the Three MySQL Queries
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
MySQL Indexes and Histograms - RMOUG Training Days 2022
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Indexing
Explain that explain
Optimizando MySQL
Optimizing MySQL Queries

More from Ligaya Turmelle (12)

PDF
Troubleshooting tldr
PDF
Rootconf admin101
PDF
Zend2016 dba tutorial
KEY
Normalization
KEY
Character sets
KEY
Tek tutorial
KEY
DPC Tutorial
PPT
MySQL 5.1 Replication
PPT
MySQL 5.5
KEY
Perf Tuning Short
PPT
Php Community
ODP
Performance Tuning
Troubleshooting tldr
Rootconf admin101
Zend2016 dba tutorial
Normalization
Character sets
Tek tutorial
DPC Tutorial
MySQL 5.1 Replication
MySQL 5.5
Perf Tuning Short
Php Community
Performance Tuning

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Modernizing your data center with Dell and AMD
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation theory and applications.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Approach and Philosophy of On baking technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Review of recent advances in non-invasive hemoglobin estimation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
20250228 LYD VKU AI Blended-Learning.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Modernizing your data center with Dell and AMD
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation_ Review paper, used for researhc scholars
Reach Out and Touch Someone: Haptics and Empathic Computing
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation theory and applications.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Approach and Philosophy of On baking technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Spectral efficient network and resource selection model in 5G networks
Review of recent advances in non-invasive hemoglobin estimation

Explain

  • 1. Bend SQL to Your Will With EXPLAIN Ligaya Turmelle MySQL Support Engineer [email_address]
  • 2. MySQL is the world's most popular open source database software, with over 100 million copies of its software downloaded or distributed throughout its history.
  • 3. a key part of LAMP (Linux, Apache, MySQL, PHP / Perl / Python), the fast-growing open source enterprise software stack.
  • 7. Objectives So what are we hoping you will learn How to get EXPLAIN output
  • 8. How to read the output of EXPLAIN
  • 9. What does it mean? What can we do to speed things up?
  • 11. Briefly discuss query optimization techniques
  • 12. Start at the beginning Syntax: EXPLAIN [EXTENDED] SELECT select_options Basically you just put an EXPLAIN before a SELECT statement. What does EXPLAIN do? displays information from the optimizer about the query execution plan. EXTENDED with SHOW WARNINGS Works only with SELECT statements Some statements can be converted to SELECTS – but the statement has to “touch” all the same columns
  • 13. Output mysql> EXPLAIN SELECT c.Name FROM City c WHERE District='Florida'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: c type: ref possible_keys: District key: District key_len: 20 ref: const rows: 15 Extra: Using where 1 row in set (0.00 sec)
  • 14. EXPLAIN output Each row provides information about one table
  • 15. EXPLAIN output columns id key_len select_type ref table rows type filtered (new to 5.1) possible_keys Extra key
  • 16. id The SELECT identifier.
  • 17. Only if there are subqueries, derived tables or unions is this incremented Derived table is a subquery in the FROM clause that created a temporary table Number reflects the order that the SELECT was done in mysql> EXPLAIN SELECT * FROM (SELECT * FROM COUNTRY) AS Country1 LIMIT 5; +----+-------------+------------+------+---------------+------+--------- | id | select_type | table | type | possible_keys | key | key_len +----+-------------+------------+------+---------------+------+--------- | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | 2 | DERIVED | COUNTRY | ALL | NULL | NULL | NULL +----+-------------+------------+------+---------------+------+---------
  • 18. select_type The type of SELECT
  • 19. Can have lots of values and each means something different SIMPLE – normal SELECT
  • 21. DERIVED - subquery in the FROM clause that is placed in a temporary table (table is derived from the subquery).
  • 22. UNION – second or more SELECT statement in a UNION
  • 23. SUBQUERY – a SELECT that is not in the FROM clause UNION and SUBQUERY can have DEPENDANT if they use the outer SELECT.
  • 24. table Displays the name or alias of the table used
  • 25. Read it down and you see the order of the tables in a JOIN which may or may not be the same as the order given in the query.
  • 26. Derived tables will be noted and numbered in the listing mysql> EXPLAIN SELECT * FROM (SELECT * FROM COUNTRY) AS Country1 LIMIT 5; +----+-------------+------------+------+---------------+------+--------- | id | select_type | table | type | possible_keys | key | key_len +----+-------------+------------+------+---------------+------+--------- | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | 2 | DERIVED | COUNTRY | ALL | NULL | NULL | NULL +----+-------------+------------+------+---------------+------+--------- 2 rows in set (0.02 sec)
  • 27. type The access type for the SELECT query
  • 31. | ref
  • 38. system From an in memory table
  • 39. Has only one row of data const optimizer knows that it can get at most one row from the table.
  • 40. Primary key or unique key mysql> EXPLAIN SELECT * FROM Country WHERE Code='USA'; +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+ | 1 | SIMPLE | Country | const | PRIMARY | PRIMARY | 3 | const | 1 | | +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------+
  • 41. eq_ref An index lookup that will only return one row
  • 42. Only used if both conditions are met All parts of a key are used by the JOIN
  • 43. Table contains unique, non-nullable key to JOIN on mysql> EXPLAIN SELECT Country.name FROM Country JOIN City ON City.CountryCode = Country.Code; +----+-------------+---------+--------+---------------+---------+---------+------------------ | id | select_type | table | type | possible_keys | key | key_len | ref +----+-------------+---------+--------+---------------+---------+---------+------------------ | 1 | SIMPLE | City | ALL | NULL | NULL | NULL | NULL | 1 | SIMPLE | Country | eq_ref | PRIMARY | PRIMARY | 3 | world.City.Countr +----+-------------+---------+--------+---------------+---------+---------+------------------
  • 44. ref Similar to eq_ref but it can return more then one row
  • 45. Selected when Either leftmost part of a unique key is used
  • 46. OR
  • 47. A non-unique or non-null key is used mysql> ATER TABLE City ADD INDEX (district); Query OK, 4079 rows affected (0.09 sec)Records: 4079 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM City WHERE District = 'California'; +----+-------------+-------+------+---------------+----------+---------+-------+------+------------ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra +----+-------------+-------+------+---------------+----------+---------+-------+------+------------ | 1 | SIMPLE | City | ref | District | District | 20 | const | 62 | Using where +----+-------------+-------+------+---------------+----------+---------+-------+------+------------ 1 row in set (0.01 sec)
  • 48. ref_or_null Again similar to eq_ref and ref but Allows null values
  • 49. OR
  • 50. Allows null conditions mysql> ALTER TABLE Country ADD INDEX (IndepYear); Query OK, 239 rows affected (0.36 sec) Records: 239 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT IndepYear FROM Country WHERE IndepYear = 1905 or IndepYear IS NULL; +----+-------------+---------+-------------+---------------+-----------+---------+-------+------ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | +----+-------------+---------+-------------+---------------+-----------+---------+-------+------ | 1 | SIMPLE | Country | ref_or_null | IndepYear | IndepYear | 3 | const | 31 | +----+-------------+---------+-------------+---------------+-----------+---------+-------+------ 1 row in set (0.00 sec)
  • 51. index_merge This is the only access type that allows you to use 2 separate indexes for a table with MySQL
  • 52. There are lots of rules of when it will be used and in what conditions – intersection, union http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html mysql> EXPLAIN SELECT * from City where id = 5 or district = 'Michigan'; +----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+ | 1 | SIMPLE | City | index_merge | PRIMARY,District | PRIMARY,District | 4,20 | NULL | 9 | Using union(PRIMARY,District); Using where | +----+-------------+-------+-------------+------------------+------------------+---------+------+------+--------------------------------------------+
  • 53. range Access method for a range value in the where clause
  • 54. (<, <=, >, >=, LIKE, IN or BETWEEN) With LIKE you can use the range access only if the first character is not a wild card character
  • 55. Not possible with hash indexes (MEMORY or NDB tables) mysql> ALTER TABLE City ADD INDEX (population); Query OK, 4079 rows affected (0.10 sec) Records: 4079 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM City WHERE population>10000000; +----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+ | 1 | SIMPLE | City | range | Population | Population | 4 | NULL | 2 | Using where | +----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
  • 56. index Doing an index scan
  • 57. Seen when Getting data is slow...
  • 58. The data being sought is available through the index Different then “Using index” in Extra mysql> EXPLAIN SELECT id FROM City; +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | City | index | NULL | PRIMARY | 4 | NULL | 4079 | Using index | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
  • 60. SLOW mysql> ALTER TABLE City DROP INDEX Population; Query OK, 4079 rows affected (0.06 sec) Records: 4079 Duplicates: 0 Warnings: 0 mysql> EXPLAIN SELECT * FROM City WHERE Population > 10000000; +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | City | ALL | NULL | NULL | NULL | NULL | 4079 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
  • 61. possible_keys Provides a list of the available indexes or NULL that the optimizer considered for the query
  • 62. Gotcha If you have tables/column with different character sets, the indexes on the tables/column may not not be available for lookup since it has to convert from one character set to another.
  • 63. key The actual index that was used for the query or NULL
  • 64. key and possible_keys are where you start looking at your indexing strategy We will briefly discuss optimizing your indexes a bit later in the talk. mysql> EXPLAIN SELECT id FROM City; +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | City | index | NULL | PRIMARY | 4 | NULL | 4079 | Using index | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
  • 65. key_len Shows the number of bytes MySQL will use from the index
  • 66. You can use this to see if the entire index or part of an index is being used for the query.
  • 67. Keep in mind that some character sets can use more then one byte per character.(Ex: UTF8 can use up to 3 bytes per character) mysql> describe City; +-------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | ← INT is 4 bytes
  • 68. ref Very different from the access type “ref”
  • 69. Show which columns or constants within the index that will be used to access the data
  • 70. rows Number of rows MySQL expects to find based on statistics Can be very different then what really shows up You can update these statistics with ANALYZE TABLE but remember that each storage engine updates it's statistics with varying levels of accuracy. Ex: InnoDB statistics are based on 10 random dives rather then on all the actual data
  • 71. filtered New – since 5.1.12
  • 72. indicates an estimated percentage of table rows that will be filtered by the table condition.
  • 73. rows shows the estimated number of rows examined rows × filtered / 100 shows the number of rows that will be joined with previous tables. displayed if you use EXPLAIN EXTENDED.
  • 74. Extra This is where all additional information is located
  • 75. Comments you need know about “Using index” - you are using a covering index (getting data from the index rather then the table)
  • 76. “Using filesort” - manual sorting was done rather then using an index for the sort
  • 77. “Using temporary” - at some point in the execution a temporary table was made. If necessary you want to keep it in RAM (tmp_table_size and max_heap_table_size)
  • 78. “Using where” - filtering outside the storage engine.
  • 79. Indexing Know your data and queries You always have to keep the big picture in mind
  • 80. Data changes and the right index now – may not be the right index later Understand how indexes work in MySQL Leftmost prefix
  • 81. More indexes is not necessarily better
  • 84. Indexing (con't) Shoot for the best EXPLAIN plan Use indexes for sorting
  • 85. Covering indexes Index hinting is possible though highly discouraged If you know absolutely that you should be using a specific index _and_ ANALYZE TABLE is not helping
  • 86. Requires that you maintain that query over time since data changes (can't set it and forget it)
  • 88. also look into STRAIGHT_JOIN
  • 89. Query Optimizing Use the slow query log You can set a maximum time for a query to run and if it goes over, log it (long_query_time) In 5.1 micro time is available You can tell it to log all queries that do not use indexes (log_queries_not_using_indexes)
  • 90. http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html Don't be afraid of sub-selects, but use them wisely Can you write it as a JOIN and if so – does it run faster then the subselect?
  • 91. Query Optimizing (con't) Be willing to consider alternate methods of doing things Instead of one big query with lots of derived tables, use a stored procedure that builds intermittent MEMORY tables that you index and work with Use Query Profiling to get the nitty gritty Available from 5.0
  • 92. There is a lot of information available using profiling
  • 95. Performance Tuning the MySQL Server Ligaya Turmelle MySQL Support Engineer [email_address]