首页 > 数据库 >PostgreSQL 函数稳定性在索引与全表访问下的性能差异

PostgreSQL 函数稳定性在索引与全表访问下的性能差异

时间:2022-09-23 17:56:21浏览次数:52  
标签:rows PostgreSQL .. 索引 全表 ms Time test id

 

一、构建测试数据

create or replace function test_volatile(id integer)
    returns bigint
    volatile
    language sql
as
$$ select count(*) from t1 $$ ;
/
 
create or replace function test_stable(id integer)
    returns bigint
    stable
    language sql
as
$$ select count(*) from t1 $$ ;
/
 
create or replace function test_immutable(id integer)
    returns bigint
    immutable
    language sql
as
$$ select count(*) from t1 $$ ;
/

insert into test select 1 from generate_series(1,1000);

 

二、没有索引情况下执行性能

这里的索引是指 test(id) 索引,显然,因为 id 值都是相同的,这里的索引实际意义不大。

test=# explain analyze select count(*) from test where id=test_volatile(1);
                                                   QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=270.00..270.01 rows=1 width=8) (actual time=16154.566..16154.566 rows=1 loops=1)
   ->  Seq Scan on test  (cost=0.00..267.50 rows=1000 width=0) (actual time=16154.564..16154.564 rows=0 loops=1)
         Filter: (id = test_volatile(1))
         Rows Removed by Filter: 1000
 Planning Time: 0.165 ms
 Execution Time: 16154.585 ms
(6 rows)

Time: 16155.404 ms (00:16.155)
test=#
test=# explain analyze select count(*) from test where id=test_stable(1);
                                                  QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=267.50..267.51 rows=1 width=8) (actual time=16401.441..16401.441 rows=1 loops=1)
   ->  Seq Scan on test  (cost=0.00..267.50 rows=1 width=0) (actual time=16401.439..16401.439 rows=0 loops=1)
         Filter: (id = test_stable(1))
         Rows Removed by Filter: 1000
 Planning Time: 28.010 ms
 Execution Time: 16401.473 ms
(6 rows)

Time: 16430.319 ms (00:16.430)

test=# explain analyze select count(*) from test where id=test_immutable(1);
                                             QUERY PLAN
-----------------------------------------------------------------------------------------------------
 Aggregate  (cost=17.50..17.51 rows=1 width=8) (actual time=0.065..0.065 rows=1 loops=1)
   ->  Seq Scan on test  (cost=0.00..17.50 rows=1 width=0) (actual time=0.064..0.064 rows=0 loops=1)
         Filter: (id = '100000'::bigint)
         Rows Removed by Filter: 1000
 Planning Time: 44.469 ms
 Execution Time: 0.083 ms
(6 rows)

Time: 45.197 ms

  

三、创建索引情况下的执行性能

create index idx_test_id on test(id);

test=# explain analyze select count(*) from test where id=test_volatile(1);
                                                   QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=270.00..270.01 rows=1 width=8) (actual time=16497.225..16497.226 rows=1 loops=1)
   ->  Seq Scan on test  (cost=0.00..267.50 rows=1000 width=0) (actual time=16497.223..16497.223 rows=0 loops=1)
         Filter: (id = test_volatile(1))
         Rows Removed by Filter: 1000
 Planning Time: 0.438 ms
 Execution Time: 16497.258 ms
(6 rows)

Time: 16498.229 ms (00:16.498)
test=# explain analyze select count(*) from test where id=test_stable(1);
                                                          QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=4.55..4.56 rows=1 width=8) (actual time=17.419..17.419 rows=1 loops=1)
   ->  Index Only Scan using idx_test_id on test  (cost=0.53..4.54 rows=1 width=0) (actual time=17.417..17.417 rows=0 loops=1)
         Index Cond: (id = test_stable(1))
         Heap Fetches: 0
 Planning Time: 16.875 ms
 Execution Time: 17.511 ms
(6 rows)

Time: 34.742 ms
test=# explain analyze select count(*) from test where id=test_immutable(1);
                                                         QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=4.30..4.31 rows=1 width=8) (actual time=0.011..0.011 rows=1 loops=1)
   ->  Index Only Scan using idx_test_id on test  (cost=0.28..4.29 rows=1 width=0) (actual time=0.009..0.009 rows=0 loops=1)
         Index Cond: (id = '100000'::bigint)
         Heap Fetches: 0
 Planning Time: 18.673 ms
 Execution Time: 0.032 ms
(6 rows)

Time: 19.042 ms

  

四、结论

1、对于 volatile 类型的函数,由于不同时刻函数结果可能不同,从安全角度需要逐行调用函数。

2、这里重点关注的 test_stable 函数:使用全表扫描,每行都要执行一次。使用索引,只需执行一次。

标签:rows,PostgreSQL,..,索引,全表,ms,Time,test,id
From: https://www.cnblogs.com/kingbase/p/16723605.html

相关文章

  • PostgreSQL 视图练习
    练习来源于《SQL基础教程(第2版)》5.1创建初满足下述三个条件的视图(视图名称为ViewPractice5_1)shop=#createviewViewPractice5_1shop-#asshop-#selectproduct_ty......
  • Centos7设置postgresql数据库开机自启动
    前言PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下如果不知道具体的路径,可以用find命令进行查找。命令如下:[root@admin~]#find/-......
  • Oracle索引并行度
    一.Rebuild索引与并行度说明在索引create和rebuild的时候,在CPU允许的情况下,我们可以使用parallel来加快操作的速度。但是这里有一个注意的问题,有关索引的并行度,这个对表......
  • 【灵光一闪】新的百度搜索引擎使用思路
    程序员行业有一句俗话,叫做“面向百度编程”、“面向CV编程”,就是指当我们遇到某些问题的时候回去百度上寻找答案,然后复制黏贴过来,虽然在程序员行业中百度搜索引擎的口碑始......
  • 8. Numpy索引和切片
    1.前言在NumPy中,如果想要访问,或修改数组中的元素,您可以采用索引或切片的方式,比如使用从0开始的索引依次访问数组中的元素,这与Python的list列表是相同的。NumPy......
  • elasticsearch的.security-7索引崩溃恢复笔记
    装了es三方插件重启后出现以下问题failedtoauthenticateuser[elastic]failedtoretrievepasswordhashforreserveduser[elastic]org.elasticsearch.action.......
  • PostgreSQL与Greenplum之区别
    一、区别:(1)Greenplum是用于报告、分析、机器学习、人工智能和高并发SQL的大规模并行数据库的开源软件。Greenplum以PostgreSQL为基础,Greenplum数据库被描述为以MP......
  • mysql索引优化:唯一性太低的字段
    除非特殊情况,否则不要在唯一性太低的字段上面添加索引。 什么叫唯一性太低?主要是一些表示状态或者类型的字段,例如:性别、是否删除等。 为何?mysql在查询时,会将索引中......
  • 数据库索引问题总结
    一、主流MySQL引擎1.isam对select和insert的处理速度更快,但不支持外键和事务安全2.V5.5后innodb是默认引擎,速度稍微逊色一点,但对事务和并发的处理更好。二、索引建立......
  • Python——索引与切片
    #索引与切片##1.序列序列:list,tuple,str其中list是可变序列typle,str是不可变序列#修改序列的值list=[3,4,5]tup=(3,4,5)str='345'list[1]=99listoutp......