oracle优化聚合,SUM等聚合函数的优化
本帖最后由 dotaddjj 于 2014-8-25 11:38 编辑我个人认为这两个sql没有可比性,首先第一个sql语句select SUM(origin_value) from ATable这个如果这个列上面有索引,走全索引快速扫描是最优秀的,一般而言索引的大小肯定比表小,那么多块读时段的大小会决定cbo选择那种执行方式,一般是全索引快速扫描,至于你这个为什么没有走该列上面的全索引扫描,个人
本帖最后由 dotaddjj 于 2014-8-25 11:38 编辑
我个人认为这两个sql没有可比性,首先第一个sql语句select SUM(origin_value) from ATable这个如果这个列上面有索引,走全索引快速扫描是最优秀的,一般而言索引的大小肯定比表小,那么多块读时段的大小会决定cbo选择那种执行方式,一般是全索引快速扫描,至于你这个为什么没有走该列上面的全索引扫描,个人也比较奇怪,这个对于该列是否有not null约束没影响的,因为null值sum后结果也不会变。
[oracle@dbserver ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Mon Aug 25 10:05:29 2014
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select sum(object_id) from ta;
Execution Plan
----------------------------------------------------------
Plan hash value: 1383641628
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 5 | 48 (3)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 5 | | |
| 2 | INDEX FAST FULL SCAN| IND_ID_NULL | 74906 | 365K| 48 (3)| 00:00:01 |
-------------------------------------------------------------------------------------
select SUM(origin_value) from BTABLE where ATable_id != -1这个sql语句如果单单是origin_value上面的单列索引是无法完成查询的,因为这个索引没办法知道ATable_id这个值的情况,cbo肯定不会考虑成本如何巨大的全索引扫描然后回表方式再去过滤 ATable_id,然后sum求和,如果你建立该列的origin_value和ATable_id的复合索引,那么cbo是会考虑该索引的全索引快速扫描。
SQL> create index ind_multi_objid_name on ta(object_id,object_name);
Index created.
SQL> select sum(object_id) from ta where object_name!='TA';
Execution Plan
----------------------------------------------------------
Plan hash value: 66995668
----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 30 | 117 (1)| 00:00:02 |
| 1 | SORT AGGREGATE | | 1 | 30 | | |
|* 2 | INDEX FAST FULL SCAN| IND_MULTI_OBJID_NAME | 74904 | 2194K| 117 (1)| 00:00:02 |
----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("OBJECT_NAME"<>'TA')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
429 consistent gets
421 physical reads
0 redo size
536 bytes sent via SQL*Net to client
520 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)