group by 不一定排序
如題,以前,每次人家問我sql執行的時候在什麼情況下會出現排序。我的答案中都會含有group by 子句,後來我發現我錯了。
下邊是一個用到group by子句但沒有排序的例子。
SQL> set autot on
SQL> select * from (select min(rownum) from dual where rownum < 10 group by rownum);
MIN(ROWNUM)
-----------
1
Execution Plan
----------------------------------------------------------
Plan hash value: 3333817076
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
| 1 | VIEW | | 1 | 13 | 2 (0)| 00:00:01 |
| 2 | SORT GROUP BY NOSORT| | 1 | | 2 (0)| 00:00:01 |
|* 3 | COUNT STOPKEY | | | | | |
| 4 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter(ROWNUM<10)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
529 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
在執行計劃中的 SORT GROUP BY NOSORT 可以看出沒有排序。
其實,在這裡oracle在執行到group by子句時,發現該處已經是排好序的,故其不在進行sort操作。
下邊是一個用到group by子句但沒有排序的例子。
SQL> set autot on
SQL> select * from (select min(rownum) from dual where rownum < 10 group by rownum);
MIN(ROWNUM)
-----------
1
Execution Plan
----------------------------------------------------------
Plan hash value: 3333817076
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
| 1 | VIEW | | 1 | 13 | 2 (0)| 00:00:01 |
| 2 | SORT GROUP BY NOSORT| | 1 | | 2 (0)| 00:00:01 |
|* 3 | COUNT STOPKEY | | | | | |
| 4 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter(ROWNUM<10)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
529 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
在執行計劃中的 SORT GROUP BY NOSORT 可以看出沒有排序。
其實,在這裡oracle在執行到group by子句時,發現該處已經是排好序的,故其不在進行sort操作。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26143577/viewspace-750073/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- group by 排序問題排序
- group by 需要排序嗎?排序
- SQL 分組排序group bySQL排序
- mysql使用group by實現組內排序實戰MySql排序
- group by排序,derived_merge優化的坑排序優化
- group by排序,derived_merge最佳化的坑排序
- mongodb常用的兩種group方法,以及對結果排序MongoDB排序
- Mysql優化_ORDER BY和GROUP BY 的優化講解(單路排序和雙路排序)MySql優化排序
- oracle文件中關於group by語句不保證排序正確的描述Oracle排序
- Oracle GroupOracle
- group_replication_bootstrap_group 用於什麼boot
- 資料庫的sort group by和hash group by資料庫
- oracle partition by group by,詳解partition by和group by對比Oracle
- IT創業不一定要用程式薦創業
- MySQL Group ReplicationMySql
- Group by 優化優化
- Oracle Hash Group ByOracle
- sql用法——group bySQL
- Oracle group by使用Oracle
- ORA-00937: not a single-group group functionFunction
- 並行HINT並不一定起作用。並行
- oracle全文索引之SECTION GROUP_6_PATH_SECTION_GROUPOracle索引
- oracle全文索引之SECTION GROUP_5_AUTO_SECTION_GROUPOracle索引
- oracle全文索引之SECTION GROUP_4_XML_SECTION_GROUPOracle索引XML
- oracle全文索引之SECTION GROUP_3_HTML_SECTION_GROUPOracle索引HTML
- oracle全文索引之SECTION GROUP_2_BASIC_SECTION_GROUPOracle索引
- oracle全文索引之SECTION GROUP_1_NULL_SECTION_GROUPOracle索引Null
- group by 查詢原理
- Oracle Column Group StatisticsOracle
- ASM Disk Group TemplateASM
- Oracle中group by用法Oracle
- [MySQL] 分組排序取前N條記錄以及生成自動數字序列,類似group by後 limitMySql排序MIT
- 劫持TCP不一定非要是中間人TCP
- 你不一定知道Mac能做的10件事Mac
- GCD使用dispatch_group_notify、dispatch_group_enter、dispatch_group_leave處理多執行緒同步操作GC執行緒
- ERP的Account group和CRM partner group的對映關係
- 排序:氣泡排序&快速排序排序
- GCD 中Group的使用GC