MySQL分组查询Group By实现原理详解
时间:2019-01-10 分类:MySQL 作者:编程之家
<p style="color:rgb(51,51,51); font-size:14px; margin-top:0px; margin-bottom:0px; padding-top:5px; padding-bottom:5px; font-family:tahoma,arial,宋体; line-height:25.2000007629395px">
由于GROUP BY 实际上也同样会进行排序操作,而且与ORDER BY 相比,GROUP BY 主要只是多了排序之后的分组操作。当然,如果在分组的时候还使用了其他的一些聚合函数,那么还需要一些聚合函数的计算。所以,在GROUP BY 的实现过程中,与 ORDER BY 一样也可以利用到索引。
<p style="color:rgb(51,宋体; line-height:25.2000007629395px">
在<a target="_blank" href="
http://lib.csdn.net/base/mysql " rel="nofollow" class="replace_word" title="MySQL知识库" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold">MySQL 中,GROUP BY 的实现同样有多种(三种)方式,其中有两种方式会利用现有的索引信息来完成 GROUP BY,另外一种为完全无法使用索引的场景下使用。下面我们分别针对这三种实现方式做
一个 分析。
<p style="color:rgb(51,宋体; line-height:25.2000007629395px">
1、使用松散(Loose)索引扫描实现 GROUP BY
<p style="color:rgb(51,宋体; line-height:25.2000007629395px">
何谓松散索引扫描实现 GROUP BY 呢?实际上就是当
MysqL 完全利用索引扫描来实现 GROUP BY 的时候,并不需要扫描所有满足条件的索引键即可完成操作得出结果。
<p style="color:rgb(51,宋体; line-height:25.2000007629395px">
下面我们通过
一个 示例来描述松散索引扫描实现 GROUP BY,在示例之前我们需要首先调整一下 group_message 表的索引,将 gmt_create 字段
添加 到 group_id 和 user_id 字段的索引中:
<div class="jb51code" style="color:rgb(51,51); font-size:14px; margin:0px; padding:0px; line-height:25.2000007629395px; width:660px; overflow:hidden; clear:both; font-family:tahoma,宋体">
<div style="margin:0px; padding:0px; line-height:25.2000007629395px">
<div id="
Highlighter _455865" class="
Syntax Highlighter sql " style="padding:0px; width:644px; line-height:25.2000007629395px; margin:1em 0px!important; position:relative!important; overflow:auto!important">
<div class="toolbar" style="line-height:13.1999998092651px; margin:0px!important; padding:0px!important; border:none!important; b
ott om:auto!important; float:none!important; height:11px!important; left:auto!important; outline:0px!important; overflow:visible!important; position:absolute!important; right:1px!important; top:1px!important; vertical-align:baseline!important; width:11px!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; z-index:10!important; color:white!important; background-color:rgb(108,226,108)!important">
<span style="line-height:21.6000003814697px; font-size:12px"><a target="_blank" href="
http://www.jb51.cc/article/85359.htm #" rel="
nofollow " class="toolbar_item command_help help" style="color:rgb(0,102,153); text-
decoration :none; font-size:14px; border:0px!important; b
ott om:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:1px 0px 0px!important; position:static!important; right:auto!important; text-align:center!important; top:auto!important; vertical-align:baseline!important; width:auto!important;
dis play:block!important">?
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin-right:0px!important; margin-bott om:0px!important; margin-left:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; font-family:Consolas,monospace!important; font-size:1em!important">ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; text-align:left!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important">ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important">
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,monospace!important; font-size:1em!important; color:rgb(175,175,175)!important">
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
1
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
2
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
3
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
4
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
5
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
6
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
7
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
8
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; font-family:Consolas,monospace!important; font-size:1em!important">
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:relative!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important">
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important; font-weight:bold!important; color:rgb(0,153)!important">create
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">index
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">
idx_gid_uid_gc
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">on
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">group_message(group_id,user_id,gmt_create);
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">Query
OK,
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">rows
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">affected
(0.03 sec)
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">Records:
96 Duplicates: 0 Warnings: 0
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">drop
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">
idx_group_message_gid_uid
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">group_message;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,96
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">affected
(0.02 sec)
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">Records:
96 Duplicates: 0 Warnings: 0
Highlighter_196737" class="
Syntax Highlighter sql " style="padding:0px; width:644px; line-height:25.2000007629395px; margin:1em 0px!important; position:relative!important; overflow:auto!important">
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
9
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
10
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
11
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
12
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
13
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
14
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
15
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; text-align:right!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
16
EXPLAIN
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">SELECT
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">user_id,153)!important">max
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">(gmt_create)
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">FROM
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">group_message
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">WHERE
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">group_id
< 10
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">GROUP
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">BY
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">
group_id,user_id\G
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">***************************
1. row ***************************
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">id:
1
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">select_type:
SIMPLE
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">table
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">:
group_message
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">type:
range
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">possible_keys:
idx_gid_uid_gc
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">key
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">:
idx_gid_uid_gc
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">key_len:
8
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">ref:
sql color1" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important; color:gray!important">NULL
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">:
4
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; white-space:pre!important">
sql spaces" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">Extra:
Using
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">where
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">;
Using
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">index
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">for
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">
group
sql plain" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,monospace!important">-
sql keyword" style="margin:3px auto 0px; padding:0px 0px 0px 5px; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,153)!important">by