ITPub博客

首页 > Linux操作系统 > Linux操作系统 > group by 需要排序吗?

group by 需要排序吗?

原创 Linux操作系统 作者:lsq_008 时间:2012-09-07 12:54:08 0 删除 编辑
研究了一下,group by是否排序是受隐含参数_gby_hash_aggregation_enabled控制的,如下测试:
SQL> alter system set "_gby_hash_aggregation_enabled"=false;

System altered.

SQL> set autot traceonly
SQL> select object_type,max(object_name) from test group by object_type;

41 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 2603667166

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |   380K|    27M|  1262   (2)| 00:00:16 |
|   1 |  SORT GROUP BY     |      |   380K|    27M|  1262   (2)| 00:00:16 |
|   2 |   TABLE ACCESS FULL| TEST |   380K|    27M|  1250   (1)| 00:00:15 |
---------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement


Statistics
----------------------------------------------------------
          5  recursive calls
          0  db block gets
       5683  consistent gets
          0  physical reads
          0  redo size
       2019  bytes sent via SQL*Net to client
        421  bytes received via SQL*Net from client
          4  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
         41  rows processed

SQL> alter system set "_gby_hash_aggregation_enabled"=true;

System altered.

SQL> select object_type,max(object_name) from test group by object_type;

41 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1435881708

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |  3061K|   224M|  1379  (10)| 00:00:17 |
|   1 |  HASH GROUP BY     |      |  3061K|   224M|  1379  (10)| 00:00:17 |
|   2 |   TABLE ACCESS FULL| TEST |  3061K|   224M|  1273   (3)| 00:00:16 |
---------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
       5612  consistent gets
          0  physical reads
          0  redo size
       1995  bytes sent via SQL*Net to client
        421  bytes received via SQL*Net from client
          4  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         41  rows processed

从10g之后,group by默认采用了hash group by,不再排序了。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10972173/viewspace-742876/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
十余年大型金融及电信系统数据库管理经验,曾服务于中国建设银行、中国移动。对oracle,mysql数据库有深入了解。 擅长python开发,独立开发了开源数据库自动化监控运维平台Power Monitor。

注册时间:2008-02-29

  • 博文量
    335
  • 访问量
    1369103