oracle10g数据库max、min的写法
一、有主键情况
1、单查最大的主键object_id(cost:2、consistent gets:100)
select max(object_id) from t;
2、单查最小的主键object_id(cost:2、consistent gets:69)
select min(object_id) from t;
3、一起查max(object_id),min(object_id) (cost:47、consistent gets 224)
select max(object_id),min(object_id) from t;
4、最优一起查询(cost:4、consistent gets:138)
select max,min from (select max(object_id) max from t)a,(select min(object_id) min from t)b;
结论:
在字段为主键有索引的情况下,单查单个max、min效率最快
在字段为主键有索引的情况下,优化方式查询最快
在字段为主键有索引的情况下,直接max、min的方式最慢
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
二、在t表中object_id不是主键没有唯一索引 并且有null值的情况
SQL> select max(object_id) from t;
MAX(OBJECT_ID)
--------------
178100
执行计划
----------------------------------------------------------
Plan hash value: 2966233522
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 288 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | TABLE ACCESS FULL| T | 69433 | 881K| 288 (1)| 00:00:04 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
48 recursive calls
0 db block gets
1099 consistent gets
1024 physical reads
0 redo size
343 bytes sent via SQL*Net to client
350 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select min(object_id) from t;
MIN(OBJECT_ID)
--------------
2
执行计划
----------------------------------------------------------
Plan hash value: 2966233522
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 288 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | TABLE ACCESS FULL| T | 69433 | 881K| 288 (1)| 00:00:04 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
4 recursive calls
0 db block gets
1095 consistent gets
0 physical reads
0 redo size
342 bytes sent via SQL*Net to client
350 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select max(object_id),min(object_id) from t;
MAX(OBJECT_ID) MIN(OBJECT_ID)
-------------- --------------
178100 2
执行计划
----------------------------------------------------------
Plan hash value: 2966233522
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 288 (1)| 00:00:04 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | TABLE ACCESS FULL| T | 69433 | 881K| 288 (1)| 00:00:04 |
---------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
4 recursive calls
0 db block gets
1095 consistent gets
0 physical reads
0 redo size
406 bytes sent via SQL*Net to client
350 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select max,min from (select max(object_id) max from t) a,(select min(object_id) min from t) b;
MAX MIN
---------- ----------
178100 2
执行计划
----------------------------------------------------------
Plan hash value: 1937292215
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 575 (1)| 00:00:07 |
| 1 | NESTED LOOPS | | 1 | 26 | 575 (1)| 00:00:07 |
| 2 | VIEW | | 1 | 13 | 288 (1)| 00:00:04 |
| 3 | SORT AGGREGATE | | 1 | 13 | | |
| 4 | TABLE ACCESS FULL| T | 69433 | 881K| 288 (1)| 00:00:04 |
| 5 | VIEW | | 1 | 13 | 288 (1)| 00:00:04 |
| 6 | SORT AGGREGATE | | 1 | 13 | | |
| 7 | TABLE ACCESS FULL| T | 69433 | 881K| 288 (1)| 00:00:04 |
-----------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
7 recursive calls
0 db block gets
2190 consistent gets
0 physical reads
0 redo size
384 bytes sent via SQL*Net to client
350 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
结论:
优化的方式查询还没有一起查询快,一起查询及单个查询花费的cpu几乎一样多。
上一篇: C语言中实现参数个数可变函数
推荐阅读
-
C#实现输入10个数存入到数组中并求max和min及平均数的方法示例
-
Oracle11g数据库导入Oracle10g数据库的方法,oracle常用数据导入导出命令
-
Codeigniter操作数据库表的优化写法总结
-
C#实现输入10个数存入到数组中并求max和min及平均数的方法示例
-
Codeigniter操作数据库表的优化写法总结
-
详解数据库连接的URL的写法及总结
-
DROP TABLE在不同数据库中的写法整理
-
使用Math.max,Math.min获取数组中的最值实例
-
通过创建SQLServer 2005到 Oracle10g 的链接服务器实现异构数据库数据转换方案
-
C# 调用Access数据库关于like模糊查询的写法