标签:count 存储 PostgreSQL -- dynamic bigint sql getDictionarys
虽然真的很鸡肋,并且性能不杂滴,但聊胜于无吧.
drop function if exists exe_dynamic_sql(bigint);
drop function if exists exe_dynamic_count(bigint);
--返回记录集
create or replace function exe_dynamic_sql(ival bigint)
returns table(objectid bigint,name varchar(128))
as $$
declare
begin
return query execute 'select objectid,name from dictionarys where parentid=$1 order by parentid,sort' using $1;
end;
$$ language plpgsql;
--赋值给变量
create or replace function exe_dynamic_count(ival bigint)
returns bigint
as $$
declare
v_count bigint;
begin
execute 'select count(*) from dictionarys where parentid=$1' using $1 into v_count;
return v_count;
end;
$$ language plpgsql;
--测试
select * from exe_dynamic_sql(26);
select exe_dynamic_count(26);
要点:
- returns table(objectid bigint,name varchar(128)),定义返回的字段和类型
- using $1执行时使用过程参数;
准备一个语句用于执行,这个就比较重要了,查询参数绑定,开发利器
有朋友抱怨同一sql时快时慢,这是因为值在表中的占比不同,占比小的值就可以使用索引,值占比超过5%里sql就很慢了.此时就可以用下面的sql调式sql,可以根据不同的值来观察执行计划.
--getDictionarys仅在当前会话下有效
prepare getDictionarys (bigint) as
select objectid,name from dictionarys where parentid=$1 order by parentid,sort;
explain (analyze,verbose,costs,buffers,timing)
execute getDictionarys(24);
explain (analyze,verbose,costs,buffers,timing)
execute getDictionarys(25);
explain (analyze,verbose,costs,buffers,timing)
execute getDictionarys(26);
--释放指定的预备语句
deallocate getDictionarys;
--释放所有预备语句
deallocate all;
标签:count,存储,PostgreSQL,--,dynamic,bigint,sql,getDictionarys 来源: https://blog.csdn.net/qq_36421955/article/details/113844386
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。