SlideShare uma empresa Scribd logo
1 de 94
Baixar para ler offline
MySQL Query Optimization


      2010.07.09 Cai Baohua
Agenda
• What the query optimizer is
• The principles of the optimization
• Explain and Profiling
• Use Index
• JOIN Optimization
• ORDER BY, GROUP BY Optimization
MySQL Query Optimizer
MySQl Query Optimizer

               Parser


              Table          Table
Optimizer   Modification   Maintenance   .....
             Module         Module

              Access
              Control
              Module
MySQl Query Optimizer


• Not only CBO but aslo CBO + RBO
 • Cost Base Optimizer
 • Rule Base Optimizer
The Principles Of the
   Optimization
•   Optimizing the query which need more optimization
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Find clear optimization objects
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Find clear optimization objects
•   Start with Explain and use Profile more often
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Find clear optimization objects
•   Start with Explain and use Profile more often
•   Always using the small result set to drive the large result set
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Find clear optimization objects
•   Start with Explain and use Profile more often
•   Always using the small result set to drive the large result set
•   Complete the sequencing in the index as much as possible
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Find clear optimization objects
•   Start with Explain and use Profile more often
•   Always using the small result set to drive the large result set
•   Complete the sequencing in the index as much as possible
•   Fetch the only fields that we need
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Find clear optimization objects
•   Start with Explain and use Profile more often
•   Always using the small result set to drive the large result set
•   Complete the sequencing in the index as much as possible
•   Fetch the only fields that we need
•   Only use the most effective conditions of the filter
•   Optimizing the query which need more optimization
•   Identify the performance bottleneck
•   Find clear optimization objects
•   Start with Explain and use Profile more often
•   Always using the small result set to drive the large result set
•   Complete the sequencing in the index as much as possible
•   Fetch the only fields that we need
•   Only use the most effective conditions of the filter
•   Avoid the complex Join and sub queries as far as possible
Explain and Profiling
Use Explain and Profiling
Explain tells you:
•   In which order the tables are read
•   What types of read operations that are made
•   Which indexes could have been used
•   Which indexes are used
•   How the tables refer to each other
•   How many rows the optimizer estimates to retrieve
    from each table
Use Explain and Profiling
Use Explain and Profiling
Explain Types
Different join types.

system                  !"#$%&'(#$"&)$*+(,$*+#$-*.$
const                   /%$%"#$0*)%$*+#$0&%1"2+3$-*.4$%-#&%#5$&)$&$
                        1*+)%&+%$
eq_ref                  6+#$-*.$7#-$-*.$8-*0$7-#92*:)$%&'(#)$
ref                     ;#9#-&($-*.)$.2%"$0&%1"2+3$2+5#<$9&(:#$
ref_or_null             =2>#$-#84$7(:)$?@==$9&(:#)$
index_merge             ;#9#-&($2+5#<$)#&-1"#)$&-#$0#-3#5$
unique_subquery         ;&0#$&)$-#8$8*-$)*0#$):'A:#-2#)$
index_subquery          /)$&'*9#$8*-$+*+B:+2A:#$2+5#<#)$
range                   /$-&+3#$2+5#<$)1&+$
index                   !"#$."*(#$2+5#<$2)$)1&++#5$
ALL                     /$8:(($%&'(#$)1&+$
Use Explain and Profiling
Explain Types
Different join types.

system                  !"#$%&'(#$"&)$*+(,$*+#$-*.$                   good
const                   /%$%"#$0*)%$*+#$0&%1"2+3$-*.4$%-#&%#5$&)$&$
                        1*+)%&+%$
eq_ref                  6+#$-*.$7#-$-*.$8-*0$7-#92*:)$%&'(#)$
ref                     ;#9#-&($-*.)$.2%"$0&%1"2+3$2+5#<$9&(:#$
ref_or_null             =2>#$-#84$7(:)$?@==$9&(:#)$
index_merge             ;#9#-&($2+5#<$)#&-1"#)$&-#$0#-3#5$
unique_subquery         ;&0#$&)$-#8$8*-$)*0#$):'A:#-2#)$
index_subquery          /)$&'*9#$8*-$+*+B:+2A:#$2+5#<#)$
range                   /$-&+3#$2+5#<$)1&+$
index                   !"#$."*(#$2+5#<$2)$)1&++#5$
ALL                     /$8:(($%&'(#$)1&+$
                                                                      bad
Use Explain and Profiling
Explain Extra
This column contains additional information about how MySQL resolves the query.


Using index                      !"#$%#&'()$*&$+%#,)#-$&)%,*.")$/%01$)"#$*2-#3$

Using where                      40)$,(($%05&$,%#$'&#-$*2$)"#$%#&'()$

Distinct                         62(7$,$&*2.(#$%05$*&$%#,-$8#%$%05$+019*2,:02$
Not exists                       ;$LEFT JOIN$1*&&*2.$%05&$08:1*<,:02$*&$'&#-$

Using filesort                   ;2$#3)%,$%05$&0%:2.$&)#8$*&$-02#$

Using temporary                  ;$)#180%,%7$),9(#$*&$'&#-$

Range checked                    !"#$%#,-$)78#$*&$08:1*<#-$*2-*=*-',((7$/0%$#,+"$
for each record                  +019*2,:02$0/$%05&$/%01$)"#$8%#=*0'&$),9(#&$
Use Explain and Profiling


• Open / Close Query Profiler
     mysql> set profiling = 1 (close: 0)
Use Explain and Profiling
Show profiles
Use Explain and Profiling
 SHOW PROFILE
• ALL - displays all information
• BLOCK IO - displays counts for block input and output operations
• CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches
• IPC - displays counts for messages sent and received
• MEMORY - is not currently implemented
• PAGE FAULTS - displays counts for major and minor page faults
• SOURCE - displays the names of functions from the source code, together with the name and line
  number of the file in which the function occurs
• SWAPS - displays swap count
Use Explain and Profiling
Show more info
Use Index
Index Types
Index Types
•   Balance-Tree
    •   Primary Key
    •   Secondary Index
    •   InnoDB, MyISAM often use
Index Types
•   Balance-Tree
    •   Primary Key
    •   Secondary Index
    •   InnoDB, MyISAM often use
•   Hash
    •   Memory, NDB Cluster
    •   “=”, “IN”, “<=>” not > < between != like
    •   not work for ORDER BY
Index Types
•   Balance-Tree
    •   Primary Key
    •   Secondary Index
    •   InnoDB, MyISAM often use
•   Hash
    •   Memory, NDB Cluster
    •   “=”, “IN”, “<=>” not > < between != like
    •   not work for ORDER BY
•   Fulltext
    •   CHAR,VARCHAR and TEXT
    •   Uses it instead of LIKE ‘%*****%’, more efficient
Index Types
•   Balance-Tree
    •   Primary Key
    •   Secondary Index
    •   InnoDB, MyISAM often use
•   Hash
    •   Memory, NDB Cluster
    •   “=”, “IN”, “<=>” not > < between != like
    •   not work for ORDER BY
•   Fulltext
    •   CHAR,VARCHAR and TEXT
    •   Uses it instead of LIKE ‘%*****%’, more efficient
•   R-Tree
    •   to solve the problem of spatial data retrieval
    •   only data type: GEOMETRY
Pros and Cons of Index
Pros and Cons of Index
•   Pros
    •   Improve the efficiency of data retrieval
    •   Reduce the cost of database I/O
    •   Reduce the cost of data sorting
Pros and Cons of Index
•   Pros
    •   Improve the efficiency of data retrieval
    •   Reduce the cost of database I/O
    •   Reduce the cost of data sorting
•   Cons
    •   index will take more disk space
    •   slow the speed of updating table (insert, update,
        delete)
When Use Index?
When Use Index?
•   Field used in WHERE more frequently, use
    index
When Use Index?
•   Field used in WHERE more frequently, use
    index
•   Field like status or type, no index
When Use Index?
•   Field used in WHERE more frequently, use
    index
•   Field like status or type, no index
    •   Contain too many records records, which
        bring too many random I/O, to many
        duplicate I/O
When Use Index?
•   Field used in WHERE more frequently, use
    index
•   Field like status or type, no index
    •   Contain too many records records, which
        bring too many random I/O, to many
        duplicate I/O
•   Field updated too often, no index
When Use Index?
•   Field used in WHERE more frequently, use
    index
•   Field like status or type, no index
    •   Contain too many records records, which
        bring too many random I/O, to many
        duplicate I/O
•   Field updated too often, no index
•   Field not in WHERE, no index
1 or N-Columns Index
1 or N-Columns Index
•   No absolute conclusion
1 or N-Columns Index
•   No absolute conclusion
•   When a filter field can filter data more than
    90% and the other filter fields will be updated
    often, which we can try to use composite index
1 or N-Columns Index
•   No absolute conclusion
•   When a filter field can filter data more than
    90% and the other filter fields will be updated
    often, which we can try to use composite index
•   Reduce the cost of index updating and disk
    space of index
1 or N-Columns Index
•   No absolute conclusion
•   When a filter field can filter data more than
    90% and the other filter fields will be updated
    often, which we can try to use composite index
•   Reduce the cost of index updating and disk
    space of index
•   let one index used in different quries
1 or N-Columns Index
•   No absolute conclusion
•   When a filter field can filter data more than
    90% and the other filter fields will be updated
    often, which we can try to use composite index
•   Reduce the cost of index updating and disk
    space of index
•   let one index used in different quries
•   Don’t over index
Index Prefixes
Index Prefixes
•   Index prefixes of CHAR,VARCHAR, BINARY,
    VARBINARY, BLOB, and TEXT columns
Index Prefixes
•   Index prefixes of CHAR,VARCHAR, BINARY,
    VARBINARY, BLOB, and TEXT columns
•   name char (200)
Index Prefixes
•   Index prefixes of CHAR,VARCHAR, BINARY,
    VARBINARY, BLOB, and TEXT columns
•   name char (200)
    •   most value are unique within the first 10-20
Index Prefixes
•   Index prefixes of CHAR,VARCHAR, BINARY,
    VARBINARY, BLOB, and TEXT columns
•   name char (200)
    •   most value are unique within the first 10-20
    •   CREATE INDEX part_of_name ON
        customer (name(10));
Index Prefixes
•   Index prefixes of CHAR,VARCHAR, BINARY,
    VARBINARY, BLOB, and TEXT columns
•   name char (200)
    •   most value are unique within the first 10-20
    •   CREATE INDEX part_of_name ON
        customer (name(10));
•   faster query and disk I/O reduction
Limitation of Mysql Index
•   MyISAM - the total length of index <= 1000 bytes
•   BLOB and TEXT only create Index Prefix
•   Mysql not support Function Index
•   “!=” or “<>”, won’t use index
•   abs(column) etc, won’t use index
•   Join (a.city = b.city). If the filter fileds’ type are not the
    same, mysql won’t use index
•   Like ‘%abc’, won’t use index
•   Hash index only can be used when “=”, “<=>”, “IN”
Join
Principle


• Nested Loop Join
Example
users_group(g)
 index ref scan
Nested Loop (ref)
 g.group_id=m.group_id




users_group(g)    group_message(m)
 index ref scan     index ref scan
Nested Loop (ref)           Nested Loop (ref)
 g.group_id=m.group_id         m.id=c.group_msg_id




                                                      Result Set Output
users_group(g)    group_message(m)   group_message_content()
 index ref scan     index ref scan        index ref scan
Ideas for optimization
• Minimize the number of Nested Loop
• Give priority to optimizing the inner loop
• Indexing filter fields
 • ... FROM A, B WHERE B.group_id =
     A.group_id
• Join Buffer size, type is All, index, range,
  index_merge
Order By, Group By
How Satisfy Order By
How Satisfy Order By


• Use Index, without doing a any extra sorting
How Satisfy Order By


• Use Index, without doing a any extra sorting
• Use filesort algorithms
Use Index
Use Index
Use Index
SELECT col1, col2 FROM
                               sort
a ORDER BY [sort]
SELECT col1, col2 FROM
a WHERE colX=value          (colx, sort)
ORDER BY [sort]
SELECT * FROM a
WHERE uid=1 ORDER BY         (uid, x, y)
x, y
SELECT * FROM a
                          won’t use index
ORDER BY YEAR(date)
          ......               ......
Use Index
SELECT col1, col2 FROM
                               sort
a ORDER BY [sort]
SELECT col1, col2 FROM
a WHERE colX=value          (colx, sort)
ORDER BY [sort]
SELECT * FROM a
WHERE uid=1 ORDER BY         (uid, x, y)
x, y
SELECT * FROM a
                          won’t use index
ORDER BY YEAR(date)
          ......               ......
Use Index
SELECT col1, col2 FROM
                               sort
a ORDER BY [sort]
SELECT col1, col2 FROM
a WHERE colX=value          (colx, sort)
ORDER BY [sort]
SELECT * FROM a
WHERE uid=1 ORDER BY         (uid, x, y)
x, y
SELECT * FROM a
                          won’t use index
ORDER BY YEAR(date)
          ......               ......
Use Index
SELECT col1, col2 FROM
                               sort
a ORDER BY [sort]
SELECT col1, col2 FROM
a WHERE colX=value          (colx, sort)
ORDER BY [sort]
SELECT * FROM a
WHERE uid=1 ORDER BY         (uid, x, y)
x, y
SELECT * FROM a
                          won’t use index
ORDER BY YEAR(date)
          ......               ......
Use Index
SELECT col1, col2 FROM
                               sort
a ORDER BY [sort]
SELECT col1, col2 FROM
a WHERE colX=value          (colx, sort)
ORDER BY [sort]
SELECT * FROM a
WHERE uid=1 ORDER BY         (uid, x, y)
x, y
SELECT * FROM a
                          won’t use index
ORDER BY YEAR(date)
          ......               ......
Use Filesort

• increase max_length_for_stort_data
• remove return fields which are not
  necessary
• increase sort_buffer_size
How Satisfy Group By


• Loose Index Scan
• Tight Index Scan
Loose Index Scan
Loose Index Scan
Loose Index Scan
     Conditions                   Example

 The query is over a      SELECT c1, c2 FROM t1
    single table            GROUP BY c1, c2;
only columns that form
a leftmost prefix of the     index on (c1,c2,c3)
  index and no other          •    GROUP BY c1, c2
                              •    CROUP BY c2, c3
       columns.
only can use aggregate
                       SELECT c1, MIN(c2) FROM
 functions like MAX,
                           t1 GROUP BY c1;
         MIN
Loose Index Scan
     Conditions                   Example

 The query is over a      SELECT c1, c2 FROM t1
    single table            GROUP BY c1, c2;
only columns that form
a leftmost prefix of the     index on (c1,c2,c3)
  index and no other          •    GROUP BY c1, c2
                              •    CROUP BY c2, c3
       columns.
only can use aggregate
                       SELECT c1, MIN(c2) FROM
 functions like MAX,
                           t1 GROUP BY c1;
         MIN
Loose Index Scan
     Conditions                   Example

 The query is over a      SELECT c1, c2 FROM t1
    single table            GROUP BY c1, c2;
only columns that form
a leftmost prefix of the     index on (c1,c2,c3)
  index and no other          •    GROUP BY c1, c2
                              •    CROUP BY c2, c3
       columns.
only can use aggregate
                       SELECT c1, MIN(c2) FROM
 functions like MAX,
                           t1 GROUP BY c1;
         MIN
Loose Index Scan
     Conditions                   Example

 The query is over a      SELECT c1, c2 FROM t1
    single table            GROUP BY c1, c2;
only columns that form
a leftmost prefix of the     index on (c1,c2,c3)
  index and no other          •    GROUP BY c1, c2
                              •    CROUP BY c2, c3
       columns.
only can use aggregate
                       SELECT c1, MIN(c2) FROM
 functions like MAX,
                           t1 GROUP BY c1;
         MIN
Loose Index Scan
Loose Index Scan
Loose Index Scan
                            SELECT c1, c2 FROM t1
                              WHERE c1 < const
 Any other parts of the
                              GROUP BY c1, c2;
 index than those from
    the GROUP BY
                            SELECT MAX(c3), MIN
referenced in the query
                             (c3), c1, c2 FROM t1
   must be constants
                              WHERE c2 > const
                              GROUP BY c1, c2;

 Prefix index cannot be       col VARCHAR(20),
used for loose index scan     INDEX (col(10))
Loose Index Scan
                            SELECT c1, c2 FROM t1
                              WHERE c1 < const
 Any other parts of the
                              GROUP BY c1, c2;
 index than those from
    the GROUP BY
                            SELECT MAX(c3), MIN
referenced in the query
                             (c3), c1, c2 FROM t1
   must be constants
                              WHERE c2 > const
                              GROUP BY c1, c2;

 Prefix index cannot be       col VARCHAR(20),
used for loose index scan     INDEX (col(10))
Loose Index Scan


If loose index scan is applicable to a query,
the EXPLAIN output shows Using index for
group-by in the Extra column.
Tight Index Scan
•   MySQL Query Optimizer
    •   If loose index scan are not met, then try
        tight index scan
•   Different with loose, tight
    •   After finding all index keys in WHERE
        conditions, then MySQL do the grouping
        operation
Tight Index Scan
Tight Index Scan
idx(c1,c2,c3) on table t1(c1,c2,c3,c4)
Tight Index Scan
  idx(c1,c2,c3) on table t1(c1,c2,c3,c4)
• A gap in the GROUP BY
   • SELECT c1, c2, c3 FROM t1 WHERE c2 =
     'a' GROUP BY c1, c3;
Tight Index Scan
  idx(c1,c2,c3) on table t1(c1,c2,c3,c4)
• A gap in the GROUP BY
   • SELECT c1, c2, c3 FROM t1 WHERE c2 =
     'a' GROUP BY c1, c3;
• not the first part of the key
   • SELECT c1, c2, c3 FROM t1 WHERE c1 =
     'a' GROUP BY c2, c3;
More...
• Books
 • <<MySQL                        >>,
    Author:

• Web Sites
 • http://dev.mysql.com/doc/refman/5.1/en/
    optimization.html
 • http://www.slideshare.net/
Q and A

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Unit 4 plsql
Unit 4  plsqlUnit 4  plsql
Unit 4 plsql
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
 
MySQL JOIN & UNION
MySQL JOIN & UNIONMySQL JOIN & UNION
MySQL JOIN & UNION
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Redshift performance tuning
Redshift performance tuningRedshift performance tuning
Redshift performance tuning
 
Sql server windowing functions
Sql server windowing functionsSql server windowing functions
Sql server windowing functions
 
8. sql
8. sql8. sql
8. sql
 
Data Modeling
Data ModelingData Modeling
Data Modeling
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
Deep Dive on Amazon Redshift
Deep Dive on Amazon RedshiftDeep Dive on Amazon Redshift
Deep Dive on Amazon Redshift
 
SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Trigger in mysql
Trigger in mysqlTrigger in mysql
Trigger in mysql
 

Destaque

56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
MYXPLAIN
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)
Karthik .P.R
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
Manikanda kumar
 

Destaque (20)

MySQL Performance Optimization #JDNL13
MySQL Performance Optimization #JDNL13MySQL Performance Optimization #JDNL13
MySQL Performance Optimization #JDNL13
 
10 performance tips we tend to forget
10 performance tips we tend to forget10 performance tips we tend to forget
10 performance tips we tend to forget
 
Zurich2007 MySQL Query Optimization
Zurich2007 MySQL Query OptimizationZurich2007 MySQL Query Optimization
Zurich2007 MySQL Query Optimization
 
Building High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic ApplicationsBuilding High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic Applications
 
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
MySQL Query Tuning for the Squeemish -- Fossetcon Orlando Sep 2014
 
56 Query Optimization
56 Query Optimization56 Query Optimization
56 Query Optimization
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
 
Tunning sql query
Tunning sql queryTunning sql query
Tunning sql query
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema Tuning
 
MySQL Query Optimization
MySQL Query OptimizationMySQL Query Optimization
MySQL Query Optimization
 
My sql optimization
My sql optimizationMy sql optimization
My sql optimization
 
Webinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuningWebinar 2013 advanced_query_tuning
Webinar 2013 advanced_query_tuning
 
MySQL Query Optimization.
MySQL Query Optimization.MySQL Query Optimization.
MySQL Query Optimization.
 
Mysql ppt
Mysql pptMysql ppt
Mysql ppt
 
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricksQuery Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)
 
Sql query patterns, optimized
Sql query patterns, optimizedSql query patterns, optimized
Sql query patterns, optimized
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 

Semelhante a Mysql query optimization

Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
Jonathan Levin
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6
MYXPLAIN
 

Semelhante a Mysql query optimization (20)

Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
 
Theming Search Results - How to Make Your Search Results Rock
Theming Search Results - How to Make Your Search Results RockTheming Search Results - How to Make Your Search Results Rock
Theming Search Results - How to Make Your Search Results Rock
 
Mentor Your Indexes
Mentor Your IndexesMentor Your Indexes
Mentor Your Indexes
 
Refactoring
RefactoringRefactoring
Refactoring
 
High Performance Rails with MySQL
High Performance Rails with MySQLHigh Performance Rails with MySQL
High Performance Rails with MySQL
 
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
SqlDay 2018 - Brief introduction into SQL Server Execution PlansSqlDay 2018 - Brief introduction into SQL Server Execution Plans
SqlDay 2018 - Brief introduction into SQL Server Execution Plans
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Introduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]sIntroduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]s
 
MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6MySQL Indexing - Best practices for MySQL 5.6
MySQL Indexing - Best practices for MySQL 5.6
 
Walkthrough Neo4j 1.9 & 2.0
Walkthrough Neo4j 1.9 & 2.0Walkthrough Neo4j 1.9 & 2.0
Walkthrough Neo4j 1.9 & 2.0
 
PostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databasePostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty database
 
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
 
Pl sql best practices document
Pl sql best practices documentPl sql best practices document
Pl sql best practices document
 
Practical my sql performance optimization
Practical my sql performance optimizationPractical my sql performance optimization
Practical my sql performance optimization
 
Statistics and Indexes Internals
Statistics and Indexes InternalsStatistics and Indexes Internals
Statistics and Indexes Internals
 
Database training for developers
Database training for developersDatabase training for developers
Database training for developers
 
MongoDB's New Aggregation framework
MongoDB's New Aggregation frameworkMongoDB's New Aggregation framework
MongoDB's New Aggregation framework
 
Find Anything In Your APEX App - Fuzzy Search with Oracle Text
Find Anything In Your APEX App - Fuzzy Search with Oracle TextFind Anything In Your APEX App - Fuzzy Search with Oracle Text
Find Anything In Your APEX App - Fuzzy Search with Oracle Text
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

Mysql query optimization

  • 1. MySQL Query Optimization 2010.07.09 Cai Baohua
  • 2. Agenda • What the query optimizer is • The principles of the optimization • Explain and Profiling • Use Index • JOIN Optimization • ORDER BY, GROUP BY Optimization
  • 4. MySQl Query Optimizer Parser Table Table Optimizer Modification Maintenance ..... Module Module Access Control Module
  • 5. MySQl Query Optimizer • Not only CBO but aslo CBO + RBO • Cost Base Optimizer • Rule Base Optimizer
  • 6. The Principles Of the Optimization
  • 7.
  • 8. Optimizing the query which need more optimization
  • 9. Optimizing the query which need more optimization • Identify the performance bottleneck
  • 10. Optimizing the query which need more optimization • Identify the performance bottleneck • Find clear optimization objects
  • 11. Optimizing the query which need more optimization • Identify the performance bottleneck • Find clear optimization objects • Start with Explain and use Profile more often
  • 12. Optimizing the query which need more optimization • Identify the performance bottleneck • Find clear optimization objects • Start with Explain and use Profile more often • Always using the small result set to drive the large result set
  • 13. Optimizing the query which need more optimization • Identify the performance bottleneck • Find clear optimization objects • Start with Explain and use Profile more often • Always using the small result set to drive the large result set • Complete the sequencing in the index as much as possible
  • 14. Optimizing the query which need more optimization • Identify the performance bottleneck • Find clear optimization objects • Start with Explain and use Profile more often • Always using the small result set to drive the large result set • Complete the sequencing in the index as much as possible • Fetch the only fields that we need
  • 15. Optimizing the query which need more optimization • Identify the performance bottleneck • Find clear optimization objects • Start with Explain and use Profile more often • Always using the small result set to drive the large result set • Complete the sequencing in the index as much as possible • Fetch the only fields that we need • Only use the most effective conditions of the filter
  • 16. Optimizing the query which need more optimization • Identify the performance bottleneck • Find clear optimization objects • Start with Explain and use Profile more often • Always using the small result set to drive the large result set • Complete the sequencing in the index as much as possible • Fetch the only fields that we need • Only use the most effective conditions of the filter • Avoid the complex Join and sub queries as far as possible
  • 18. Use Explain and Profiling Explain tells you: • In which order the tables are read • What types of read operations that are made • Which indexes could have been used • Which indexes are used • How the tables refer to each other • How many rows the optimizer estimates to retrieve from each table
  • 19. Use Explain and Profiling
  • 20. Use Explain and Profiling Explain Types Different join types. system !"#$%&'(#$"&)$*+(,$*+#$-*.$ const /%$%"#$0*)%$*+#$0&%1"2+3$-*.4$%-#&%#5$&)$&$ 1*+)%&+%$ eq_ref 6+#$-*.$7#-$-*.$8-*0$7-#92*:)$%&'(#)$ ref ;#9#-&($-*.)$.2%"$0&%1"2+3$2+5#<$9&(:#$ ref_or_null =2>#$-#84$7(:)$?@==$9&(:#)$ index_merge ;#9#-&($2+5#<$)#&-1"#)$&-#$0#-3#5$ unique_subquery ;&0#$&)$-#8$8*-$)*0#$):'A:#-2#)$ index_subquery /)$&'*9#$8*-$+*+B:+2A:#$2+5#<#)$ range /$-&+3#$2+5#<$)1&+$ index !"#$."*(#$2+5#<$2)$)1&++#5$ ALL /$8:(($%&'(#$)1&+$
  • 21. Use Explain and Profiling Explain Types Different join types. system !"#$%&'(#$"&)$*+(,$*+#$-*.$ good const /%$%"#$0*)%$*+#$0&%1"2+3$-*.4$%-#&%#5$&)$&$ 1*+)%&+%$ eq_ref 6+#$-*.$7#-$-*.$8-*0$7-#92*:)$%&'(#)$ ref ;#9#-&($-*.)$.2%"$0&%1"2+3$2+5#<$9&(:#$ ref_or_null =2>#$-#84$7(:)$?@==$9&(:#)$ index_merge ;#9#-&($2+5#<$)#&-1"#)$&-#$0#-3#5$ unique_subquery ;&0#$&)$-#8$8*-$)*0#$):'A:#-2#)$ index_subquery /)$&'*9#$8*-$+*+B:+2A:#$2+5#<#)$ range /$-&+3#$2+5#<$)1&+$ index !"#$."*(#$2+5#<$2)$)1&++#5$ ALL /$8:(($%&'(#$)1&+$ bad
  • 22. Use Explain and Profiling Explain Extra This column contains additional information about how MySQL resolves the query. Using index !"#$%#&'()$*&$+%#,)#-$&)%,*.")$/%01$)"#$*2-#3$ Using where 40)$,(($%05&$,%#$'&#-$*2$)"#$%#&'()$ Distinct 62(7$,$&*2.(#$%05$*&$%#,-$8#%$%05$+019*2,:02$ Not exists ;$LEFT JOIN$1*&&*2.$%05&$08:1*<,:02$*&$'&#-$ Using filesort ;2$#3)%,$%05$&0%:2.$&)#8$*&$-02#$ Using temporary ;$)#180%,%7$),9(#$*&$'&#-$ Range checked !"#$%#,-$)78#$*&$08:1*<#-$*2-*=*-',((7$/0%$#,+"$ for each record +019*2,:02$0/$%05&$/%01$)"#$8%#=*0'&$),9(#&$
  • 23. Use Explain and Profiling • Open / Close Query Profiler mysql> set profiling = 1 (close: 0)
  • 24. Use Explain and Profiling Show profiles
  • 25. Use Explain and Profiling SHOW PROFILE • ALL - displays all information • BLOCK IO - displays counts for block input and output operations • CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches • IPC - displays counts for messages sent and received • MEMORY - is not currently implemented • PAGE FAULTS - displays counts for major and minor page faults • SOURCE - displays the names of functions from the source code, together with the name and line number of the file in which the function occurs • SWAPS - displays swap count
  • 26. Use Explain and Profiling Show more info
  • 29. Index Types • Balance-Tree • Primary Key • Secondary Index • InnoDB, MyISAM often use
  • 30. Index Types • Balance-Tree • Primary Key • Secondary Index • InnoDB, MyISAM often use • Hash • Memory, NDB Cluster • “=”, “IN”, “<=>” not > < between != like • not work for ORDER BY
  • 31. Index Types • Balance-Tree • Primary Key • Secondary Index • InnoDB, MyISAM often use • Hash • Memory, NDB Cluster • “=”, “IN”, “<=>” not > < between != like • not work for ORDER BY • Fulltext • CHAR,VARCHAR and TEXT • Uses it instead of LIKE ‘%*****%’, more efficient
  • 32. Index Types • Balance-Tree • Primary Key • Secondary Index • InnoDB, MyISAM often use • Hash • Memory, NDB Cluster • “=”, “IN”, “<=>” not > < between != like • not work for ORDER BY • Fulltext • CHAR,VARCHAR and TEXT • Uses it instead of LIKE ‘%*****%’, more efficient • R-Tree • to solve the problem of spatial data retrieval • only data type: GEOMETRY
  • 33. Pros and Cons of Index
  • 34. Pros and Cons of Index • Pros • Improve the efficiency of data retrieval • Reduce the cost of database I/O • Reduce the cost of data sorting
  • 35. Pros and Cons of Index • Pros • Improve the efficiency of data retrieval • Reduce the cost of database I/O • Reduce the cost of data sorting • Cons • index will take more disk space • slow the speed of updating table (insert, update, delete)
  • 37. When Use Index? • Field used in WHERE more frequently, use index
  • 38. When Use Index? • Field used in WHERE more frequently, use index • Field like status or type, no index
  • 39. When Use Index? • Field used in WHERE more frequently, use index • Field like status or type, no index • Contain too many records records, which bring too many random I/O, to many duplicate I/O
  • 40. When Use Index? • Field used in WHERE more frequently, use index • Field like status or type, no index • Contain too many records records, which bring too many random I/O, to many duplicate I/O • Field updated too often, no index
  • 41. When Use Index? • Field used in WHERE more frequently, use index • Field like status or type, no index • Contain too many records records, which bring too many random I/O, to many duplicate I/O • Field updated too often, no index • Field not in WHERE, no index
  • 42. 1 or N-Columns Index
  • 43. 1 or N-Columns Index • No absolute conclusion
  • 44. 1 or N-Columns Index • No absolute conclusion • When a filter field can filter data more than 90% and the other filter fields will be updated often, which we can try to use composite index
  • 45. 1 or N-Columns Index • No absolute conclusion • When a filter field can filter data more than 90% and the other filter fields will be updated often, which we can try to use composite index • Reduce the cost of index updating and disk space of index
  • 46. 1 or N-Columns Index • No absolute conclusion • When a filter field can filter data more than 90% and the other filter fields will be updated often, which we can try to use composite index • Reduce the cost of index updating and disk space of index • let one index used in different quries
  • 47. 1 or N-Columns Index • No absolute conclusion • When a filter field can filter data more than 90% and the other filter fields will be updated often, which we can try to use composite index • Reduce the cost of index updating and disk space of index • let one index used in different quries • Don’t over index
  • 49. Index Prefixes • Index prefixes of CHAR,VARCHAR, BINARY, VARBINARY, BLOB, and TEXT columns
  • 50. Index Prefixes • Index prefixes of CHAR,VARCHAR, BINARY, VARBINARY, BLOB, and TEXT columns • name char (200)
  • 51. Index Prefixes • Index prefixes of CHAR,VARCHAR, BINARY, VARBINARY, BLOB, and TEXT columns • name char (200) • most value are unique within the first 10-20
  • 52. Index Prefixes • Index prefixes of CHAR,VARCHAR, BINARY, VARBINARY, BLOB, and TEXT columns • name char (200) • most value are unique within the first 10-20 • CREATE INDEX part_of_name ON customer (name(10));
  • 53. Index Prefixes • Index prefixes of CHAR,VARCHAR, BINARY, VARBINARY, BLOB, and TEXT columns • name char (200) • most value are unique within the first 10-20 • CREATE INDEX part_of_name ON customer (name(10)); • faster query and disk I/O reduction
  • 54. Limitation of Mysql Index • MyISAM - the total length of index <= 1000 bytes • BLOB and TEXT only create Index Prefix • Mysql not support Function Index • “!=” or “<>”, won’t use index • abs(column) etc, won’t use index • Join (a.city = b.city). If the filter fileds’ type are not the same, mysql won’t use index • Like ‘%abc’, won’t use index • Hash index only can be used when “=”, “<=>”, “IN”
  • 55. Join
  • 58.
  • 60. Nested Loop (ref) g.group_id=m.group_id users_group(g) group_message(m) index ref scan index ref scan
  • 61. Nested Loop (ref) Nested Loop (ref) g.group_id=m.group_id m.id=c.group_msg_id Result Set Output users_group(g) group_message(m) group_message_content() index ref scan index ref scan index ref scan
  • 62. Ideas for optimization • Minimize the number of Nested Loop • Give priority to optimizing the inner loop • Indexing filter fields • ... FROM A, B WHERE B.group_id = A.group_id • Join Buffer size, type is All, index, range, index_merge
  • 65. How Satisfy Order By • Use Index, without doing a any extra sorting
  • 66. How Satisfy Order By • Use Index, without doing a any extra sorting • Use filesort algorithms
  • 69. Use Index SELECT col1, col2 FROM sort a ORDER BY [sort] SELECT col1, col2 FROM a WHERE colX=value (colx, sort) ORDER BY [sort] SELECT * FROM a WHERE uid=1 ORDER BY (uid, x, y) x, y SELECT * FROM a won’t use index ORDER BY YEAR(date) ...... ......
  • 70. Use Index SELECT col1, col2 FROM sort a ORDER BY [sort] SELECT col1, col2 FROM a WHERE colX=value (colx, sort) ORDER BY [sort] SELECT * FROM a WHERE uid=1 ORDER BY (uid, x, y) x, y SELECT * FROM a won’t use index ORDER BY YEAR(date) ...... ......
  • 71. Use Index SELECT col1, col2 FROM sort a ORDER BY [sort] SELECT col1, col2 FROM a WHERE colX=value (colx, sort) ORDER BY [sort] SELECT * FROM a WHERE uid=1 ORDER BY (uid, x, y) x, y SELECT * FROM a won’t use index ORDER BY YEAR(date) ...... ......
  • 72. Use Index SELECT col1, col2 FROM sort a ORDER BY [sort] SELECT col1, col2 FROM a WHERE colX=value (colx, sort) ORDER BY [sort] SELECT * FROM a WHERE uid=1 ORDER BY (uid, x, y) x, y SELECT * FROM a won’t use index ORDER BY YEAR(date) ...... ......
  • 73. Use Index SELECT col1, col2 FROM sort a ORDER BY [sort] SELECT col1, col2 FROM a WHERE colX=value (colx, sort) ORDER BY [sort] SELECT * FROM a WHERE uid=1 ORDER BY (uid, x, y) x, y SELECT * FROM a won’t use index ORDER BY YEAR(date) ...... ......
  • 74. Use Filesort • increase max_length_for_stort_data • remove return fields which are not necessary • increase sort_buffer_size
  • 75. How Satisfy Group By • Loose Index Scan • Tight Index Scan
  • 78. Loose Index Scan Conditions Example The query is over a SELECT c1, c2 FROM t1 single table GROUP BY c1, c2; only columns that form a leftmost prefix of the index on (c1,c2,c3) index and no other • GROUP BY c1, c2 • CROUP BY c2, c3 columns. only can use aggregate SELECT c1, MIN(c2) FROM functions like MAX, t1 GROUP BY c1; MIN
  • 79. Loose Index Scan Conditions Example The query is over a SELECT c1, c2 FROM t1 single table GROUP BY c1, c2; only columns that form a leftmost prefix of the index on (c1,c2,c3) index and no other • GROUP BY c1, c2 • CROUP BY c2, c3 columns. only can use aggregate SELECT c1, MIN(c2) FROM functions like MAX, t1 GROUP BY c1; MIN
  • 80. Loose Index Scan Conditions Example The query is over a SELECT c1, c2 FROM t1 single table GROUP BY c1, c2; only columns that form a leftmost prefix of the index on (c1,c2,c3) index and no other • GROUP BY c1, c2 • CROUP BY c2, c3 columns. only can use aggregate SELECT c1, MIN(c2) FROM functions like MAX, t1 GROUP BY c1; MIN
  • 81. Loose Index Scan Conditions Example The query is over a SELECT c1, c2 FROM t1 single table GROUP BY c1, c2; only columns that form a leftmost prefix of the index on (c1,c2,c3) index and no other • GROUP BY c1, c2 • CROUP BY c2, c3 columns. only can use aggregate SELECT c1, MIN(c2) FROM functions like MAX, t1 GROUP BY c1; MIN
  • 84. Loose Index Scan SELECT c1, c2 FROM t1 WHERE c1 < const Any other parts of the GROUP BY c1, c2; index than those from the GROUP BY SELECT MAX(c3), MIN referenced in the query (c3), c1, c2 FROM t1 must be constants WHERE c2 > const GROUP BY c1, c2; Prefix index cannot be col VARCHAR(20), used for loose index scan INDEX (col(10))
  • 85. Loose Index Scan SELECT c1, c2 FROM t1 WHERE c1 < const Any other parts of the GROUP BY c1, c2; index than those from the GROUP BY SELECT MAX(c3), MIN referenced in the query (c3), c1, c2 FROM t1 must be constants WHERE c2 > const GROUP BY c1, c2; Prefix index cannot be col VARCHAR(20), used for loose index scan INDEX (col(10))
  • 86. Loose Index Scan If loose index scan is applicable to a query, the EXPLAIN output shows Using index for group-by in the Extra column.
  • 87.
  • 88. Tight Index Scan • MySQL Query Optimizer • If loose index scan are not met, then try tight index scan • Different with loose, tight • After finding all index keys in WHERE conditions, then MySQL do the grouping operation
  • 90. Tight Index Scan idx(c1,c2,c3) on table t1(c1,c2,c3,c4)
  • 91. Tight Index Scan idx(c1,c2,c3) on table t1(c1,c2,c3,c4) • A gap in the GROUP BY • SELECT c1, c2, c3 FROM t1 WHERE c2 = 'a' GROUP BY c1, c3;
  • 92. Tight Index Scan idx(c1,c2,c3) on table t1(c1,c2,c3,c4) • A gap in the GROUP BY • SELECT c1, c2, c3 FROM t1 WHERE c2 = 'a' GROUP BY c1, c3; • not the first part of the key • SELECT c1, c2, c3 FROM t1 WHERE c1 = 'a' GROUP BY c2, c3;
  • 93. More... • Books • <<MySQL >>, Author: • Web Sites • http://dev.mysql.com/doc/refman/5.1/en/ optimization.html • http://www.slideshare.net/