我们知道 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)

  

最新文章

  1. SQLServer------存储过程在C#中的使用方法
  2. C++混合编程之idlcpp教程Python篇(9)
  3. codeforces round #234B(DIV2) C Inna and Huge Candy Matrix
  4. TCP报文段首部详解
  5. Android WebView 获取网页的标题
  6. SSH框架总结(框架分析+环境搭建+实例源码下载)
  7. linux 命令行模式下,浏览网页
  8. 使用Cyclone IV控制DDR2
  9. 光流算法:灰度恒常约束,LK算法,HS算法
  10. pyhton小方法
  11. WordPress更改固定链接出现404的解决方案
  12. 自定义ComboBox,简简单单实现
  13. [原创.数据可视化系列之十三]idw反距离权重插值算法的javascript代码实现
  14. [LeetCode] K Inverse Pairs Array K个翻转对数组
  15. 46)django-发送邮件
  16. 加密方法与HTTPS 原理详解
  17. 大道至简第一章和java理论学时第一节。感受。
  18. [原] unity3d调用android版 人人sdk
  19. 《Python 网络爬虫权威指南》 分享 pdf下载
  20. PTA(BasicLevel)-1009 说反话

热门文章

  1. Canal实现MySQL协议
  2. 一文详解JackSon配置信息
  3. WPF开发随笔收录-DataAnnotations实现数据校验(MVVM架构下)
  4. jenkins结合ansible发布
  5. 【Python基础教程】三种常用、效率最高的Python字符串拼接方法
  6. MySQL进行 批量插入,批量删除,批量更新,批量查询
  7. 执行docker一系列命令失败
  8. 微服务追踪SQL(支持Isto管控下的gorm查询追踪)
  9. NC20861 兔子的逆序对
  10. 常用类-LocalDate、LocalTime、LocalDateTime