楼主: 暗纹河豚毒

[SQL] 分组求max,min速度很快,但是求max-min速度很慢

[复制链接]
论坛徽章:
1088
金色在线徽章
日期:2007-04-25 04:02:08金色在线徽章
日期:2007-06-29 04:02:43金色在线徽章
日期:2007-03-11 04:02:02在线时间
日期:2007-04-11 04:01:02在线时间
日期:2007-04-12 04:01:02在线时间
日期:2007-03-07 04:01:022008版在线时间
日期:2010-05-01 00:01:152008版在线时间
日期:2011-05-01 00:01:342008版在线时间
日期:2008-06-03 11:59:43ITPUB年度最佳技术原创精华奖
日期:2013-03-22 13:18:30
41#
发表于 2013-9-9 22:42 | 只看该作者
newkid 发表于 2013-9-9 22:31
这个以前有过定论的吧,必须分开来写。

他这个不是index min/max那计划

使用道具 举报

回复
求职 : 数据库开发
论坛徽章:
4
林肯
日期:2013-07-30 18:00:55奔驰
日期:2013-08-04 16:30:37奥迪
日期:2013-08-12 12:28:15阿斯顿马丁
日期:2014-01-09 18:07:50
42#
发表于 2013-9-9 22:51 | 只看该作者
with a as (
select b.elec_meter_id elec_meter_id,
       max(b.total_used) max_used,
       min(b.total_used) min_used
  from bigdata b,
       (select distinct e.meter_id
          from elec_meter_info e,
               (select org_id
                  from org_info
                 where super_org_id = '52ac306a38755fcf01388e69195c007e') o
         where e.is_delete = 'N'
           and e.is_disable = 'N'
           and e.is_init > 0
           and e.org_stat_area = 0
           and e.org_id = o.org_id) m
where b.treate_date >= '2013-01-01 00:00:00'
   and b.treate_date <= '2014-01-01 00:00:00'
   and b.elec_meter_id = m.meter_id
group by b.elec_meter_id)
select a.elec_meter_id,max_used-min_used from a;
这样呢?

使用道具 举报

回复
论坛徽章:
484
ITPUB北京香山2007年会纪念徽章
日期:2007-01-24 14:35:02ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24ITPUB北京2009年会纪念徽章
日期:2009-02-09 11:42:452010新春纪念徽章
日期:2010-03-01 11:04:552010数据库技术大会纪念徽章
日期:2010-05-13 10:04:272010系统架构师大会纪念
日期:2010-09-04 13:35:54ITPUB9周年纪念徽章
日期:2010-10-08 09:28:512011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:54
43#
发表于 2013-9-10 00:23 | 只看该作者
b.treate_date >= '2013-01-01 00:00:00'   and b.treate_date <= '2014-01-01 00:00:00'

这地方感觉有两个问题
一是隐式转换,二是后一个条件应为b.treate_date <'2014-01-01 00:00:00'

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
44#
发表于 2013-9-10 06:30 | 只看该作者
lastwinner 发表于 2013-9-10 00:23
b.treate_date >= '2013-01-01 00:00:00'   and b.treate_date

看看我的例子

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
45#
发表于 2013-9-10 06:53 | 只看该作者
我找了另一台windows机器,内存也很大,同样的计划,执行都慢了,全表扫描估计用了8秒

SQL> select b.b,
  2         max(b.id) , /*很快*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

                   B            MAX(B.ID)             MIN_USED
-------------------- -------------------- --------------------
                   1               999863                   59
                  11               999989                   11
                  10               999988                   10
                  12               999961                  157

已用时间:  00: 00: 09.62
SQL> select b.b,
  2         max(b.id) - /*很慢*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

                   B             MIN_USED
-------------------- --------------------
                  11               999978
                   1               999804
                  10               999978
                  12               999804

已用时间:  00: 00: 09.62
SQL> exec dbms_stats.gather_table_stats(ownname => 'scott',tabname => 'big',estimate_percent => 100,method_opt=> 'for all columns') ;

PL/SQL 过程已成功完成。

已用时间:  00: 00: 06.19
SQL>
SQL>
SQL> exec dbms_stats.gather_table_stats(ownname => 'scott',tabname => 'e',estimate_percent => 100,method_opt=> 'for all columns') ;

PL/SQL 过程已成功完成。

已用时间:  00: 00: 00.04
SQL>
SQL>
SQL> select b.b,
  2         max(b.id) , /*很快*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

                   B            MAX(B.ID)             MIN_USED
-------------------- -------------------- --------------------
                   1               999863                   59
                  11               999989                   11
                  10               999988                   10
                  12               999961                  157

已用时间:  00: 00: 09.47
SQL> set lines 132 pages 50000
SQL> set autot trace
SP2-0618: 无法找到会话标识符。启用检查 PLUSTRACE 角色
SP2-0611: 启用 STATISTICS 报告时出错
SQL> /
已用时间:  00: 00: 00.00

执行计划
----------------------------------------------------------
Plan hash value: 69571800

----------------------------------------------------------------------------------
| Id  | Operation             | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |          |    13 |   182 |   644   (4)| 00:00:08 |
|   1 |  HASH GROUP BY        |          |    13 |   182 |   644   (4)| 00:00:08 |
|*  2 |   HASH JOIN           |          |   128K|  1751K|   638   (3)| 00:00:08 |
|   3 |    VIEW               | VW_GBF_5 |    13 |    39 |     8  (13)| 00:00:01 |
|   4 |     HASH GROUP BY     |          |    13 |    39 |     8  (13)| 00:00:01 |
|*  5 |      TABLE ACCESS FULL| E        |   500 |  1500 |     7   (0)| 00:00:01 |
|*  6 |    TABLE ACCESS FULL  | BIG      |   285K|  3069K|   628   (3)| 00:00:08 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."B"="ITEM_1")
   5 - filter(TO_CHAR("E"."A") LIKE '1%')
   6 - filter("B"."A">=3 AND "B"."A"<=4)

SQL> set autot on
SP2-0618: 无法找到会话标识符。启用检查 PLUSTRACE 角色
SP2-0611: 启用 STATISTICS 报告时出错
SQL> /

                   B            MAX(B.ID)             MIN_USED
-------------------- -------------------- --------------------
                   1               999863                   59
                  11               999989                   11
                  10               999988                   10
                  12               999961                  157

已用时间:  00: 00: 09.65

执行计划
----------------------------------------------------------
Plan hash value: 69571800

----------------------------------------------------------------------------------
| Id  | Operation             | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |          |    13 |   182 |   644   (4)| 00:00:08 |
|   1 |  HASH GROUP BY        |          |    13 |   182 |   644   (4)| 00:00:08 |
|*  2 |   HASH JOIN           |          |   128K|  1751K|   638   (3)| 00:00:08 |
|   3 |    VIEW               | VW_GBF_5 |    13 |    39 |     8  (13)| 00:00:01 |
|   4 |     HASH GROUP BY     |          |    13 |    39 |     8  (13)| 00:00:01 |
|*  5 |      TABLE ACCESS FULL| E        |   500 |  1500 |     7   (0)| 00:00:01 |
|*  6 |    TABLE ACCESS FULL  | BIG      |   285K|  3069K|   628   (3)| 00:00:08 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."B"="ITEM_1")
   5 - filter(TO_CHAR("E"."A") LIKE '1%')
   6 - filter("B"."A">=3 AND "B"."A"<=4)

SQL> show sga
ORA-00942: 表或视图不存在


SQL>

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
46#
发表于 2013-9-10 07:22 | 只看该作者
在另一个磁盘新建表空间测试

SQL> conn / as sysdba
已连接。
SQL> create tablespace ts datafile 'e:\temp\ts.dbf' size 100M;

表空间已创建。

已用时间:  00: 00: 00.50
SQL> alter user scott default tablespace ts quota unlimited on ts;

用户已更改。

已用时间:  00: 00: 00.01
SQL> conn scott/tiger
已连接。
SQL> create table big(id int,a int,b int);

表已创建。

已用时间:  00: 00: 00.02
SQL> create table e(a int);

表已创建。

已用时间:  00: 00: 00.01
SQL> insert into big select level l,mod(level,7),mod(level,29) from dual connect by level<=1e6;

已创建 1000000 行。

已用时间:  00: 00: 02.96
SQL> insert into e select mod(level,13)from dual connect by level<=1e4;

已创建 10000 行。

已用时间:  00: 00: 00.04
SQL> commit;

提交完成。

已用时间:  00: 00: 00.00
SQL>
SQL> set autot on
SP2-0618: 无法找到会话标识符。启用检查 PLUSTRACE 角色
SP2-0611: 启用 STATISTICS 报告时出错
SQL>
SQL> select b.b,
  2         max(b.id) , /*很快*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

                   B            MAX(B.ID)             MIN_USED
-------------------- -------------------- --------------------
                   1               999863                   59
                  10               999988                   10
                  11               999989                   11
                  12               999961                  157

已用时间:  00: 00: 00.41

执行计划
----------------------------------------------------------
Plan hash value: 375847392

-----------------------------------------------------------------------------------
| Id  | Operation              | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |          |    70M|  3482M|   991  (38)| 00:00:12 |
|   1 |  SORT GROUP BY NOSORT  |          |    70M|  3482M|   991  (38)| 00:00:12 |
|   2 |   MERGE JOIN           |          |    70M|  3482M|   991  (38)| 00:00:12 |
|   3 |    SORT JOIN           |          |   296K|    11M|   639   (4)| 00:00:08 |
|   4 |     VIEW               | VW_GBC_5 |   296K|    11M|   639   (4)| 00:00:08 |
|   5 |      HASH GROUP BY     |          |   296K|    11M|   639   (4)| 00:00:08 |
|*  6 |       TABLE ACCESS FULL| BIG      |   296K|    11M|   626   (2)| 00:00:08 |
|*  7 |    SORT JOIN           |          |  3077 | 40001 |     8  (13)| 00:00:01 |
|*  8 |     TABLE ACCESS FULL  | E        |  3077 | 40001 |     7   (0)| 00:00:01 |
-----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   6 - filter("B"."A"<=4 AND "B"."A">=3)
   7 - access("ITEM_1"="E"."A")
       filter("ITEM_1"="E"."A")
   8 - filter(TO_CHAR("E"."A") LIKE '1%')

Note
-----
   - dynamic sampling used for this statement (level=2)

SQL>
SQL> select b.b,
  2         max(b.id) - /*很慢*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

                   B             MIN_USED
-------------------- --------------------
                  11               999978
                   1               999804
                  10               999978
                  12               999804

已用时间:  00: 00: 09.63

执行计划
----------------------------------------------------------
Plan hash value: 2033372611

----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    31M|  1561M|  2525  (76)| 00:00:31 |
|   1 |  HASH GROUP BY      |      |    31M|  1561M|  2525  (76)| 00:00:31 |
|*  2 |   HASH JOIN         |      |    31M|  1561M|   789  (22)| 00:00:10 |
|*  3 |    TABLE ACCESS FULL| E    |  3077 | 40001 |     7   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL| BIG  |   296K|    11M|   627   (3)| 00:00:08 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."B"="E"."A")
   3 - filter(TO_CHAR("E"."A") LIKE '1%')
   4 - filter("B"."A">=3 AND "B"."A"<=4)

Note
-----
   - dynamic sampling used for this statement (level=2)

SQL>
SQL> exec dbms_stats.gather_table_stats(ownname => 'scott',tabname => 'big',estimate_percent => 100,method_opt=> 'for all columns') ;

PL/SQL 过程已成功完成。

已用时间:  00: 00: 06.22
SQL>
SQL> exec dbms_stats.gather_table_stats(ownname => 'scott',tabname => 'e',estimate_percent => 100,method_opt=> 'for all columns') ;

PL/SQL 过程已成功完成。

已用时间:  00: 00: 00.02
SQL>
SQL>
SQL>
SQL> select b.b,
  2         max(b.id) , /*很快*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

                   B            MAX(B.ID)             MIN_USED
-------------------- -------------------- --------------------
                   1               999863                   59
                  10               999988                   10
                  11               999989                   11
                  12               999961                  157

已用时间:  00: 00: 00.30

执行计划
----------------------------------------------------------
Plan hash value: 69571800

----------------------------------------------------------------------------------
| Id  | Operation             | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |          |    13 |   182 |   644   (4)| 00:00:08 |
|   1 |  HASH GROUP BY        |          |    13 |   182 |   644   (4)| 00:00:08 |
|*  2 |   HASH JOIN           |          |   128K|  1751K|   638   (3)| 00:00:08 |
|   3 |    VIEW               | VW_GBF_5 |    13 |    39 |     8  (13)| 00:00:01 |
|   4 |     HASH GROUP BY     |          |    13 |    39 |     8  (13)| 00:00:01 |
|*  5 |      TABLE ACCESS FULL| E        |   500 |  1500 |     7   (0)| 00:00:01 |
|*  6 |    TABLE ACCESS FULL  | BIG      |   285K|  3069K|   628   (3)| 00:00:08 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."B"="ITEM_1")
   5 - filter(TO_CHAR("E"."A") LIKE '1%')
   6 - filter("B"."A">=3 AND "B"."A"<=4)

SQL>
SQL> select b.b,
  2         max(b.id) - /*很慢*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

                   B             MIN_USED
-------------------- --------------------
                  11               999978
                   1               999804
                  10               999978
                  12               999804

已用时间:  00: 00: 09.71

执行计划
----------------------------------------------------------
Plan hash value: 2033372611

----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |    13 |   182 |   903  (32)| 00:00:11 |
|   1 |  HASH GROUP BY      |      |    13 |   182 |   903  (32)| 00:00:11 |
|*  2 |   HASH JOIN         |      |  4926K|    65M|   660   (6)| 00:00:08 |
|*  3 |    TABLE ACCESS FULL| E    |   500 |  1500 |     7   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL| BIG  |   285K|  3069K|   628   (3)| 00:00:08 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."B"="E"."A")
   3 - filter(TO_CHAR("E"."A") LIKE '1%')
   4 - filter("B"."A">=3 AND "B"."A"<=4)

SQL>

使用道具 举报

回复
论坛徽章:
0
47#
 楼主| 发表于 2013-9-10 07:54 | 只看该作者
angu204 发表于 2013-9-9 22:51
with a as (
select b.elec_meter_id elec_meter_id,
       max(b.total_used) max_used,

谢谢

这种方法我在发帖之前就试过了,还是很慢

使用道具 举报

回复
论坛徽章:
0
48#
 楼主| 发表于 2013-9-10 08:00 | 只看该作者
〇〇 发表于 2013-9-10 06:53
我找了另一台windows机器,内存也很大,同样的计划,执行都慢了,全表扫描估计用了8秒

SQL> select b.b, ...

晚上执行了
exec dbms_stats.gather_table_stats(ownname => 'ecms0219',tabname => 'bigdata’,estimate_percent => 100,method_opt=> 'for all columns');
之后,两条SQL执行速度差不多,查了多次都在0.3s以内,原先慢SQL的计划现在都变成了NESTED LOOPS。

使用道具 举报

回复
论坛徽章:
30
生肖徽章:猴
日期:2015-03-20 10:13:49ITPUB 11周年纪念徽章
日期:2012-10-23 16:55:31优秀写手
日期:2015-03-07 06:00:14沸羊羊
日期:2015-03-13 13:26:07美羊羊
日期:2015-04-06 20:32:13天蝎座
日期:2015-07-20 12:34:47射手座
日期:2015-09-18 12:38:55乌索普
日期:2016-08-03 07:04:28奥运会纪念徽章:手球
日期:2016-09-26 07:19:26山治
日期:2016-10-19 05:48:10
49#
发表于 2013-9-10 09:10 | 只看该作者
暗纹河豚毒 发表于 2013-9-10 08:00
晚上执行了
exec dbms_stats.gather_table_stats(ownname => 'ecms0219',tabname => 'bigdata’,estimat ...

没有使用HINT?

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
50#
发表于 2013-9-10 10:53 | 只看该作者
lz把你的distinct改为group by 试试看
我的改了以后减法和不做减法计划相同了

select /*+ gather_plan_statistics */b.b,
       max(b.id) - /*很慢*/
       min(b.id) min_used
  from big b,
       (select e.a
          from e
        where e.a like '1%' group by e.a) m
where b.a>=3
   and b.a<=4
   and b.b= m.a
group by b.b;

select * from TABLE(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST'));

SQL> select /*+ gather_plan_statistics */b.b,
  2         max(b.id) - /*很慢*/
  3         min(b.id) min_used
  4    from big b,
  5         (select distinct e.a
  6            from e
  7          where e.a like '1%') m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

select * from TABLE(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST'));

         B   MIN_USED
---------- ----------
         1     999804
        11     999978
        12     999804
        10     999978

SQL> SQL>
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  238f8vj22nb8m, child number 0
-------------------------------------
select /*+ gather_plan_statistics */b.b,        max(b.id) - /*很慢*/
  min(b.id) min_used   from big b,        (select distinct e.a
from e         where e.a like '1%') m where b.a>=3    and b.a<=4
and b.b= m.a group by b.b

Plan hash value: 2033372611

-----------------------------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |      1 |        |      4 |00:00:10.61 |    2230 |       |       |          |
|   1 |  HASH GROUP BY      |      |      1 |     13 |      4 |00:00:10.61 |    2230 |   838K|   838K| 4105K (0)|
|*  2 |   HASH JOIN         |      |      1 |   4926K|     30M|00:00:05.75 |    2230 |  1517K|  1517K| 1004K (0)|
|*  3 |    TABLE ACCESS FULL| E    |      1 |    500 |   3077 |00:00:00.01 |      22 |       |       |          |
|*  4 |    TABLE ACCESS FULL| BIG  |      1 |    285K|    285K|00:00:00.09 |    2208 |       |       |          |
-----------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."B"="E"."A")
   3 - filter(TO_CHAR("E"."A") LIKE '1%')
   4 - filter(("B"."A">=3 AND "B"."A"<=4))


已选择26行。

SQL> select /*+ gather_plan_statistics */b.b,
  2         max(b.id) - /*很慢*/
  3         min(b.id) min_used
  4    from big b,
  5         (select e.a
  6            from e
  7          where e.a like '1%' group by e.a) m
  8  where b.a>=3
  9     and b.a<=4
10     and b.b= m.a
11  group by b.b;

select * from TABLE(dbms_xplan.display_cursor(NULL,NULL,'ALLSTATS LAST'));
         B   MIN_USED
---------- ----------
         1     999804
        11     999978
        12     999804
        10     999978

SQL> SQL>

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  44x1j8gq0xhrg, child number 0
-------------------------------------
select /*+ gather_plan_statistics */b.b,        max(b.id) - /*很慢*/
  min(b.id) min_used   from big b,        (select e.a           from e
       where e.a like '1%' group by e.a) m where b.a>=3    and b.a<=4
and b.b= m.a group by b.b

Plan hash value: 3990005897

-------------------------------------------------------------------------------------------------------------------
| Id  | Operation             | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |      |      1 |        |      4 |00:00:00.15 |    2230 |       |       |          |
|   1 |  HASH GROUP BY        |      |      1 |     13 |      4 |00:00:00.15 |    2230 |   838K|   838K| 1254K (0)|
|*  2 |   HASH JOIN           |      |      1 |    128K|  39410 |00:00:00.13 |    2230 |  1517K|  1517K|  863K (0)|
|   3 |    VIEW               |      |      1 |     13 |      4 |00:00:00.01 |      22 |       |       |          |
|   4 |     HASH GROUP BY     |      |      1 |     13 |      4 |00:00:00.01 |      22 |  1518K|  1518K|  707K (0)|
|*  5 |      TABLE ACCESS FULL| E    |      1 |    500 |   3077 |00:00:00.01 |      22 |       |       |          |
|*  6 |    TABLE ACCESS FULL  | BIG  |      1 |    285K|    285K|00:00:00.08 |    2208 |       |       |          |
-------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("B"."B"="M"."A")
   5 - filter(TO_CHAR("E"."A") LIKE '1%')
   6 - filter(("B"."A">=3 AND "B"."A"<=4))


使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表