Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1412872
  • 博文数量: 173
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3841
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-30 13:00
个人简介

About me:Oracle ACE pro,optimistic,passionate and harmonious. Focus on ORACLE,MySQL and other database programming,peformance tuning,db design, j2ee,Linux/AIX,Architecture tech,etc

文章分类

全部博文(173)

文章存档

2025年(1)

2024年(27)

2023年(28)

2022年(43)

2020年(62)

2014年(3)

2013年(9)

分类: Oracle

2025-01-22 09:09:36

索引是有序存储的,查询leftmost或rightmost就可以查询min/max,这是一种非常快的操作,因为min.max索引前导列,{BANNED}最佳需要通过索引扫描leftmost或rightmost中的no.1的非null行即可


 pg和mysql支持索引列min.max写在一起,会快速定位,而oracle不行,oracle可以拆分成两个标量子查询实现。


pg查询索引列min.max可以写在一起,自动拆分

explain select max(id),min(id) from main_t;
                                                QUERY PLAN                                                
----------------------------------------------------------------------------------------------------------
 Result  (cost=0.64..0.65 rows=1 width=8)
   InitPlan 1 (returns $0)
     ->  Limit  (cost=0.28..0.32 rows=1 width=4)
           ->  Index Only Scan Backward using idx1_main_t on main_t  (cost=0.28..45.77 rows=1000 width=4)
                 Index Cond: (id IS NOT NULL)
   InitPlan 2 (returns $1)
     ->  Limit  (cost=0.28..0.32 rows=1 width=4)
           ->  Index Only Scan using idx1_main_t on main_t main_t_1  (cost=0.28..45.77 rows=1000 width=4)
                 Index Cond: (id IS NOT NULL)
(9 rows)

 mysql也是可以写在一起,而且mysql会const化,在优化阶段fast execution,对应Rows fetched before execution
explain analyze
select max(id),min(id) from main_t\G
*************************** 1. row ***************************
EXPLAIN: -> Rows fetched before execution  (cost=0.00..0.00 rows=1) (actual time=0.000..0.000 rows=1 loops=1)



oracle的索引列min/max不能写在一起,可以使用标量子查询拆分后走索引

 select max(id),min(id) from main_t;
Elapsed: 00:00:00.01


Execution Plan
----------------------------------------------------------
Plan hash value: 3205363217


-----------------------------------------------------------------------------
| Id  | Operation          | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |        |     1 |     4 |     9   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |        |     1 |     4 |            |          |
|   2 |   TABLE ACCESS FULL| MAIN_T | 10000 | 40000 |     9   (0)| 00:00:01 |
-----------------------------------------------------------------------------


select max(id) from main_t;
Elapsed: 00:00:00.00


Execution Plan
----------------------------------------------------------
Plan hash value: 2290008741


-----------------------------------------------------------------------------------------
| Id  | Operation                  | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |            |     1 |     4 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE            |            |     1 |     4 |            |          |
|   2 |   INDEX FULL SCAN (MIN/MAX)| IDX_MAIN_T |     1 |     4 |     2   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------


拆分后正常
select (select max(id) from main_t) max_id,(select min(id) from main_t) min_id
from dual;
Execution Plan
----------------------------------------------------------
Plan hash value: 3605433664


-----------------------------------------------------------------------------------------
| Id  | Operation                  | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |            |     1 |       |     6   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE            |            |     1 |     4 |            |          |
|   2 |   INDEX FULL SCAN (MIN/MAX)| IDX_MAIN_T |     1 |     4 |     2   (0)| 00:00:01 |
|   3 |  SORT AGGREGATE            |            |     1 |     4 |            |          |
|   4 |   INDEX FULL SCAN (MIN/MAX)| IDX_MAIN_T |     1 |     4 |     2   (0)| 00:00:01 |
|   5 |  FAST DUAL                 |            |     1 |       |     2   (0)| 00:00:01 |
----------------------------

阅读(36) | 评论(0) | 转发(0) |
0

上一篇:ORACLE分区pruning及其注意点

下一篇:没有了

给主人留下些什么吧!~~