我们知道 volatile 函数会影响SQL的执行性能,比如:volatile 类型函数无法建函数索引、volatile 函数针对每条记录都要执行一次。本篇的例子主要讲述 volatile 类型的函数还会影响子查询的提升。

1、构建例子

create table t1(id1 integer,name1 varchar(9),addr1 text);
create table t2(id2 integer,name2 varchar(9),addr2 text);
insert into t1 select generate_series(1,1000000),generate_series(1,1000000),'abc';
insert into t2 select generate_series(1,1000000),generate_series(1,1000000),'abc';
create index ind_t1 on t1(id1);
create index ind_t2 on t2(id2);

2、volatile 函数与执行计划

对于 t2 表的访问无法使用索引。

test=# \df+ replace
List of functions
Schema | Name | Result data type | Argument data types | Type | Volatility | Parallel | Owner | Security | Access privileges | Language | Source code | Description
--------+---------+-------------------+---------------------+------+------------+----------+--------+----------+-------------------+----------+---------------------------------+-------------
sys | replace | character varying | text, text, text | func | volatile | safe | system | invoker | | c | ora_replace_text | test=# explain select id1,name1 from t1 a ,(select id2,replace(id2,'b','B') name2 from t2 ) b where a.id1=b.id2 and name1='123';
QUERY PLAN
---------------------------------------------------------------------
Hash Join (cost=17935.01..52120.02 rows=1 width=10)
Hash Cond: (t2.id2 = a.id1)
-> Seq Scan on t2 (cost=0.00..20435.00 rows=1000000 width=36)
-> Hash (cost=17935.00..17935.00 rows=1 width=10)
-> Seq Scan on t1 a (cost=0.00..17935.00 rows=1 width=10)
Filter: ((name1)::text = '123'::text)
(6 rows)

不使用子查询情况下的,可以使用索引:

test=# explain select  id1,name1,id2,replace(id2,'b','B') name2 from t1 a,t2 b where a.id1=b.id2 and name1='123';
QUERY PLAN
------------------------------------------------------------------------------
Nested Loop (cost=0.42..17943.46 rows=1 width=46)
-> Seq Scan on t1 a (cost=0.00..17935.00 rows=1 width=10)
Filter: ((name1)::text = '123'::text)
-> Index Only Scan using ind_t2 on t2 b (cost=0.42..8.44 rows=1 width=4)
Index Cond: (id2 = a.id1)
(5 rows)

3、immutable 函数与执行计划

改成immutable 函数后,子查询可以提升,从而能够使用索引。

test=# \df+ replace
List of functions
Schema | Name | Result data type | Argument data types | Type | Volatility | Parallel | Owner | Security | Access privileges | Language | Source code | Description
--------+---------+-------------------+---------------------+------+------------+----------+--------+----------+-------------------+----------+---------------------------------+-------------
sys | replace | character varying | text, text, text | func | immutable | safe | system | invoker | | c | ora_replace_text | test=# explain select id1,name1 from t1 a ,(select id2,replace(id2,'b','B') name2 from t2 ) b where a.id1=b.id2 and name1='123';
QUERY PLAN
----------------------------------------------------------------------------
Nested Loop (cost=0.42..17943.45 rows=1 width=10)
-> Seq Scan on t1 a (cost=0.00..17935.00 rows=1 width=10)
Filter: ((name1)::text = '123'::text)
-> Index Only Scan using ind_t2 on t2 (cost=0.42..8.44 rows=1 width=4)
Index Cond: (id2 = a.id1)
(5 rows)

  

volatile 函数影响子查询提升的更多相关文章

  1. Oracle学习总结_day03_day04_条件查询_排序_函数_子查询

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day03_条件查询_排序_函数 清空回收站: PUR ...

  2. 逆袭之旅DAY14.东软实训.Oracle.多表连接、分组函数、子查询

    2018-07-10 08:29:55 思考应用场景 异常数据的测试 6.显示能挣得奖金的雇员的姓名.工资.奖金,并以工资和奖金降序排列.select ename,sal,commfrom empWH ...

  3. Hibernate 函数 ,子查询 和原生SQL查询

    一. 函数 聚合函数:count(),avg(),sum(),min(),max() 例:(1)查询Dept表中的所有的记录条数. String hql=" select count(*) ...

  4. Hibernate 笔记 HQL查询 条件查询,聚集函数,子查询,导航查询

    在hibernate中进行多表查询,每个表中各取几个字段,也就是说查询出来的结果集并没有一个实体类与之对应,如何解决这个问题? 解决方案一,按照Object[]数据取出数据,然后自己组bean 解决方 ...

  5. postgresql子查询优化(提升子查询)

    问题背景 在开发项目过程中,客户要求使用gbase8s数据库(基于informix),简单的分页页面响应很慢.排查发现分页sql是先查询出数据在外面套一层后再取多少条,如果去掉嵌套的一层,直接获取则很 ...

  6. [sql Server]除非另外还指定了TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效

    今天遇到一个奇怪的问题,项目突然要从mysql切换到sql server数据库,包含order by 子句的嵌套子查询报错. 示例:select top 10 name,age,sex from ( ...

  7. Server Sql 多表查询、子查询和分页

    一.多表查询:根据特定的连接条件从不同的表中获取所需的数据 多表查询语法: SELECT table1.column, table2.column FROM table1, table2 WHERE ...

  8. MYSQL子查询与连接

    37:子查询与连接SET 列名 gbk;//改变客户端数据表的编码类型. 子查询子查询(Subquery)是指出现在其他SQL语句内的SELECT子句例如SELECT * FROM t1 WHERE ...

  9. [mysql]子查询与连接

    1,子查询(Subquery)是指出现在其他 SQL 语句内的select子句 例如: select * from t1 where col1 = (select col2 from t2); 其中 ...

随机推荐

  1. C4C UI Design背景色

  2. 没有Kubernetes怎么玩Dapr?

    Dapr 被设计成一个面向开发者的企业级微服务编程平台,它独立于具体的技术平台,可以运行在"任何地方".Dapr本身并不提供"基础设施(infrastructure)&q ...

  3. SpringCloud Gateway微服务网关实战与源码分析-上

    概述 定义 Spring Cloud Gateway 官网地址 https://spring.io/projects/spring-cloud-gateway/ 最新版本3.1.3 Spring Cl ...

  4. 浏览器中的原生base64方法

    在web开发中,经常涉及到对文本.文件等进行base64编码处理,在之前的开发中,使用js-base64来进行处理,但其实浏览器已经原生包含了处理方法.性能更好,兼容性也更高. atob() - AS ...

  5. IIS部署的H5的单页面跳转的配置

    <?xml version="1.0" encoding="UTF-8"?><configuration> <system.web ...

  6. (一)java基础篇---第一个程序

    先认识java的基础知识 1.变量命名规则 :1)变量名由数字字母下划线组成,2)不能使用java的关键字,比如public这种,3)遵循小驼峰命名法 2.数据类型 2.1基本数据类型有8种 其中分为 ...

  7. grafana监控配置

    一.配置开启smtp服务 1.编辑grafana配置文件grafana.ini [smtp] enabled = true host = smtp.163.com:25 user = 157xxxx3 ...

  8. Nginx搭建简易文件服务器

    Nginx搭建简易文件服务器 1.安装nginx,此处略过 2.修改nginx配置文件 详细如下 # 此处为部分文件是否有权限,使用root,则不会出现403权限问题 user root; worke ...

  9. 20220722-Java中this关键字

    this关键字知识总结 学习资源:B站韩顺平老师Java入门教学 代码示例1 public class This01 { public static void main(String[] args) ...

  10. 基于ABP和Magicodes实现Excel导出操作

      前端使用的vue-element-admin框架,后端使用ABP框架,Excel导出使用的Magicodes.IE.Excel.Abp库.Excel导入和导出操作几乎一样,不再介绍.文本主要介绍E ...