• Stable 函数不能修改数据库,单个Query中所有行给定同样的参数确保返回相同的结果。这种稳定级别允许优化器将多次函数调用转换为一次。在索引扫描的条件中使用这种函数是可行的,因为索引扫描只计算一次比较值(comparison value),而不是每行都计算一次。
  • Immutable 函数不能修改数据库,在任何情况下,只要输入参数相同,返回结果就相同。这种级别的函数,优化器可以提前进行计算,在查询过程中作为常量参数。比如:SELECT...WHERE x=2+2 可以简化为SELECT...WHERE x=4。

以下以例子说明二者的差异。

一、KingbaseES

1、准备数据

create table t1(id1 integer,id2 integer);
insert into t1 select generate_series(1,10000000),generate_series(1,10000000); test=# \timing on
Timing is on.
test=# select count(*) from t1;
count
----------
10000000
(1 row) Time: 681.445 ms

2、创建immutable 和 stable 函数

create or replace function f001()
returns bigint
immutable
language sql
as
$$ select count(*) from t1 $$ ; create or replace function f002()
returns bigint
stable
language sql
as
$$ select count(*) from t1 $$ ;

3、单独explain 函数

可以看到对于 immutable 函数,在 explain 时,实际会去执行的;而stable 函数,explain 时则不会实际执行。

test=# explain select f001();
QUERY PLAN
------------------------------------------
Result (cost=0.00..0.01 rows=1 width=8)
(1 row) Time: 450.572 ms
test=# explain select f002();
QUERY PLAN
------------------------------------------
Result (cost=0.00..0.26 rows=1 width=8)
(1 row) Time: 0.641 ms test=# select f001();
f001
----------
10000000
(1 row) Time: 448.720 ms
test=# select f002();
f002
----------
10000000
(1 row) Time: 426.745 ms

  

4、例子一

可以看到 immutable 函数执行时间主要花在planning上,也就是在制定执行计划前,就已经取得函数的值;而 stable 函数,则在语句解析和执行时,都要执行函数,而且,针对语句的访问的每个tuple,都要执行一次函数调用。

test=# explain analyze select * from (select * from t1 limit 10) a where a.id1=f001();
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Subquery Scan on a (cost=0.00..0.27 rows=1 width=8) (actual time=0.012..0.013 rows=0 loops=1)
Filter: (a.id1 = '10000000'::bigint)
Rows Removed by Filter: 10
-> Limit (cost=0.00..0.15 rows=10 width=8) (actual time=0.009..0.010 rows=10 loops=1)
-> Seq Scan on t1 (cost=0.00..148609.21 rows=10007621 width=8) (actual time=0.008..0.009 rows=10 loops=1)
Planning Time: 413.963 ms
Execution Time: 0.026 ms
(7 rows) test=# explain analyze select * from (select * from t1 limit 10) a where a.id1=f002();
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Subquery Scan on a (cost=0.00..2.77 rows=1 width=8) (actual time=3691.788..3691.788 rows=0 loops=1)
Filter: (a.id1 = f002())
Rows Removed by Filter: 10
-> Limit (cost=0.00..0.15 rows=10 width=8) (actual time=0.012..0.028 rows=10 loops=1)
-> Seq Scan on t1 (cost=0.00..148609.21 rows=10007621 width=8) (actual time=0.011..0.021 rows=10 loops=1)
Planning Time: 364.233 ms
Execution Time: 3691.807 ms
(7 rows) Time: 4056.907 ms (00:04.057) test=# explain analyze select * from (select * from t1 where 1=2) a where a.id1=f002();
QUERY PLAN
------------------------------------------------------------------------------------------
Result (cost=0.00..2675533.51 rows=1 width=8) (actual time=0.001..0.001 rows=0 loops=1)
One-Time Filter: false
-> Seq Scan on t1 (cost=0.00..2675533.51 rows=1 width=8) (never executed)
Filter: (id1 = f002())
Planning Time: 490.720 ms
Execution Time: 0.017 ms
(6 rows) test=# explain analyze select * from (select * from t1 limit 1) a where a.id1=f002();
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
Subquery Scan on a (cost=0.00..0.28 rows=1 width=8) (actual time=390.833..390.834 rows=0 loops=1)
Filter: (a.id1 = f002())
Rows Removed by Filter: 1
-> Limit (cost=0.00..0.01 rows=1 width=8) (actual time=0.024..0.053 rows=1 loops=1)
-> Seq Scan on t1 (cost=0.00..148609.21 rows=10007621 width=8) (actual time=0.023..0.023 rows=1 loops=1)
Planning Time: 426.693 ms
Execution Time: 390.852 ms
(7 rows)

5、例子二

sysdate 函数 为 stable 时:sysdate 函数在同一事务内多次执行返回的结果都相同,跨事务则每次结果不同。

test=# begin
test-# for i in 1..5 loop
test-# raise notice '%', sysdate();
test-# perform sys_sleep(1);
test-# commit;
test-# end loop;
test-# end;
test-# /
NOTICE: 2022-06-29 19:53:18
NOTICE: 2022-06-29 19:53:19
NOTICE: 2022-06-29 19:53:20
NOTICE: 2022-06-29 19:53:21
NOTICE: 2022-06-29 19:53:22
ANONYMOUS BLOCK
Time: 5011.031 ms (00:05.011) test=# begin
test-# for i in 1..5 loop
test-# raise notice '%', sysdate();
test-# perform sys_sleep(1);
test-# end loop;
test-# end;
test-# /
NOTICE: 2022-06-29 19:54:14
NOTICE: 2022-06-29 19:54:14
NOTICE: 2022-06-29 19:54:14
NOTICE: 2022-06-29 19:54:14
NOTICE: 2022-06-29 19:54:14
ANONYMOUS BLOCK
Time: 5005.724 ms (00:05.006)

  

sysdate 函数为 immutable 时:不管是否跨事务,sysdate 函数结果都相同。

test=# alter function sysdate immutable;
ALTER FUNCTION
Time: 6.276 ms

test=# begin
test-# for i in 1..5 loop
test-# raise notice '%', sysdate();
test-# perform sys_sleep(1);
test-# commit;
test-# end loop;
test-# end;
test-# /
NOTICE: 2022-06-29 19:54:58
NOTICE: 2022-06-29 19:54:58
NOTICE: 2022-06-29 19:54:58
NOTICE: 2022-06-29 19:54:58
NOTICE: 2022-06-29 19:54:58
ANONYMOUS BLOCK
Time: 5007.899 ms (00:05.008)

test=# begin
test-# for i in 1..5 loop
test-# raise notice '%', sysdate();
test-# perform sys_sleep(1);
test-# end loop;
test-# end;
test-# /
NOTICE: 2022-06-29 19:55:11
NOTICE: 2022-06-29 19:55:11
NOTICE: 2022-06-29 19:55:11
NOTICE: 2022-06-29 19:55:11
NOTICE: 2022-06-29 19:55:11
ANONYMOUS BLOCK
Time: 5007.694 ms (00:05.008)

  

二、Oracle

1、创建函数

create or replace function f001
return integer
deterministic
as
cnt integer;
begin
for i in 1..10 loop
select count(*) into cnt from t1 ;
end loop;
return cnt;
end ; create or replace function f002
return integer
as
cnt integer;
begin
for i in 1..10 loop
select count(*) into cnt from t1 ;
end loop;
return cnt;
end ;

2、单独explain 函数

可以看到两个函数都不会执行,不管是 deterministic,还是 volatile

SQL> explain plan for select f001() from dual;

Explained.

Elapsed: 00:00:00.00
SQL> explain plan for select f002() from dual; Explained. Elapsed: 00:00:00.00

3、实际执行

deterministic 只需执行一次,但是在SQL执行时才执行函数调用,而非explain时;volatile 解析时,不需要调用函数,而针对每个tuple 都必须要调用一次,如果没有记录,则无需调用。

SQL> select * from (select * from t1 where rownum<11) where id1=f001();

no rows selected

Elapsed: 00:00:00.48

SQL> select * from (select * from t1 where rownum<11) where id1=f002();

no rows selected

Elapsed: 00:00:05.01

SQL> select * from (select * from t1 where 1=2) where id1=f002();

no rows selected

Elapsed: 00:00:00.00

  

immutable 与 stable 函数的差异的更多相关文章

  1. PHP 日期计算函数【差异天数】

    function count_days($a,$b){ $a_dt=getdate($a); $b_dt=getdate($b); $a_new=mktime(12,0,0,$a_dt['mon'], ...

  2. 十八般武艺玩转GaussDB(DWS)性能调优:SQL改写

    摘要:本文将系统介绍在GaussDB(DWS)系统中影响性能的坏味道SQL及SQL模式,帮助大家能够从原理层面尽快识别这些坏味道SQL,在调优过程中及时发现问题,进行整改. 数据库的应用中,充斥着坏味 ...

  3. Oracle 与 PostgreSQL 函数行为的差异引发性能差异

    对于Oracle,对于数据修改的操作通过存储过程处理,而对于函数一般不进行数据修改操作.同时,函数可以通过 Select 进行调用,而存储过程则不行. 一.对于volatile 函数的行为 1.Ora ...

  4. KingbaseES 与Oracle 函数稳定性对于性能影响差异比较

    一.函数的属性 KingbaseES 函数在定义时有三种稳定性级别:volatile.stable 和 immutable.默认情况下,创建函数的稳定性为volatile.以下是这三种函数的区别: V ...

  5. PG 函数的易变性(Function Volatility Categories)

    此概念的接触是在做分区表的时候碰到的,分区表按时间字段分区,在查询时当where条件中时间为now()或者current_time()等时是无法查询的,即使进行格式转换也不行,只有是时间格式如‘201 ...

  6. CREATE FUNCTION - 定义一个新函数

    SYNOPSIS CREATE [ OR REPLACE ] FUNCTION name ( [ argtype [, ...] ] ) RETURNS rettype { LANGUAGE lang ...

  7. KingbaseES 函数稳定性与SQL性能

    背景:客户现场的一次艰苦的调优过程(https://www.cnblogs.com/kingbase/p/16015834.html),让我觉得非常有必要让数据库用户了解函数的不同稳定性属性,及其对于 ...

  8. 《zw版·delphi与halcon系列原创教程》zw版_THOperatorSetX控件函数列表 v11中文增强版

    <zw版·delphi与halcon系列原创教程>zw版_THOperatorSetX控件函数列表v11中文增强版 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就 ...

  9. 《zw版·delphi与halcon系列原创教程》zw版_THImagex控件函数列表

    <zw版·delphi与halcon系列原创教程>zw版_THImagex控件函数列表 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就要7w多行,但核心控件就是两 ...

随机推荐

  1. MySql查询日周月

    常用计算日期的函数 日 date(日期) = CURDATE() 自然周 YEARWEEK(date_format(日期,'%Y-%m-%d') , 1) = YEARWEEK(now() , 1) ...

  2. SAP 实例 5 CFW Events

    REPORT demo_custom_control . * Declarations ***************************************************** CL ...

  3. openGauss内核:SQL解析过程分析

    摘要:在传统数据库中SQL引擎一般指对用户输入的SQL语句进行解析.优化的软件模块.SQL的解析过程主要分为:词法.语法和语义分析. 本文分享自华为云社区< openGauss内核分析(三):S ...

  4. 业务可视化-让你的流程图"Run"起来

    前言 最近在研究业务可视化的问题,在日常的工作中,流程图和代码往往是分开管理的. 一个被维护多次的系统,到最后流程图和代码是否匹配这个都很难说. 于是一直有一个想法,让程序直接读流程图,根据流程图的配 ...

  5. Java之取余操作 "%"

    取模运算与取余运算两个概念有重叠的部分但又不完全一致.主要的区别在于对负整数进行除法运算时操作不同. 对于整形数a,b来说,取模运算或者求余运算的方法都是: 1.求 整数商 c = a / b: 2. ...

  6. idea插件和springboot镜像

    主题 https://blog.csdn.net/zyx1260168395/article/details/102928172 springboot镜像 http://start.springboo ...

  7. 1.9. 触摸按钮(touch pad)测试

    1.9.1. 基础 Esp32部分GPIO内置了touch按钮功能(电容式),具体有touch功能的引脚在配置为touchpad后,单片机读入的电容值随是否被触碰发生变化,系统根据电容值的变化判断判断 ...

  8. zabbix监控添加学习笔记

    在实际生产环境中,除了CPU.内存等一些系统信息可以挂载zabbix的自带模板Template OS Linux:但是一些公司开发的定制服务需要自己写模板或者监控项去监控: 一.监控公司的java服务 ...

  9. 枚举子集为什么是 O(3^n) 的

    这是更新日志 \(2021/2/9\) 代数推导 \(2021/2/10\) 组合意义,构建 TOC 目录 枚举子集 复杂度证明 代数推导 组合意义 Summary 枚举子集 枚举子集为什么是 \(O ...

  10. 当我们进行性能优化,我们在优化什么(LightHouse优化实操)

    好的互联网产品不仅仅在功能上要高人一筹,在性能层面也需要出类拔萃,否则金玉其外败絮其中,页面是美轮美奂了,结果首屏半天加载不出来,难免让用户乘兴而来,败兴而归. 幸运的是,前端的性能优化有诸多有迹可循 ...