12
返回列表 发新帖
楼主: mo_yin

[PL/SQL] 联合索引使用了聚合函数

[复制链接]
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
11#
发表于 2016-11-8 00:49 | 只看该作者
mo_yin 发表于 2016-11-7 15:53
经过咨询和查找,总结一下原因,如果有误请指正。谢谢。

联合索引((clu_date,clu_no))其中clu_date ...

你只列出了一种计划。针对1楼说的现象,把两种计划都列出来看看。

使用道具 举报

回复
论坛徽章:
3
ITPUB15周年纪念
日期:2016-10-13 13:15:34秀才
日期:2017-03-20 13:42:20秀才
日期:2017-03-28 15:59:38
12#
发表于 2016-11-15 14:54 | 只看该作者
以上解答都没有解答你所说的“如果我使用的cluNo是test表中存在的,那么这个查询会很快;
      但是如果我使用的cluNo是test表中不存在的,那么这个查询就非常慢;

最好把后一种执行计划贴出(如newkid大神所说),索引是有序的,看你第一种执行计划用的是 index max/min的方式,当clu_no有值时只需要在date索引的最右端块查找到clu_date就可以,当clu_no没有对应值时会遍历索引块逐一查找,但最后没有找到,这过程会比较耗时

使用道具 举报

回复
论坛徽章:
2
乌索普
日期:2016-11-28 11:16:45秀才
日期:2016-12-21 16:55:07
13#
发表于 2016-11-16 14:51 | 只看该作者
如果查询条件中没有clu_date,只有clu_no应该是不会走这个索引了吧,oracle不太了解,但是mysql一定不走。

使用道具 举报

回复
论坛徽章:
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
14#
发表于 2016-11-16 19:39 | 只看该作者

create table test(id_test int,clu_no int,clu_date int,clu_other_date int);
insert into test values(2,2,2,2);
create index aaa on test(clu_date,clu_no);
set autot on
select max(clu_other_date) from test t where t.clu_no =1;
select max(clu_other_date) from test t where t.clu_no =2;
select max(clu_date) from test t where t.clu_no =1;
select max(clu_date) from test t where t.clu_no =2;

SQL> set autot on
--求max的列不在索引中,fts
SQL> select max(clu_other_date) from test t where t.clu_no =1;

MAX(CLU_OTHER_DATE)
-------------------


已用时间:  00: 00: 00.01

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

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    26 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |    26 |            |          |
|*  2 |   TABLE ACCESS FULL| TEST |     1 |    26 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

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

   2 - filter("T"."CLU_NO"=1)

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


统计信息
----------------------------------------------------------
          5  recursive calls
          0  db block gets
         15  consistent gets
          0  physical reads
          0  redo size
        439  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
--求max的列不在索引中,fts
SQL> select max(clu_other_date) from test t where t.clu_no =2;

MAX(CLU_OTHER_DATE)
-------------------
                  2

已用时间:  00: 00: 00.00

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

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    26 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |    26 |            |          |
|*  2 |   TABLE ACCESS FULL| TEST |     1 |    26 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

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

   2 - filter("T"."CLU_NO"=2)

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


统计信息
----------------------------------------------------------
          4  recursive calls
          0  db block gets
         15  consistent gets
          0  physical reads
          0  redo size
        441  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
--求max的列在索引中,fis
SQL> select max(clu_date) from test t where t.clu_no =1;

MAX(CLU_DATE)
-------------


已用时间:  00: 00: 00.00

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

------------------------------------------------------------------------------------
| Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |      |     1 |    26 |     1   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE             |      |     1 |    26 |            |          |
|   2 |   FIRST ROW                 |      |     1 |    26 |     1   (0)| 00:00:01 |
|*  3 |    INDEX FULL SCAN (MIN/MAX)| AAA  |     1 |    26 |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------

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

   3 - filter("T"."CLU_NO"=1)

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


统计信息
----------------------------------------------------------
          4  recursive calls
          0  db block gets
         10  consistent gets
          0  physical reads
          0  redo size
        433  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
--求max的列在索引中,fis
SQL> select max(clu_date) from test t where t.clu_no =2;

MAX(CLU_DATE)
-------------
            2

已用时间:  00: 00: 00.01

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

------------------------------------------------------------------------------------
| Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |      |     1 |    26 |     1   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE             |      |     1 |    26 |            |          |
|   2 |   FIRST ROW                 |      |     1 |    26 |     1   (0)| 00:00:01 |
|*  3 |    INDEX FULL SCAN (MIN/MAX)| AAA  |     1 |    26 |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------

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

   3 - filter("T"."CLU_NO"=2)

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


统计信息
----------------------------------------------------------
          4  recursive calls
          0  db block gets
         10  consistent gets
          0  physical reads
          0  redo size
        435  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

使用道具 举报

回复
论坛徽章:
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
15#
发表于 2016-11-16 19:45 | 只看该作者
如果用hint,那么--求max的列不在索引中,也可以its

SQL> select /*+index(t aaa)*/ max(clu_other_date) from test t where t.clu_no =2;

MAX(CLU_OTHER_DATE)
-------------------
                  2

已用时间:  00: 00: 00.01

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

-------------------------------------------------------------------------------------
| Id  | Operation                    | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |      |     1 |    26 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE              |      |     1 |    26 |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID| TEST |     1 |    26 |     2   (0)| 00:00:01 |
|*  3 |    INDEX FULL SCAN           | AAA  |     1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

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

   3 - access("T"."CLU_NO"=2)
       filter("T"."CLU_NO"=2)

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


统计信息
----------------------------------------------------------
          4  recursive calls
          0  db block gets
         10  consistent gets
          0  physical reads
          0  redo size
        441  bytes sent via SQL*Net to client
        419  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

使用道具 举报

回复

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

本版积分规则 发表回复

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