pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedINSERT question
6+ messages / 4 participants
[nested] [flat]
* INSERT question
@ 2004-12-13 16:28 sarlav kumar <sarlavk@yahoo.com>
0 siblings, 3 replies; 6+ messages in thread
From: sarlav kumar @ 2004-12-13 16:28 UTC (permalink / raw)
To: pgsql-performance; pgsql-novice@postgresql.org
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!
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: INSERT question
@ 2004-12-13 16:48 Tom Lane <tgl@sss.pgh.pa.us>
parent: sarlav kumar <sarlavk@yahoo.com>
2 siblings, 1 reply; 6+ messages in thread
From: Tom Lane @ 2004-12-13 16:48 UTC (permalink / raw)
To: sarlav kumar <sarlavk@yahoo.com>; +Cc: pgsql-novice@postgresql.org
sarlav kumar <sarlavk@yahoo.com> writes:
> 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.
INSERT into merchant_buyer_country (merchant_id,country,enabled,group_id)
SELECT 1203, code, 'true', 1 FROM country
WHERE send IS NOT NULL OR receive IS NOT NULL;
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: INSERT question
@ 2004-12-13 16:49 Bruno Wolff III <bruno@wolff.to>
parent: sarlav kumar <sarlavk@yahoo.com>
2 siblings, 1 reply; 6+ messages in thread
From: Bruno Wolff III @ 2004-12-13 16:49 UTC (permalink / raw)
To: sarlav kumar <sarlavk@yahoo.com>; +Cc: pgsql-performance; pgsql-novice@postgresql.org
On Mon, Dec 13, 2004 at 08:28:39 -0800,
sarlav kumar <sarlavk@yahoo.com> wrote:
>
> 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);
>
You have to use a SELECT instead of the VAlues clause. Something like the
following should work:
INSERT INTO merchant_buyer_country (merchant_id, country, enabled, group_id)
SELECT 1203, code, TRUE, 1 FROM country
WHERE send IS NOT NULL OR receive IS NOT NULL
;
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: INSERT question
@ 2004-12-13 18:31 sarlav kumar <sarlavk@yahoo.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 6+ messages in thread
From: sarlav kumar @ 2004-12-13 18:31 UTC (permalink / raw)
To: pgsql-novice@postgresql.org
Thanks!! that worked!:)
Tom Lane <tgl@sss.pgh.pa.us> wrote:sarlav kumar writes:
> 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.
INSERT into merchant_buyer_country (merchant_id,country,enabled,group_id)
SELECT 1203, code, 'true', 1 FROM country
WHERE send IS NOT NULL OR receive IS NOT NULL;
regards, tom lane
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: INSERT question
@ 2004-12-13 18:33 sarlav kumar <sarlavk@yahoo.com>
parent: Bruno Wolff III <bruno@wolff.to>
0 siblings, 0 replies; 6+ messages in thread
From: sarlav kumar @ 2004-12-13 18:33 UTC (permalink / raw)
To: pgsql-performance
Thanks guys!! that worked!:)
Michael Adler <adler@pobox.com> wrote:
On Mon, Dec 13, 2004 at 08:28:39AM -0800, sarlav kumar wrote:
> 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.
INSERT into merchant_buyer_country (merchant_id,country,enabled,group_id)
SELECT 1203, code FROM country WHERE send IS NOT NULL OR receive IS NOT NULL;
-Mike Adler
Bruno Wolff III <bruno@wolff.to> wrote:
On Mon, Dec 13, 2004 at 08:28:39 -0800,
sarlav kumar wrote:
>
> 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);
>
You have to use a SELECT instead of the VAlues clause. Something like the
following should work:
INSERT INTO merchant_buyer_country (merchant_id, country, enabled, group_id)
SELECT 1203, code, TRUE, 1 FROM country
WHERE send IS NOT NULL OR receive IS NOT NULL
;
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
---------------------------------
Do you Yahoo!?
All your favorites on one personal page Try My Yahoo!
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: [PERFORM] INSERT question
@ 2004-12-29 18:58 george young <gry@ll.mit.edu>
parent: sarlav kumar <sarlavk@yahoo.com>
2 siblings, 0 replies; 6+ messages in thread
From: george young @ 2004-12-29 18:58 UTC (permalink / raw)
To: sarlav kumar <sarlavk@yahoo.com>; +Cc: pgsql-novice@postgresql.org
On Mon, 13 Dec 2004 08:28:39 -0800 (PST)
sarlav kumar <sarlavk@yahoo.com> threw this fish to the penguins:
> 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.
Try:
insert into merchant_buyer_country select 1203,code,true,1 from country where send is not null or receive is not null;
-- George Young
--
"Are the gods not just?" "Oh no, child.
What would become of us if they were?" (CSL)
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2004-12-29 18:58 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-12-13 16:28 INSERT question sarlav kumar <sarlavk@yahoo.com>
2004-12-13 16:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2004-12-13 18:31 ` sarlav kumar <sarlavk@yahoo.com>
2004-12-13 16:49 ` Bruno Wolff III <bruno@wolff.to>
2004-12-13 18:33 ` sarlav kumar <sarlavk@yahoo.com>
2004-12-29 18:58 ` george young <gry@ll.mit.edu>
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