pg.ddx.io pgsql-performance@postgresql.org mailing list archive
help / color / mirror / Atom feedMultiple insert performance trick or performance misunderstanding?
2+ messages / 2 participants
[nested] [flat]
* Multiple insert performance trick or performance misunderstanding?
@ 2005-09-24 20:51 Ron Mayer <rm_pg@cheapcomplexdevices.com>
0 siblings, 1 reply; 2+ messages in thread
From: Ron Mayer @ 2005-09-24 20:51 UTC (permalink / raw)
To: pgsql-performance
When I need to insert a few hundred or thousand things in
a table from a 3-tier application, it seems I'm much better
off creating a big string of semicolon separated insert
statements rather than sending them one at a time - even
when I use the obvious things like wrapping the statements
in a transaction and using the library's prepared statements.
I tried both Ruby/DBI and C#/Npgsql; and in both cases
sets of inserts that took 3 seconds when run individually
took about 0.7 seconds when concatenated together.
Is it expected that I'd be better off sending big
concatenated strings like
"insert into tbl (c1,c2) values (v1,v2);insert into tbl (c1,c2) values (v3,v4);..."
instead of sending them one at a time?
db.ExecuteSQL("BEGIN");
sql = new System.Text.StringBulder(10000);
for ([a lot of data elements]) {
sql.Append(
"insert into user_point_features (col1,col2)"+
" values (" +obj.val1 +","+obj.val2+");"
);
}
db.ExecuteSQL(sql.ToString());
db.ExecuteSQL("COMMIT");
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Multiple insert performance trick or performance misunderstanding?
@ 2005-09-24 21:15 Tom Lane <tgl@sss.pgh.pa.us>
parent: Ron Mayer <rm_pg@cheapcomplexdevices.com>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2005-09-24 21:15 UTC (permalink / raw)
To: Ron Mayer <rm_pg@cheapcomplexdevices.com>; +Cc: pgsql-performance
Ron Mayer <rm_pg@cheapcomplexdevices.com> writes:
> Is it expected that I'd be better off sending big
> concatenated strings like
> "insert into tbl (c1,c2) values (v1,v2);insert into tbl (c1,c2) values (v3,v4);..."
> instead of sending them one at a time?
It's certainly possible, if the network round trip from client to server
is slow. I do not think offhand that there is any material advantage
for the processing within the server (assuming you've wrapped the whole
thing into one transaction in both cases); if anything, the
concatenated-statement case is probably a bit worse inside the server
because it will transiently eat more memory. But network latency or
client-side per-command overhead could well cause the results you see.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2005-09-24 21:15 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-24 20:51 Multiple insert performance trick or performance misunderstanding? Ron Mayer <rm_pg@cheapcomplexdevices.com>
2005-09-24 21:15 ` Tom Lane <tgl@sss.pgh.pa.us>
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