PostgreSQL 中插入缓慢

发布于 2024-10-16 04:09:40 字数 1891 浏览 2 评论 0原文

我在表中执行 INSERT 时遇到问题。

表结构是:

uri (varchar 10000) PK
id_language (varchar 10) PK
id_category (int4) PK
id_data (varchar 50) PK
id_ordinal (int4) PK (this field have a trigger to auto increment)
n_text (text)

当我运行这个函数来执行 900000 个 INSERT 时,它运行得又好又快:

CREATE OR REPLACE FUNCTION doInserts() RETURNS integer AS
$BODY$
DECLARE
   i integer;
BEGIN
       i := 1;

       while i <= 900000 loop
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'pagetitle', 'Pagina teste ' || i);

               i := i + 1;
       end loop;
   RETURN i;
END
$BODY$
LANGUAGE 'plpgsql' ;

但是当我用 100000 个 INSERT 执行此操作时,它似乎永远不会结束 INSERT 的操作,现在运行在 5 小时...

CREATE OR REPLACE FUNCTION doInserts() RETURNS integer AS
$BODY$
DECLARE
   i integer;
BEGIN
       i := 1;

       while i <= 100000 loop
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'pagetitle', 'Pagina teste ' || i);
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'country_ad', 'italy');
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'services_available', 'service 1');
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'services_available', 'service 2');

               i := i + 1;
       end loop;
   RETURN i;
END
$BODY$
LANGUAGE 'plpgsql' ;

这里可能有什么问题?有什么线索吗?会不会是钥匙的问题?

还有一则信息。在我插入 900000 个寄存器后,我使用“DELETE FROM”来删除寄存器,然后我运行了仅插入 100000 个寄存器的功能

最好的问候,

I have a problem when doing INSERT's in a table.

The table structure is:

uri (varchar 10000) PK
id_language (varchar 10) PK
id_category (int4) PK
id_data (varchar 50) PK
id_ordinal (int4) PK (this field have a trigger to auto increment)
n_text (text)

When I run this function to do 900000 INSERT's it runs well and fast:

CREATE OR REPLACE FUNCTION doInserts() RETURNS integer AS
$BODY$
DECLARE
   i integer;
BEGIN
       i := 1;

       while i <= 900000 loop
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'pagetitle', 'Pagina teste ' || i);

               i := i + 1;
       end loop;
   RETURN i;
END
$BODY$
LANGUAGE 'plpgsql' ;

But when I do this with 100000 INSERT's it seems to never end the
INSERT's operation, It is running at 5h now...

CREATE OR REPLACE FUNCTION doInserts() RETURNS integer AS
$BODY$
DECLARE
   i integer;
BEGIN
       i := 1;

       while i <= 100000 loop
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'pagetitle', 'Pagina teste ' || i);
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'country_ad', 'italy');
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'services_available', 'service 1');
               insert into tdir_uris_text (uri, id_language, id_category, id_data, n_text) values ('http://localhos/teste' || i, 'PORT', '2', 'services_available', 'service 2');

               i := i + 1;
       end loop;
   RETURN i;
END
$BODY$
LANGUAGE 'plpgsql' ;

What could be the problem here? Any clues? Could be the keys problem?

One more information. After I have inserted the 900000 registers I have used a "DELETE FROM" to delete the registers then I have run the Function that only inserts 100000 registers

Best Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者 2024-10-23 04:09:40

使用序列而不是触发器。每次测试时,请确保截断而不是删除。

Use a sequence not a trigger. Everytime you test, make sure you truncate not delete.

灯下孤影 2024-10-23 04:09:40

(该字段有一个自动触发器
增量)

为什么要使用触发器?你不能使用 SERIAL (又名 SEQUENCE )吗?

100.000 个具有相同值的插入是一个很好的测试,但与实际使用情况无关。这 100,000 个插入在同一事务中运行,因此您必须针对相当大的事务优化配置设置。内存、WAL 等的设置是什么?

(this field have a trigger to auto
increment)

Why do you use a trigger? Can't you use a SERIAL (a.k.a. SEQUENCE) ?

And 100.000 insert's with the same value is a nice test, but has nothing to do with real live usage. These 100.000 inserts run within the same transaction, so you have to optimize the configuration settings for pretty large transactions. What are your settings for memory, WAL, etc. ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文