如何解决带有大量插入语句的Postgresql脚本会降低性能
我们有一个MS sql服务器脚本,该脚本进行性能测试以测试客户服务器的运行速度。
这是对我们的产品如何在批处理作业中加载数据的简单/简单的模拟,通常,它在具有SSD的快速PC上运行大约11秒,在具有NVME磁盘的Ryzen服务器上运行大约8秒,大约20秒在没有SSD磁盘的较旧(例如2013)服务器上,需要-30秒。我们曾经有一个客户安装了一个VM,该VM花费39秒来运行脚本,所以我们知道该VM存在问题,因此它是确定相对服务器性能的非常好的脚本。
最近我将脚本移植到了postgres,因为我们想摆脱mssql的许可费用,但是该脚本需要大约35分钟的时间才能运行,所以我想知道是否有我需要的
postgres实例安装在具有SSD磁盘的快速Windows 10 PC上,并且sql server中的相同脚本需要10-11秒。它是一个普通的安装,我们对postgres的了解还不足以对其进行优化。
当然,真正的问题是-“ postgres真的那么慢吗?”如果没有,我们应该能够做一些调整以加快速度,这将告诉我们移植我们的产品以支持postgres是否可行。
这是脚本代码。如果要运行循环,则可能需要减少循环次数:
DROP PROCEDURE IF EXISTS zx();
CREATE OR REPLACE PROCEDURE zx() LANGUAGE plpgsql
AS $$
DECLARE startdate date;
enddate date;
i INTEGER;
j INTEGER;
g1 VARCHAR(255);
g2 VARCHAR(255);
g3 VARCHAR(255);
g4 VARCHAR(255);
g5 VARCHAR(255);
BEGIN
create table TestTable(id int DEFAULT NEXTVAL ('TestTable_seq') not NULL
PRIMARY KEY,textfield1 varchar(255),textfield2 varchar(255),textfield3 varchar(255),textfield4 varchar(255),textfield5 varchar(255))
;
create table TestTableSub(id int DEFAULT NEXTVAL ('TestTableSub_seq') not null PRIMARY KEY,textfield5 varchar(255))
;
i := 0;
while i < 1000 LOOP
g1 := uuid_generate_v4 ();
g2 := uuid_generate_v4 ();
g3 := uuid_generate_v4 ();
g4 := uuid_generate_v4 ();
g5 := uuid_generate_v4 ();
insert into testtable(textfield1,textfield2,textfield3,textfield4,textfield5) values (g1,g2,g3,g4,g5);
j := 0;
while j < 1000 LOOP
g1 := uuid_generate_v4 ();
g2 := uuid_generate_v4 ();
g3 := uuid_generate_v4 ();
g4 := uuid_generate_v4 ();
g5 := uuid_generate_v4 ();
insert into testtablesub(textfield1,g5);
j := j + 1;
END LOOP;
COMMIT;
i := i + 1;
END LOOP;
END;
$$
--drop table TestTable;
--drop table TestTableSub;
--drop sequence TestTable;
--drop sequence TestTableSub;
CALL zx();
-- SELECT Count(*) FROM TestTable;
-- SELECT Count(*) FROM TestTableSub;
-- drop table TestTable;
-- drop table TestTableSub;
如果您有兴趣,这是sql服务器中的同一脚本,需要花费11秒的时间
dbcc dropcleanbuffers
CREATE DATABASE PerfTest ON PRIMARY
use PerfTest
set nocount on
declare @startdate datetime
select @startdate = getdate()
create table TestTable(id int identity(1,1) not null,textfield1 nvarchar(255),textfield2 nvarchar(255),textfield3 nvarchar(255),textfield4 nvarchar(255),textfield5 nvarchar(255))
create table TestTableSub(id int identity(1,textfield5 nvarchar(255))
begin tran
declare @i int
select @i = 0
while @i < 1000
begin
declare @g1 nvarchar(255),@g2 nvarchar(255),@g3 nvarchar(255),@g4 nvarchar(255),@g5 nvarchar(255)
select @g1 = newid(),@g2 = newid(),@g3 = newid(),@g4 = newid(),@g5 = newid()
insert into testtable(textfield1,textfield5) values (@g1,@g2,@g3,@g4,@g5)
commit
begin tran
declare @j int
select @j = 0
while @j < 1000
begin
select @g1 = newid(),@g5 = newid()
insert into testtablesub(textfield1,@g5)
select @j = @j + 1
end
select @i = @i + 1
end
commit
declare @enddate datetime
select @enddate = getdate()
select @@servername,db_name(),@startdate,@enddate,datediff(MILLISECOND,@enddate)
select * from sysfiles
drop table TestTable
drop table TestTableSub
use master
Drop database PerfTest
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。