pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: sarlav kumar <sarlavk@yahoo.com>
To: pgsqlperform <pgsql-performance@postgresql.org>
To: pgsql-novice@postgresql.org
Subject: INSERT question
Date: Mon, 13 Dec 2004 08:28:39 -0800 (PST)
Message-ID: <20041213162839.60931.qmail@web51303.mail.yahoo.com> (raw)

Hi All,
 
I have a question regarding multiple inserts.
The following function inserts for each country found in country table, values into merchant_buyer_country.
 
----------------------------------------------------------------------------------------------------------------------------------------
        CSQLStatement st( sql );
        CSQLStatement st1( sql );
        SQLINTEGER rows;
        long num_codes = 0;
        rows = st.Select( "SELECT * FROM merchant_buyer_country where merchant_id = %lu ",merchant_id );
        if  ( rows )
                return 0;
    char code[4];
        rows = st.Select( "SELECT code FROM country WHERE send IS NOT NULL OR receive IS NOT NULL" );
    SQLBindCol( st.hstmt, 1, SQL_C_CHAR, code, sizeof(code), 0 );
   long i;
   for (i = 0; i < rows; i++ )
   {
          st.Fetch();
          st1.Command("INSERT INTO merchant_buyer_country (merchant_id,country,enabled,group_id)  VALUES(%lu ,'%s', true, %lu )", merchant_id,
 code,group_id);
   }
        st.CloseCursor();
    st1.CloseCursor();
        return 1;
----------------------------------------------------------------------------------------------------------------------------------------

On looking at the log file, I saw separate inserts being performed, and each insert takes about 1 second. 
 
insert into merchant_buyer_country (merchant_id,country,enabled,group_id) values(1203,'IN','true',1);
insert into merchant_buyer_country merchant_id,country,enabled,group_id)  values(1203,'US','true',1);
insert into merchant_buyer_country merchant_id,country,enabled,group_id) values (1203,'AR','true',1);
insert into merchant_buyer_country (merchant_id,country,enabled,group_id) values(1203,'AZ','true',1);
insert into merchant_buyer_country merchant_id,country,enabled,group_id) values (1203,'BG','true',1);
insert into merchant_buyer_country merchant_id,country,enabled,group_id) values(1203,'SP','true',1);
.....
 





There are more than 100 countries and this takes a lot of time for the inserts to complete. 
Is there a way to write the INSERT as follows?
 
INSERT into merchant_buyer_country (merchant_id,country,enabled,group_id)  values (1203, 
(SELECT code FROM country WHERE send IS NOT NULL OR receive IS NOT NULL), 'true',1);
 
I tried this, but I get the following problem:
ERROR:  More than one tuple returned by a subselect used as an expression.
 
I know there is a way to this, but I am not sure where I am going wrong. Can someone please help me figure this out.
 
Thanks,
Saranya
 

		
---------------------------------
Do you Yahoo!?
 Meet the all-new My Yahoo! – Try it today! 

view thread (6+ messages)  latest in thread

Message-ID: <20041213162839.60931.qmail@web51303.mail.yahoo.com>
Permalink:  ../20041213162839.60931.qmail@web51303.mail.yahoo.com/
Also on:    postgresql.org/message-id/20041213162839.60931.qmail@web51303.mail.yahoo.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: sarlavk@yahoo.com, pgsql-novice@postgresql.org
  Subject: Re: INSERT question
  In-Reply-To: <20041213162839.60931.qmail@web51303.mail.yahoo.com>

* 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