您好,登錄后才能下訂單哦!
這篇文章主要講解了“分組函數group by優化方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“分組函數group by優化方法是什么”吧!
通過實驗來說明怎么對字段為空的group by字段進行優化。
sql語句
SELECT COUNT(*), ACCT_TYPE FROM tb_info GROUP by ACCT_TYPE
order by 1 ;
COUNT(*) ACCT_T
---------- ------
2 304
7 205
8 204
10 802
15 214
22 202
26 211
40 805
238 200
5982
19692 300
COUNT(*) ACCT_T
---------- ------
200761 100
在表上tb_info 創建ACCT_TYPE一般索引
SQL> create index tb_ind04 on tb_info (acct_type);
Index created.
SQL> exec dbms_stats.gather_table_stats(ownname =>'SYS',tabname => 'tb_info',estimate_percent => 10,method_opt=> 'forall indexed columns') ;
PL/SQL procedure successfully completed.
執行計劃如下:
explain plan for
2 SELECT COUNT(*), ACCT_TYPE FROM tb_info GROUPby ACCT_TYPE;
Explained.
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1843165528
------------------------------------------------------------------------------------
| Id |Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECTSTATEMENT | | 8 | 32 | 1737 (2)| 00:00:21 |
| 1 | HASH GROUP BY | | 8 | 32 | 1737 (2)| 00:00:21 |
| 2 | TABLE ACCESS FULL| tb_info | 226K| 883K| 1725 (1)| 00:00:21 |
為什么不走索引?原因在這
238 200
5982
19692 300
此字段有5982個是空值。
增加索引,讓ACCT_TYPE 空值的也保存在索引中
SQL> drop index tb_ind04;
Index dropped.
SQL>
SQL> create index tb_ind04 on tb_info (acct_type,1);
SQL> explain plan for SELECT COUNT(*), ACCT_TYPE FROM tb_info GROUP by ACCT_TYPE;
Explained.
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1399786149
----------------------------------------------------------------------------------
| Id |Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECTSTATEMENT | | 8 | 32 | 169 (9)| 00:00:03 |
| 1 | HASH GROUP BY | | 8 | 32 | 169 (9)| 00:00:03 |
| 2 | INDEX FAST FULL SCAN| TB_IND04 | 226K| 883K| 157 (2)| 00:00:02 |
----------------------------------------------------------------------------------
感謝各位的閱讀,以上就是“分組函數group by優化方法是什么”的內容了,經過本文的學習后,相信大家對分組函數group by優化方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。