SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
1Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
How MySQL handles ORDER
BY, GROUP BY, and DISTINCT
MySQL University Session
November 1, 2007
Sergey Petrunia, sergefp@mysql.com
2Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling ORDER BY
• Available means to produce ordered streams:
– Use an ordered index
• range access
– not with MyISAM/InnoDB's DS-MRR
– not with Falcon
– Has extra (invisible) cost with NDB
• ref access (but not ref-or-null)
results of ref(t.keypart1=const) are ordered by
t.keypart2, t.keypart3, ...
• index access
– Use filesort
3Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Executing join and producing ordered stream
There are three ways to produce ordered join output
Method EXPLAIN shows
Use an ordered index Nothing particular
Use filesort() on 1st non-constant table “Using filesort” in the first row
Put join result into a temporary table
and use filesort() on it
“Using temporary; Using filesort” in the
first row
EXPLAIN is a bit counterintuitive:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 range a a 5 NULL 10
Using where;
Using temporary;
Using filesort
1 SIMPLE t2a ref a a 5 t2.b 1 Using where
4Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Using index to produce ordered join result
• ORDER BY must use
columns from one index
• DESC is ok if it is present
for all columns
• Equality propagation:
– “a=b AND b=const” is
detected
– “WHERE x=t.key ORDER
BY x” is not
• Cannot use join buffering
– Use of matching join order
disables use of join
buffering.
5Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Use filesort on first non-const table
• Same as previous
method, but use filesort
instead of an the index
– All same properties
• Condition on 1st table is
checked before filesort
• But LIMIT can't be
“pushed down”
• In the code: see
JOIN::simple_order,
simple_group
6Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Use filesort for the entire join output
• This is the catch-all method
– Places no limit on join order, use of join buffering, etc
• Works only for *all* tables (can't filesort {t1,t2})
• LIMIT is not pushed down to before filesort
7Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
A more detailed look at filesort
• filesort() operation
– Do one in-memory qsort if we have enough space
– Otherwise sort in chunks and then merge them until we get
one sorted sequence.
• filesort() modes
– Sort tuples that contain all required columns from the source
table
– Sort <sort_key, rowid> pairs, return a sequence of source
table's rowids
• One will need to do a separate pass where the rows are read in
sort (not disk) order
EXPLAIN does not tell which mode is used, need to use some
indirect evidence like Handler_XXX counters.
8Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
ORDER BY and the join optimizer
A proper approach:
• Have cost of sorting
• Let the optimizer take it into account:
query_cost= cost(join_execution) + need_sort? cost_of_sort : 0
• But this is not implemented yet
• Implementation challenges
– Don't know condition selectivity ->
– Will need to do smart choice
whether to use join buffering
• Solution scheme:
– Produce best join order ignoring the need to sort
– Produce best join order that doesn't require sorting
– Compare those three
9Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
ORDER BY and the join optimizer (2)
Currently we have:
• No formula to get cost of sorting
• Join order choice doesn't properly account for cost of
sorting
– Makes an trivial attempt, grep for “have to make a temp
table” in best_extension_by_limited_search()
• this can misfire, see BUG#XXXX
• Once the join order is fixed,
we consider changing access
method for 1st table to produce
required ordering. The choice
is cost-based
10Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling GROUP BY
General approach to processing GROUP BY
reset all aggregate functions;
for each join record combination (*)
{
group= {find out the group this record combination falls into}; (**)
update aggregate functions;
}
for each group
{
get aggregate function values;
if (HAVING clause is satisfied)
pass the group row for further processing;
}
11Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Available GROUP BY strategies
There are three
• Ordered Index Scan
execute the join in a way that record combinations are
grouped together
• Loose Index Scan
handles a special case when there is only one table we
need only one row for each group
• Groups table
Execute join so we get groups intermixed and use temp.
table to find the group.
12Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling GROUP BY with ordered index scan
• Groups are processed one after
another
• ORDER BY group_by_list
is guaranteed for free
• Codewise:
– Join result is captured with
end_send_group()
– Still uses some parts of
temptable-based solution (e.g.
join->tmp_table_param)
Because it needs two buffers
• Current join record combination
• GROUP values for group being
examined
13Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
GROUP BY / Loose Index Scan
14Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
GROUP BY / Loose Index Scan (2)
• Limited applicability
– Join must have only one table
– Only MIN(col)/MAX(col) functions are allowed
– The index must cover
• GROUP BY columns
• Several equality columns (optional)
• MIN/MAX column(s)
• Low-cardinality indexes are good
(contrary to other access)
• Codewise
– It's an access method
– Uses QUICK_GROUP_MIN_MAX_SELECT
– Produces grouped result right away, doesn't need to capture
join output
15Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling GROUP BY with groups table
16Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling GROUP BY with groups table (2)
• Groups table has
– GROUP BY columns
• with unique index
– Aggregate function columns
• This method handles only O(1) data per group aggregates
• Group table properties
– HEAP table with HASH key by default
• Auto-converted to MyISAM when full
– MyISAM table when HEAP cannot be used
• Regular BTREE key by default
• Unique constraint (key on hash value) otherwise
• Join results captured by end_update(), which
– Finds the group in the temp table
– Updates the group values
17Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
GROUP BY special cases
• Constants are removed from GROUP list
• GROUP BY all_unique_key_columns is detected and
removed
• SELECT MIN(key), MAX(key) without GROUP BY
can/is resolved with one index lookup in certain cases
• We don't take advantage of HASH indexes for GROUP
BY, although we could do it when using range access
over HASH indexes (we don't get ordering but get
grouping)
18Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling DISTINCT aggregates
• aggregate_func(DISTINCT ...)
needs to store O(#groupsize)
data per group
• Which can't be done efficiently
with group table
• Therefore, we do filesort pass
first so we enumerate groups
one after another
• DISTINCT group functions
accumulate data in UNIQUE
objects
(i.e. BTREEs which can be flushed
to disk and then merged to get an
ordered distinct sequence)
19Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling DISTINCT aggregates (2)
Loose Index Scan can be used for DISTINCT aggregates:
SELECT COUNT(DISTINCT t.key) FROM t1;
SELECT COUNT(DISTINCT key1part1)
FROM t1 GROUP BY key1part1, key1part2;
• Can handle GROUP BY iff aggregate's list is a prefix of it
• Have limitations on select list and WHERE clause
– similar to those of GROUP BY's LooseIndexScan
• We've started to implement it, see WL#3220
– Can provide speedups for low-cardinality indexes
– Slower than index scan for high cardinality indexes
• Need cost model to decide when to use LooseIndexScan
• Need storage engines to support efficient “jump forward” function
20Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Handling SELECT DISTINCT
• The task is similar to GROUP BY
– Need to detect groups and pick one record from every
groups
– Therefore handled by the same code
• May be converted to GROUP BY's internal data structure
• As with GROUP BY, trivial cases are detected and
removed
– Constants in select list
– GROUP BY and DISTINCT on the same columns w/o
aggregates
• Unlike GROUP BY, DISTINCT doesn't necessarily
produce ordered stream
21Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
SELECT DISTINCT in EXPLAIN
• One “Using temporary” even if it actually uses several
temporary tables
explain select distinct col1 from t1;
explain
select distinct max(col1) from t1 group by col2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULLNULL NULL 4 Using temporary
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4
Using temporary;
Using filesort
22Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Our plans for GROUP BY/ORDER BY
• WL#3220 Loose index scan for aggregate functions
• BUG#29443 make the join optimizer a *little* bit smarter
about the costs (but not a full solution)
23Copyright 2007 MySQL AB The World’s Most Popular Open Source Database
Questions
Thank you!

Mais conteúdo relacionado

Mais procurados

10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQLSatoshi Nagayasu
 
Redis深入浅出
Redis深入浅出Redis深入浅出
Redis深入浅出iammutex
 
Caching solutions with Redis
Caching solutions   with RedisCaching solutions   with Redis
Caching solutions with RedisGeorge Platon
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with ModulesItamar Haber
 
A simple introduction to redis
A simple introduction to redisA simple introduction to redis
A simple introduction to redisZhichao Liang
 
Redis overview for Software Architecture Forum
Redis overview for Software Architecture ForumRedis overview for Software Architecture Forum
Redis overview for Software Architecture ForumChristopher Spring
 
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...DataStax
 
Boosting Machine Learning with Redis Modules and Spark
Boosting Machine Learning with Redis Modules and SparkBoosting Machine Learning with Redis Modules and Spark
Boosting Machine Learning with Redis Modules and SparkDvir Volk
 
[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...
[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...
[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...Insight Technology, Inc.
 
Базы данных. HDFS
Базы данных. HDFSБазы данных. HDFS
Базы данных. HDFSVadim Tsesko
 
What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6EDB
 
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 TaipeiPostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 TaipeiSatoshi Nagayasu
 
An Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL databaseAn Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL databaseAli MasudianPour
 
Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...
Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...
Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...Ontico
 
Как PostgreSQL работает с диском
Как PostgreSQL работает с дискомКак PostgreSQL работает с диском
Как PostgreSQL работает с дискомPostgreSQL-Consulting
 
Introduction to redis - version 2
Introduction to redis - version 2Introduction to redis - version 2
Introduction to redis - version 2Dvir Volk
 
Mysql Fulltext Search 1
Mysql Fulltext Search 1Mysql Fulltext Search 1
Mysql Fulltext Search 1johnymas
 

Mais procurados (20)

10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL
 
Redis深入浅出
Redis深入浅出Redis深入浅出
Redis深入浅出
 
Caching solutions with Redis
Caching solutions   with RedisCaching solutions   with Redis
Caching solutions with Redis
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
SphinxSE with MySQL
SphinxSE with MySQLSphinxSE with MySQL
SphinxSE with MySQL
 
Extend Redis with Modules
Extend Redis with ModulesExtend Redis with Modules
Extend Redis with Modules
 
A simple introduction to redis
A simple introduction to redisA simple introduction to redis
A simple introduction to redis
 
Redis overview for Software Architecture Forum
Redis overview for Software Architecture ForumRedis overview for Software Architecture Forum
Redis overview for Software Architecture Forum
 
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
 
Boosting Machine Learning with Redis Modules and Spark
Boosting Machine Learning with Redis Modules and SparkBoosting Machine Learning with Redis Modules and Spark
Boosting Machine Learning with Redis Modules and Spark
 
[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...
[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...
[db tech showcase Tokyo 2017] A11: SQLite - The most used yet least appreciat...
 
Базы данных. HDFS
Базы данных. HDFSБазы данных. HDFS
Базы данных. HDFS
 
What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6
 
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 TaipeiPostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
PostgreSQL 9.4, 9.5 and Beyond @ COSCUP 2015 Taipei
 
An Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL databaseAn Introduction to REDIS NoSQL database
An Introduction to REDIS NoSQL database
 
Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...
Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...
Новые возможности полнотекстового поиска в PostgreSQL / Олег Бартунов (Postgr...
 
Как PostgreSQL работает с диском
Как PostgreSQL работает с дискомКак PostgreSQL работает с диском
Как PostgreSQL работает с диском
 
Redis introduction
Redis introductionRedis introduction
Redis introduction
 
Introduction to redis - version 2
Introduction to redis - version 2Introduction to redis - version 2
Introduction to redis - version 2
 
Mysql Fulltext Search 1
Mysql Fulltext Search 1Mysql Fulltext Search 1
Mysql Fulltext Search 1
 

Semelhante a How mysql handles ORDER BY, GROUP BY, and DISTINCT

My sql technical reference manual
My sql technical reference manualMy sql technical reference manual
My sql technical reference manualMir Majid
 
Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...
Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...
Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...Ontico
 
Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespaceMarco Tusa
 
8. column oriented databases
8. column oriented databases8. column oriented databases
8. column oriented databasesFabio Fumarola
 
SQL Server 2014 Memory Optimised Tables - Advanced
SQL Server 2014 Memory Optimised Tables - AdvancedSQL Server 2014 Memory Optimised Tables - Advanced
SQL Server 2014 Memory Optimised Tables - AdvancedTony Rogerson
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytWrushabhShirsat3
 
Aioug vizag oracle12c_new_features
Aioug vizag oracle12c_new_featuresAioug vizag oracle12c_new_features
Aioug vizag oracle12c_new_featuresAiougVizagChapter
 
database-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptdatabase-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptIftikhar70
 
database-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptdatabase-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptsubbu998029
 
Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...
Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...
Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...Databricks
 
Skillwise - Enhancing dotnet app
Skillwise - Enhancing dotnet appSkillwise - Enhancing dotnet app
Skillwise - Enhancing dotnet appSkillwise Group
 
Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2PoguttuezhiniVP
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...Lucidworks
 
Java class 8
Java class 8Java class 8
Java class 8Edureka!
 

Semelhante a How mysql handles ORDER BY, GROUP BY, and DISTINCT (20)

My sql technical reference manual
My sql technical reference manualMy sql technical reference manual
My sql technical reference manual
 
Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...
Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...
Columnar Table Performance Enhancements Of Greenplum Database with Block Meta...
 
Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespace
 
Fudcon talk.ppt
Fudcon talk.pptFudcon talk.ppt
Fudcon talk.ppt
 
8. column oriented databases
8. column oriented databases8. column oriented databases
8. column oriented databases
 
SQL Server 2014 Memory Optimised Tables - Advanced
SQL Server 2014 Memory Optimised Tables - AdvancedSQL Server 2014 Memory Optimised Tables - Advanced
SQL Server 2014 Memory Optimised Tables - Advanced
 
Intro to column stores
Intro to column storesIntro to column stores
Intro to column stores
 
unit-ii.pptx
unit-ii.pptxunit-ii.pptx
unit-ii.pptx
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
 
The Google Bigtable
The Google BigtableThe Google Bigtable
The Google Bigtable
 
Aioug vizag oracle12c_new_features
Aioug vizag oracle12c_new_featuresAioug vizag oracle12c_new_features
Aioug vizag oracle12c_new_features
 
database-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptdatabase-stucture-and-space-managment.ppt
database-stucture-and-space-managment.ppt
 
database-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptdatabase-stucture-and-space-managment.ppt
database-stucture-and-space-managment.ppt
 
2. Lecture2_NOSQL_KeyValue.ppt
2. Lecture2_NOSQL_KeyValue.ppt2. Lecture2_NOSQL_KeyValue.ppt
2. Lecture2_NOSQL_KeyValue.ppt
 
Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...
Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...
Lessons from the Field: Applying Best Practices to Your Apache Spark Applicat...
 
Skillwise - Enhancing dotnet app
Skillwise - Enhancing dotnet appSkillwise - Enhancing dotnet app
Skillwise - Enhancing dotnet app
 
Postgresql Database Administration Basic - Day2
Postgresql  Database Administration Basic  - Day2Postgresql  Database Administration Basic  - Day2
Postgresql Database Administration Basic - Day2
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
 
Java class 8
Java class 8Java class 8
Java class 8
 

Mais de Sergey Petrunya

New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12Sergey Petrunya
 
MariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixesMariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixesSergey Petrunya
 
Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8Sergey Petrunya
 
Improving MariaDB’s Query Optimizer with better selectivity estimates
Improving MariaDB’s Query Optimizer with better selectivity estimatesImproving MariaDB’s Query Optimizer with better selectivity estimates
Improving MariaDB’s Query Optimizer with better selectivity estimatesSergey Petrunya
 
JSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger pictureJSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger pictureSergey Petrunya
 
Optimizer Trace Walkthrough
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace WalkthroughSergey Petrunya
 
ANALYZE for Statements - MariaDB's hidden gem
ANALYZE for Statements - MariaDB's hidden gemANALYZE for Statements - MariaDB's hidden gem
ANALYZE for Statements - MariaDB's hidden gemSergey Petrunya
 
Optimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesSergey Petrunya
 
MariaDB 10.4 - что нового
MariaDB 10.4 - что новогоMariaDB 10.4 - что нового
MariaDB 10.4 - что новогоSergey Petrunya
 
Using histograms to get better performance
Using histograms to get better performanceUsing histograms to get better performance
Using histograms to get better performanceSergey Petrunya
 
MariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit holeMariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit holeSergey Petrunya
 
Query Optimizer in MariaDB 10.4
Query Optimizer in MariaDB 10.4Query Optimizer in MariaDB 10.4
Query Optimizer in MariaDB 10.4Sergey Petrunya
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkSergey Petrunya
 
MariaDB 10.3 Optimizer - where does it stand
MariaDB 10.3 Optimizer - where does it standMariaDB 10.3 Optimizer - where does it stand
MariaDB 10.3 Optimizer - where does it standSergey Petrunya
 
MyRocks in MariaDB | M18
MyRocks in MariaDB | M18MyRocks in MariaDB | M18
MyRocks in MariaDB | M18Sergey Petrunya
 
New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3Sergey Petrunya
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLSergey Petrunya
 
Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Sergey Petrunya
 

Mais de Sergey Petrunya (20)

New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12New optimizer features in MariaDB releases before 10.12
New optimizer features in MariaDB releases before 10.12
 
MariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixesMariaDB's join optimizer: how it works and current fixes
MariaDB's join optimizer: how it works and current fixes
 
Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8Improved histograms in MariaDB 10.8
Improved histograms in MariaDB 10.8
 
Improving MariaDB’s Query Optimizer with better selectivity estimates
Improving MariaDB’s Query Optimizer with better selectivity estimatesImproving MariaDB’s Query Optimizer with better selectivity estimates
Improving MariaDB’s Query Optimizer with better selectivity estimates
 
JSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger pictureJSON Support in MariaDB: News, non-news and the bigger picture
JSON Support in MariaDB: News, non-news and the bigger picture
 
Optimizer Trace Walkthrough
Optimizer Trace WalkthroughOptimizer Trace Walkthrough
Optimizer Trace Walkthrough
 
ANALYZE for Statements - MariaDB's hidden gem
ANALYZE for Statements - MariaDB's hidden gemANALYZE for Statements - MariaDB's hidden gem
ANALYZE for Statements - MariaDB's hidden gem
 
Optimizer features in recent releases of other databases
Optimizer features in recent releases of other databasesOptimizer features in recent releases of other databases
Optimizer features in recent releases of other databases
 
MariaDB 10.4 - что нового
MariaDB 10.4 - что новогоMariaDB 10.4 - что нового
MariaDB 10.4 - что нового
 
Using histograms to get better performance
Using histograms to get better performanceUsing histograms to get better performance
Using histograms to get better performance
 
MariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit holeMariaDB Optimizer - further down the rabbit hole
MariaDB Optimizer - further down the rabbit hole
 
Query Optimizer in MariaDB 10.4
Query Optimizer in MariaDB 10.4Query Optimizer in MariaDB 10.4
Query Optimizer in MariaDB 10.4
 
Lessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmarkLessons for the optimizer from running the TPC-DS benchmark
Lessons for the optimizer from running the TPC-DS benchmark
 
MariaDB 10.3 Optimizer - where does it stand
MariaDB 10.3 Optimizer - where does it standMariaDB 10.3 Optimizer - where does it stand
MariaDB 10.3 Optimizer - where does it stand
 
MyRocks in MariaDB | M18
MyRocks in MariaDB | M18MyRocks in MariaDB | M18
MyRocks in MariaDB | M18
 
New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3New Query Optimizer features in MariaDB 10.3
New Query Optimizer features in MariaDB 10.3
 
MyRocks in MariaDB
MyRocks in MariaDBMyRocks in MariaDB
MyRocks in MariaDB
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQL
 
Say Hello to MyRocks
Say Hello to MyRocksSay Hello to MyRocks
Say Hello to MyRocks
 
Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2Common Table Expressions in MariaDB 10.2
Common Table Expressions in MariaDB 10.2
 

Último

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 

Último (20)

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 

How mysql handles ORDER BY, GROUP BY, and DISTINCT

  • 1. 1Copyright 2007 MySQL AB The World’s Most Popular Open Source Database How MySQL handles ORDER BY, GROUP BY, and DISTINCT MySQL University Session November 1, 2007 Sergey Petrunia, sergefp@mysql.com
  • 2. 2Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling ORDER BY • Available means to produce ordered streams: – Use an ordered index • range access – not with MyISAM/InnoDB's DS-MRR – not with Falcon – Has extra (invisible) cost with NDB • ref access (but not ref-or-null) results of ref(t.keypart1=const) are ordered by t.keypart2, t.keypart3, ... • index access – Use filesort
  • 3. 3Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Executing join and producing ordered stream There are three ways to produce ordered join output Method EXPLAIN shows Use an ordered index Nothing particular Use filesort() on 1st non-constant table “Using filesort” in the first row Put join result into a temporary table and use filesort() on it “Using temporary; Using filesort” in the first row EXPLAIN is a bit counterintuitive: id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t2 range a a 5 NULL 10 Using where; Using temporary; Using filesort 1 SIMPLE t2a ref a a 5 t2.b 1 Using where
  • 4. 4Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Using index to produce ordered join result • ORDER BY must use columns from one index • DESC is ok if it is present for all columns • Equality propagation: – “a=b AND b=const” is detected – “WHERE x=t.key ORDER BY x” is not • Cannot use join buffering – Use of matching join order disables use of join buffering.
  • 5. 5Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Use filesort on first non-const table • Same as previous method, but use filesort instead of an the index – All same properties • Condition on 1st table is checked before filesort • But LIMIT can't be “pushed down” • In the code: see JOIN::simple_order, simple_group
  • 6. 6Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Use filesort for the entire join output • This is the catch-all method – Places no limit on join order, use of join buffering, etc • Works only for *all* tables (can't filesort {t1,t2}) • LIMIT is not pushed down to before filesort
  • 7. 7Copyright 2007 MySQL AB The World’s Most Popular Open Source Database A more detailed look at filesort • filesort() operation – Do one in-memory qsort if we have enough space – Otherwise sort in chunks and then merge them until we get one sorted sequence. • filesort() modes – Sort tuples that contain all required columns from the source table – Sort <sort_key, rowid> pairs, return a sequence of source table's rowids • One will need to do a separate pass where the rows are read in sort (not disk) order EXPLAIN does not tell which mode is used, need to use some indirect evidence like Handler_XXX counters.
  • 8. 8Copyright 2007 MySQL AB The World’s Most Popular Open Source Database ORDER BY and the join optimizer A proper approach: • Have cost of sorting • Let the optimizer take it into account: query_cost= cost(join_execution) + need_sort? cost_of_sort : 0 • But this is not implemented yet • Implementation challenges – Don't know condition selectivity -> – Will need to do smart choice whether to use join buffering • Solution scheme: – Produce best join order ignoring the need to sort – Produce best join order that doesn't require sorting – Compare those three
  • 9. 9Copyright 2007 MySQL AB The World’s Most Popular Open Source Database ORDER BY and the join optimizer (2) Currently we have: • No formula to get cost of sorting • Join order choice doesn't properly account for cost of sorting – Makes an trivial attempt, grep for “have to make a temp table” in best_extension_by_limited_search() • this can misfire, see BUG#XXXX • Once the join order is fixed, we consider changing access method for 1st table to produce required ordering. The choice is cost-based
  • 10. 10Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling GROUP BY General approach to processing GROUP BY reset all aggregate functions; for each join record combination (*) { group= {find out the group this record combination falls into}; (**) update aggregate functions; } for each group { get aggregate function values; if (HAVING clause is satisfied) pass the group row for further processing; }
  • 11. 11Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Available GROUP BY strategies There are three • Ordered Index Scan execute the join in a way that record combinations are grouped together • Loose Index Scan handles a special case when there is only one table we need only one row for each group • Groups table Execute join so we get groups intermixed and use temp. table to find the group.
  • 12. 12Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling GROUP BY with ordered index scan • Groups are processed one after another • ORDER BY group_by_list is guaranteed for free • Codewise: – Join result is captured with end_send_group() – Still uses some parts of temptable-based solution (e.g. join->tmp_table_param) Because it needs two buffers • Current join record combination • GROUP values for group being examined
  • 13. 13Copyright 2007 MySQL AB The World’s Most Popular Open Source Database GROUP BY / Loose Index Scan
  • 14. 14Copyright 2007 MySQL AB The World’s Most Popular Open Source Database GROUP BY / Loose Index Scan (2) • Limited applicability – Join must have only one table – Only MIN(col)/MAX(col) functions are allowed – The index must cover • GROUP BY columns • Several equality columns (optional) • MIN/MAX column(s) • Low-cardinality indexes are good (contrary to other access) • Codewise – It's an access method – Uses QUICK_GROUP_MIN_MAX_SELECT – Produces grouped result right away, doesn't need to capture join output
  • 15. 15Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling GROUP BY with groups table
  • 16. 16Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling GROUP BY with groups table (2) • Groups table has – GROUP BY columns • with unique index – Aggregate function columns • This method handles only O(1) data per group aggregates • Group table properties – HEAP table with HASH key by default • Auto-converted to MyISAM when full – MyISAM table when HEAP cannot be used • Regular BTREE key by default • Unique constraint (key on hash value) otherwise • Join results captured by end_update(), which – Finds the group in the temp table – Updates the group values
  • 17. 17Copyright 2007 MySQL AB The World’s Most Popular Open Source Database GROUP BY special cases • Constants are removed from GROUP list • GROUP BY all_unique_key_columns is detected and removed • SELECT MIN(key), MAX(key) without GROUP BY can/is resolved with one index lookup in certain cases • We don't take advantage of HASH indexes for GROUP BY, although we could do it when using range access over HASH indexes (we don't get ordering but get grouping)
  • 18. 18Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling DISTINCT aggregates • aggregate_func(DISTINCT ...) needs to store O(#groupsize) data per group • Which can't be done efficiently with group table • Therefore, we do filesort pass first so we enumerate groups one after another • DISTINCT group functions accumulate data in UNIQUE objects (i.e. BTREEs which can be flushed to disk and then merged to get an ordered distinct sequence)
  • 19. 19Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling DISTINCT aggregates (2) Loose Index Scan can be used for DISTINCT aggregates: SELECT COUNT(DISTINCT t.key) FROM t1; SELECT COUNT(DISTINCT key1part1) FROM t1 GROUP BY key1part1, key1part2; • Can handle GROUP BY iff aggregate's list is a prefix of it • Have limitations on select list and WHERE clause – similar to those of GROUP BY's LooseIndexScan • We've started to implement it, see WL#3220 – Can provide speedups for low-cardinality indexes – Slower than index scan for high cardinality indexes • Need cost model to decide when to use LooseIndexScan • Need storage engines to support efficient “jump forward” function
  • 20. 20Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Handling SELECT DISTINCT • The task is similar to GROUP BY – Need to detect groups and pick one record from every groups – Therefore handled by the same code • May be converted to GROUP BY's internal data structure • As with GROUP BY, trivial cases are detected and removed – Constants in select list – GROUP BY and DISTINCT on the same columns w/o aggregates • Unlike GROUP BY, DISTINCT doesn't necessarily produce ordered stream
  • 21. 21Copyright 2007 MySQL AB The World’s Most Popular Open Source Database SELECT DISTINCT in EXPLAIN • One “Using temporary” even if it actually uses several temporary tables explain select distinct col1 from t1; explain select distinct max(col1) from t1 group by col2; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULLNULL NULL 4 Using temporary id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
  • 22. 22Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Our plans for GROUP BY/ORDER BY • WL#3220 Loose index scan for aggregate functions • BUG#29443 make the join optimizer a *little* bit smarter about the costs (but not a full solution)
  • 23. 23Copyright 2007 MySQL AB The World’s Most Popular Open Source Database Questions Thank you!