pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Lendvary Gyorgy <gyurika@prolan.hu>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] questions
Date: Tue, 9 Jun 1998 15:55:46 +0300
Message-ID: <l03110705b1a2df4f8fb2@[147.233.159.109]> (raw)
In-Reply-To: <357D2423.52EEFBA0@prolan.hu>

At 15:01 +0300 on 9/6/98, Lendvary Gyorgy wrote:


> sprintf(buff, "CREATE TABLE boci (tup_num int, hapci int)");
> PQexec(conn, buff);
> for (i=0; i<10000; i++)
> {
>     sprintf(buff, "INSERT INTO boci VALUES(i)");
>     PQexec(conn, buff);
> }
>
> for (i=0; i<10000; i++)
> {
>     x = GetValue(); /* GetValue isn't an interesting function */
>     sprintf(buff, "UPDATE boci SET hapci = %d WHERE tup_num = %d", x,
> i);
>     PQexec(conn, buff);
> }
>
> I don't want to update every tuple indvidually but I want to prepare a
> 'block write'. I hope you understand what I'd like to.
> Can you give me a good method for saving a long time? I need a program
> that is about 10 times faster than mine.

Batch inserts can be done faster with COPY rather than insert. I don't see
why you first prepare your table and only then fill it with values. First,
the record number could very well be created with a sequence, but even if
you want to do it programmatically, you should make it much faster (and
more efficient in disk space) if you insert the x directly.

Read the description of how to use COPY in the libpq manual. In general, it
would need to PQexec a COPY command, and then use PQputline inside the loop
and PQendcopy after it. Do all this inside a transaction block, and you'll
get the fastest results for an insert.

If you still need to programmatically update the lines afterwards, it must
be done with UPDATE. There's no help for it. But still, you can use it
inside a transaction block to improve speed. If all you need to do is
replace the values of hapci throughout, you can simply drop all the lines
and use a new COPY.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





view thread (2+ messages)

Message-ID: <l03110705b1a2df4f8fb2@[147.233.159.109]>
Permalink:  ../l03110705b1a2df4f8fb2@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03110705b1a2df4f8fb2@[147.233.159.109]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: herouth@oumail.openu.ac.il, gyurika@prolan.hu
  Subject: Re: [SQL] questions
  In-Reply-To: <l03110705b1a2df4f8fb2@[147.233.159.109]>
  References: <357D2423.52EEFBA0@prolan.hu> <l03110705b1a2df4f8fb2@[147.233.159.109]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox