组函数会忽略null值

SQL> select distinct id from abc;

ID


1

2

4

SQL> select count( distinct id ) from abc;

COUNT(DISTINCTID)


3

SQL> select count(id) from abc;

COUNT(ID)


3

SQL> select count(*) from abc;

COUNT(*)


5

SQL> select *from abc;

ID NAME


1 asdf

2 sdfa

sdfa

4 fdas

asdf

SQL>

hint被忽略

如果CBO认为使用hint会导致错误的结果时,hint将被忽略,详见下例SQL> select /*+ index(t t_ind) */ count(*) from t;

Execution Plan


Plan hash value: 2966233522


| Id | Operation | Name | Rows | Cost (%CPU)| Time |


| 0 | SELECT STATEMENT | | 1 | 57 (2)| 00:00:01 |

| 1 | SORT AGGREGATE | | 1 | | |

| 2 | TABLE ACCESS FULL| T | 50366 | 57 (2)| 00:00:01 |


因为我们是对记录求总数,且我们并没有在建立索引时指定不能为空,索引如果CBO选择在索引上进行count时,但索引字段上的值为空时,结果将不准确,故CBO没有选择索引。 SQL> select /*+ index(t t_ind) */ count(id) from t;

Execution Plan


Plan hash value: 646498162


| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |


| 0 | SELECT STATEMENT | | 1 | 5 | 285 (1)| 00:00:04 |

| 1 | SORT AGGREGATE | | 1 | 5 | | |

| 2 | INDEX FULL SCAN| T_IND | 50366 | 245K| 285 (1)| 00:00:04 |


因为我们只对id进行count,这个动作相当于count索引上的所有id值,这个操作和对表上的id字段进行count是一样的(组函数会忽略null值)

/*+ INDEX_JOIN(TABLE INDEX_NAME1 INDEX_NAME2) */

当谓词中引用的列都有索引的时候,可以通过指定采用索引关联的方式,来访问数据select /*+ index_join(t t_ind t_bm) */ id from t where id=100 and object_name='EMPLOYEES'

/*+ no_use_nl(table_1,table_2) */在多表关联查询中,指定不使用nest loops方式进行多表关联。

select /*+ no_use_nl(t,t1) */ t.* from t,t1 where t.id=t1.id;

/*+ no_use_hash(table_1,table_2) */在多表关联查询中,指定不使用hash join方式进行多表关联。

select /*+ no_use_hash(t,t1) */ t.* from t,t1 where t.id=t1.id;

/*+ no_use_merge(table_1,table_2) */在多表关联查询中,指定不使用merge join方式进行多表关联。

select /*+ no_use_merge(t,t1) */ t.* from t,t1 where t.id=t1.id;

/*+ dynamic_sampling(table_name n) */

设置sql执行时动态采用的级别,这个级别为0~10select /*+ dynamic_sampling(t 4) */ * from t where id > 1234

NL_AJ

Use an anti-join in a sub-query. (depricated in Oracle 10g)

相关推荐
MZZDX15 分钟前
MySQL相关知识总结
数据库·mysql
青山撞入怀11142 小时前
sql题目练习——聚合函数
数据库·sql
摇滚侠2 小时前
Spring Boot 3零基础教程,创建第一个 Spring Boot 3 应用,Spring Boot 3 外部配置,笔记03
java·spring boot·笔记
disanleya2 小时前
MySQL默认端口为何是3306?修改后如何管理?
数据库·mysql·adb
没有bug.的程序员5 小时前
服务网格 Service Mesh:微服务通信的终极进化
java·分布式·微服务·云原生·service_mesh
川石课堂软件测试5 小时前
MySQL数据库之DBA命令
数据库·网络协议·mysql·http·单元测试·prometheus·dba
ybb_ymm7 小时前
mysql8在linux下的默认规则修改
linux·运维·数据库·mysql
南尘NCA86668 小时前
企业微信防封防投诉拦截系统:从痛点解决到技术实现
java·网络·企业微信
倔强的石头_8 小时前
Navicat Premium 与金仓数据库融合实践:高效管理国产数据库新方案
数据库