pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bulk Inserts
9+ messages / 3 participants
[nested] [flat]

* Bulk Inserts
@ 2019-08-09 22:06 Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Souvik Bhattacherjee @ 2019-08-09 22:06 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,

I'm trying to measure the performance of the following: Multiple txns
inserting tuples into a table concurrently vs single txn doing the whole
insertion.

*new table created as:*
create table tab2 (
id serial,
attr1 integer not null,
attr2 integer not null,
primary key(id)
);

*EXP 1: inserts with multiple txn:*
insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where attr2
= 10);
insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where attr2
= 20);

note: attr2 has only two values 10 and 20

*EXP 2: inserts with a single txn:*
insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1);

I also performed another experiment as follows:
*EXP 3:* select attr1, attr2 into tab2 from tab1;

The observation here is EXP 3  is much faster than EXP 2 probably due to
bulk inserts used by Postgres. However I could not find a way to insert id
values in tab2 using EXP 3. Also select .. into .. from .. throws an error
if we create a table first and then populate the tuples using the command.

I have the following questions:
1. Is it possible to have an id column in tab2 and perform a bulk insert
using select .. into .. from .. or using some other means?
2. If a table is already created, is it possible to do bulk inserts via
multiple txns inserting into the same table (EXP 3)?

Best,
-SB

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-08-09 23:25 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-11 02:47   ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Adrian Klaver @ 2019-08-09 23:25 UTC (permalink / raw)
  To: Souvik Bhattacherjee <kivuosb@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 8/9/19 3:06 PM, Souvik Bhattacherjee wrote:
> Hi,
> 
> I'm trying to measure the performance of the following: Multiple txns 
> inserting tuples into a table concurrently vs single txn doing the whole 
> insertion.
> 
> *new table created as:*
> create table tab2 (
> id serial,
> attr1 integer not null,
> attr2 integer not null,
> primary key(id)
> );
> 
> *EXP 1: inserts with multiple txn:*
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> attr2 = 10);
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> attr2 = 20);
> 
> note: attr2 has only two values 10 and 20
> 
> *EXP 2: inserts with a single txn:*
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1);
> 
> I also performed another experiment as follows:
> *EXP 3:* select attr1, attr2 into tab2 from tab1;
> 
> The observation here is EXP 3  is much faster than EXP 2 probably due to 
> bulk inserts used by Postgres. However I could not find a way to insert 
> id values in tab2 using EXP 3. Also select .. into .. from .. throws an 
> error if we create a table first and then populate the tuples using the 
> command.

Yes as SELECT INTO is functionally the same as CREATE TABLE AS:

https://www.postgresql.org/docs/11/sql-selectinto.html

> 
> I have the following questions:
> 1. Is it possible to have an id column in tab2 and perform a bulk insert 
> using select .. into .. from .. or using some other means?

Not using SELECT INTO for reasons given above.
Though it is possible to SELECT INTO as you show in EXP 3 and then:
	alter table tab2 add column id serial primary key;
EXP 2 shows the other means.

> 2. If a table is already created, is it possible to do bulk inserts via 
> multiple txns inserting into the same table (EXP 3)?

Yes, but you will some code via client or function that batches the 
inserts for you.

> 
> Best,
> -SB


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-08-11 02:47   ` Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-11 03:01     ` Re: Bulk Inserts Rob Sargent <robjsargent@gmail.com>
  2019-08-11 15:11     ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 9+ messages in thread

From: Souvik Bhattacherjee @ 2019-08-11 02:47 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi Adrian,

Thanks for the response.

> Yes, but you will some code via client or function that batches the
> inserts for you.

Could you please elaborate a bit on how EXP 1 could be performed such that
it uses bulk inserts?

Best,
-SB

On Fri, Aug 9, 2019 at 7:26 PM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 8/9/19 3:06 PM, Souvik Bhattacherjee wrote:
> > Hi,
> >
> > I'm trying to measure the performance of the following: Multiple txns
> > inserting tuples into a table concurrently vs single txn doing the whole
> > insertion.
> >
> > *new table created as:*
> > create table tab2 (
> > id serial,
> > attr1 integer not null,
> > attr2 integer not null,
> > primary key(id)
> > );
> >
> > *EXP 1: inserts with multiple txn:*
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
> > attr2 = 10);
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
> > attr2 = 20);
> >
> > note: attr2 has only two values 10 and 20
> >
> > *EXP 2: inserts with a single txn:*
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1);
> >
> > I also performed another experiment as follows:
> > *EXP 3:* select attr1, attr2 into tab2 from tab1;
> >
> > The observation here is EXP 3  is much faster than EXP 2 probably due to
> > bulk inserts used by Postgres. However I could not find a way to insert
> > id values in tab2 using EXP 3. Also select .. into .. from .. throws an
> > error if we create a table first and then populate the tuples using the
> > command.
>
> Yes as SELECT INTO is functionally the same as CREATE TABLE AS:
>
> https://www.postgresql.org/docs/11/sql-selectinto.html
>
> >
> > I have the following questions:
> > 1. Is it possible to have an id column in tab2 and perform a bulk insert
> > using select .. into .. from .. or using some other means?
>
> Not using SELECT INTO for reasons given above.
> Though it is possible to SELECT INTO as you show in EXP 3 and then:
>         alter table tab2 add column id serial primary key;
> EXP 2 shows the other means.
>
> > 2. If a table is already created, is it possible to do bulk inserts via
> > multiple txns inserting into the same table (EXP 3)?
>
> Yes, but you will some code via client or function that batches the
> inserts for you.
>
> >
> > Best,
> > -SB
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-11 02:47   ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-08-11 03:01     ` Rob Sargent <robjsargent@gmail.com>
  2019-08-11 03:35       ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Rob Sargent @ 2019-08-11 03:01 UTC (permalink / raw)
  To: Souvik Bhattacherjee <kivuosb@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>



> On Aug 10, 2019, at 8:47 PM, Souvik Bhattacherjee <kivuosb@gmail.com> wrote:
> 
> Hi Adrian,
> 
> Thanks for the response.
> 
> > Yes, but you will some code via client or function that batches the 
> > inserts for you.
> 
> Could you please elaborate a bit on how EXP 1 could be performed such that it uses bulk inserts?
> 
> Best,
> -SB
> 
> On Fri, Aug 9, 2019 at 7:26 PM Adrian Klaver <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> On 8/9/19 3:06 PM, Souvik Bhattacherjee wrote:
> > Hi,
> > 
> > I'm trying to measure the performance of the following: Multiple txns 
> > inserting tuples into a table concurrently vs single txn doing the whole 
> > insertion.
> > 
> > *new table created as:*
> > create table tab2 (
> > id serial,
> > attr1 integer not null,
> > attr2 integer not null,
> > primary key(id)
> > );
> > 
> > *EXP 1: inserts with multiple txn:*
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> > attr2 = 10);
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> > attr2 = 20);
> > 
> > note: attr2 has only two values 10 and 20
> > 
> > *EXP 2: inserts with a single txn:*
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1);
> > 
> > I also performed another experiment as follows:
> > *EXP 3:* select attr1, attr2 into tab2 from tab1;
> > 
> > The observation here is EXP 3  is much faster than EXP 2 probably due to 
> > bulk inserts used by Postgres. However I could not find a way to insert 
> > id values in tab2 using EXP 3. Also select .. into .. from .. throws an 
> > error if we create a table first and then populate the tuples using the 
> > command.
> 
> Yes as SELECT INTO is functionally the same as CREATE TABLE AS:
> 
> https://www.postgresql.org/docs/11/sql-selectinto.html <https://www.postgresql.org/docs/11/sql-selectinto.html;
> 
> > 
> > I have the following questions:
> > 1. Is it possible to have an id column in tab2 and perform a bulk insert 
> > using select .. into .. from .. or using some other means?
> 
> Not using SELECT INTO for reasons given above.
> Though it is possible to SELECT INTO as you show in EXP 3 and then:
>         alter table tab2 add column id serial primary key;
> EXP 2 shows the other means.
> 
> > 2. If a table is already created, is it possible to do bulk inserts via 
> > multiple txns inserting into the same table (EXP 3)?
> 
> Yes, but you will some code via client or function that batches the 
> inserts for you.
> 
> > 
> > Best,
> > -SB
> 
> 
> -- 
> Adrian Klaver
> adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
Top-posting (i.e. putting your reply at the top is discouraged here)
Does this appeal to you:
COPY (SELECT * FROM relation) TO ... (https://www.postgresql.org/docs/10/sql-copy.html <https://www.postgresql.org/docs/10/sql-copy.html;)

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-11 02:47   ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-11 03:01     ` Re: Bulk Inserts Rob Sargent <robjsargent@gmail.com>
@ 2019-08-11 03:35       ` Souvik Bhattacherjee <kivuosb@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Souvik Bhattacherjee @ 2019-08-11 03:35 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

> Does this appeal to you:
> COPY (SELECT * FROM relation) TO ... (
https://www.postgresql.org/docs/10/sql-copy.html)

Not sure if COPY can be used to transfer data between tables.

On Sat, Aug 10, 2019 at 11:01 PM Rob Sargent <robjsargent@gmail.com> wrote:

>
>
> On Aug 10, 2019, at 8:47 PM, Souvik Bhattacherjee <kivuosb@gmail.com>
> wrote:
>
> Hi Adrian,
>
> Thanks for the response.
>
> > Yes, but you will some code via client or function that batches the
> > inserts for you.
>
> Could you please elaborate a bit on how EXP 1 could be performed such that
> it uses bulk inserts?
>
> Best,
> -SB
>
> On Fri, Aug 9, 2019 at 7:26 PM Adrian Klaver <adrian.klaver@aklaver.com>
> wrote:
>
>> On 8/9/19 3:06 PM, Souvik Bhattacherjee wrote:
>> > Hi,
>> >
>> > I'm trying to measure the performance of the following: Multiple txns
>> > inserting tuples into a table concurrently vs single txn doing the
>> whole
>> > insertion.
>> >
>> > *new table created as:*
>> > create table tab2 (
>> > id serial,
>> > attr1 integer not null,
>> > attr2 integer not null,
>> > primary key(id)
>> > );
>> >
>> > *EXP 1: inserts with multiple txn:*
>> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
>> > attr2 = 10);
>> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
>> > attr2 = 20);
>> >
>> > note: attr2 has only two values 10 and 20
>> >
>> > *EXP 2: inserts with a single txn:*
>> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1);
>> >
>> > I also performed another experiment as follows:
>> > *EXP 3:* select attr1, attr2 into tab2 from tab1;
>> >
>> > The observation here is EXP 3  is much faster than EXP 2 probably due
>> to
>> > bulk inserts used by Postgres. However I could not find a way to insert
>> > id values in tab2 using EXP 3. Also select .. into .. from .. throws an
>> > error if we create a table first and then populate the tuples using the
>> > command.
>>
>> Yes as SELECT INTO is functionally the same as CREATE TABLE AS:
>>
>> https://www.postgresql.org/docs/11/sql-selectinto.html
>>
>> >
>> > I have the following questions:
>> > 1. Is it possible to have an id column in tab2 and perform a bulk
>> insert
>> > using select .. into .. from .. or using some other means?
>>
>> Not using SELECT INTO for reasons given above.
>> Though it is possible to SELECT INTO as you show in EXP 3 and then:
>>         alter table tab2 add column id serial primary key;
>> EXP 2 shows the other means.
>>
>> > 2. If a table is already created, is it possible to do bulk inserts via
>> > multiple txns inserting into the same table (EXP 3)?
>>
>> Yes, but you will some code via client or function that batches the
>> inserts for you.
>>
>> >
>> > Best,
>> > -SB
>>
>>
>> --
>> Adrian Klaver
>> adrian.klaver@aklaver.com
>>
> Top-posting (i.e. putting your reply at the top is discouraged here)
> Does this appeal to you:
>
> COPY (SELECT * FROM relation) TO ... (
> https://www.postgresql.org/docs/10/sql-copy.html)
>
>
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-11 02:47   ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-08-11 15:11     ` Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-13 13:34       ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Adrian Klaver @ 2019-08-11 15:11 UTC (permalink / raw)
  To: Souvik Bhattacherjee <kivuosb@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 8/10/19 7:47 PM, Souvik Bhattacherjee wrote:
> Hi Adrian,
> 
> Thanks for the response.
> 
>  > Yes, but you will some code via client or function that batches the
>  > inserts for you.
> 
> Could you please elaborate a bit on how EXP 1 could be performed such 
> that it uses bulk inserts?

I guess it comes down to what you define as bulk inserts. From your OP:

EXP 1: inserts with multiple txn:
insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
attr2 = 10);
insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
attr2 = 20);

If the selects are returning more then one row then you are already 
doing bulk inserts. If they are returning single rows or you want to 
batch them then you need some sort of code to do that. Something 
like(pseudo Python like code):

attr2_vals= [(10, 20, 30, 40), (50, 60, 70, 80)]

for val_batch in attr2_vals:
	BEGIN
	for id in val_batch:
		insert into tab2 (attr1, attr2) (select attr1, attr2
                 from tab1 where attr2 = id)
         COMMIT

> 
> Best,
> -SB



-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-11 02:47   ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-11 15:11     ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-08-13 13:34       ` Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-13 15:08         ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Souvik Bhattacherjee @ 2019-08-13 13:34 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

> If the selects are returning more then one row then you are already
> doing bulk inserts. If they are returning single rows or you want to
> batch them then you need some sort of code to do that. Something
> like(pseudo Python like code):

> attr2_vals= [(10, 20, 30, 40), (50, 60, 70, 80)]

> for val_batch in attr2_vals:
        BEGIN
        for id in val_batch:
                insert into tab2 (attr1, attr2) (select attr1, attr2
                 from tab1 where attr2 = id)
         COMMIT

For *EXP 1: inserts with multiple txn:*
insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where attr2
= 10);
insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where attr2
= 20);

tab1 has ~6M rows and there are only two values for the attribute attr2 in
tab1 which are evenly distributed. So, yes, I guess I'm already doing
batching
here.

Also, I ran the following two statements to see if their performances are
comparable.
While STMT 1 always runs faster in my machine but their performances seem
to differ
by a couple of seconds at most.

STMT 1: select attr1, attr2 into tab2 from tab1;
STMT 2: insert into tab2 (select attr1, attr2 from tab1);

However adding the serial id column as an ALTER TABLE statement actually
takes more time
than inserting the tuples, so the combined total time is more than double
the time taken to insert
the tuples into tab2 without serial id column.

Best,
-SB



On Sun, Aug 11, 2019 at 11:11 AM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 8/10/19 7:47 PM, Souvik Bhattacherjee wrote:
> > Hi Adrian,
> >
> > Thanks for the response.
> >
> >  > Yes, but you will some code via client or function that batches the
> >  > inserts for you.
> >
> > Could you please elaborate a bit on how EXP 1 could be performed such
> > that it uses bulk inserts?
>
> I guess it comes down to what you define as bulk inserts. From your OP:
>
> EXP 1: inserts with multiple txn:
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
> attr2 = 10);
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
> attr2 = 20);
>
> If the selects are returning more then one row then you are already
> doing bulk inserts. If they are returning single rows or you want to
> batch them then you need some sort of code to do that. Something
> like(pseudo Python like code):
>
> attr2_vals= [(10, 20, 30, 40), (50, 60, 70, 80)]
>
> for val_batch in attr2_vals:
>         BEGIN
>         for id in val_batch:
>                 insert into tab2 (attr1, attr2) (select attr1, attr2
>                  from tab1 where attr2 = id)
>          COMMIT
>
> >
> > Best,
> > -SB
>
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-11 02:47   ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-11 15:11     ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-13 13:34       ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
@ 2019-08-13 15:08         ` Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-13 16:32           ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Adrian Klaver @ 2019-08-13 15:08 UTC (permalink / raw)
  To: Souvik Bhattacherjee <kivuosb@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 8/13/19 6:34 AM, Souvik Bhattacherjee wrote:
>  > If the selects are returning more then one row then you are already
>  > doing bulk inserts. If they are returning single rows or you want to
>  > batch them then you need some sort of code to do that. Something
>  > like(pseudo Python like code):
> 
>  > attr2_vals= [(10, 20, 30, 40), (50, 60, 70, 80)]
> 
>  > for val_batch in attr2_vals:
>          BEGIN
>          for id in val_batch:
>                  insert into tab2 (attr1, attr2) (select attr1, attr2
>                   from tab1 where attr2 = id)
>           COMMIT
> 
> For *EXP 1: inserts with multiple txn:*
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> attr2 = 10);
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> attr2 = 20);
> 
> tab1 has ~6M rows and there are only two values for the attribute attr2 in
> tab1 which are evenly distributed. So, yes, I guess I'm already doing 
> batching
> here.
> 
> Also, I ran the following two statements to see if their performances 
> are comparable.
> While STMT 1 always runs faster in my machine but their performances 
> seem to differ
> by a couple of seconds at most.
> 
> STMT 1: select attr1, attr2 into tab2 from tab1;
> STMT 2: insert into tab2 (select attr1, attr2 from tab1);

All I have left is:

select tab2 row_number () OVER (order by attr2, attr1 )AS id, attr1, 
attr2 into tab2 from tab1;

That will not create a serial type in the id column though. You can 
attach a sequence to that column. Something like:

1) create sequence tab2_id start <max id + 1> owned by tab2.id;

2) alter table tab2 alter COLUMN id set default nextval('tab2_id');



> 
> However adding the serial id column as an ALTER TABLE statement actually 
> takes more time
> than inserting the tuples, so the combined total time is more than 
> double the time taken to insert
> the tuples into tab2 without serial id column.
> 
> Best,
> -SB
> 
> 
> 



-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Bulk Inserts
  2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-09 23:25 ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-11 02:47   ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-11 15:11     ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-13 13:34       ` Re: Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
  2019-08-13 15:08         ` Re: Bulk Inserts Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-08-13 16:32           ` Souvik Bhattacherjee <kivuosb@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Souvik Bhattacherjee @ 2019-08-13 16:32 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

> All I have left is:

> select tab2 row_number () OVER (order by attr2, attr1 )AS id, attr1,
> attr2 into tab2 from tab1;

> That will not create a serial type in the id column though. You can
> attach a sequence to that column. Something like:

> 1) create sequence tab2_id start <max id + 1> owned by tab2.id;

> 2) alter table tab2 alter COLUMN id set default nextval('tab2_id');

Thanks. This is a bit indirect but works fine. Performance wise this turns
out to the best when inserting rows from one table to another (new) table
with a serial id column in the new table.

Best,
-SB

On Tue, Aug 13, 2019 at 11:08 AM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 8/13/19 6:34 AM, Souvik Bhattacherjee wrote:
> >  > If the selects are returning more then one row then you are already
> >  > doing bulk inserts. If they are returning single rows or you want to
> >  > batch them then you need some sort of code to do that. Something
> >  > like(pseudo Python like code):
> >
> >  > attr2_vals= [(10, 20, 30, 40), (50, 60, 70, 80)]
> >
> >  > for val_batch in attr2_vals:
> >          BEGIN
> >          for id in val_batch:
> >                  insert into tab2 (attr1, attr2) (select attr1, attr2
> >                   from tab1 where attr2 = id)
> >           COMMIT
> >
> > For *EXP 1: inserts with multiple txn:*
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
> > attr2 = 10);
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where
> > attr2 = 20);
> >
> > tab1 has ~6M rows and there are only two values for the attribute attr2
> in
> > tab1 which are evenly distributed. So, yes, I guess I'm already doing
> > batching
> > here.
> >
> > Also, I ran the following two statements to see if their performances
> > are comparable.
> > While STMT 1 always runs faster in my machine but their performances
> > seem to differ
> > by a couple of seconds at most.
> >
> > STMT 1: select attr1, attr2 into tab2 from tab1;
> > STMT 2: insert into tab2 (select attr1, attr2 from tab1);
>
> All I have left is:
>
> select tab2 row_number () OVER (order by attr2, attr1 )AS id, attr1,
> attr2 into tab2 from tab1;
>
> That will not create a serial type in the id column though. You can
> attach a sequence to that column. Something like:
>
> 1) create sequence tab2_id start <max id + 1> owned by tab2.id;
>
> 2) alter table tab2 alter COLUMN id set default nextval('tab2_id');
>
>
>
> >
> > However adding the serial id column as an ALTER TABLE statement actually
> > takes more time
> > than inserting the tuples, so the combined total time is more than
> > double the time taken to insert
> > the tuples into tab2 without serial id column.
> >
> > Best,
> > -SB
> >
> >
> >
>
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2019-08-13 16:32 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-09 22:06 Bulk Inserts Souvik Bhattacherjee <kivuosb@gmail.com>
2019-08-09 23:25 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-08-11 02:47   ` Souvik Bhattacherjee <kivuosb@gmail.com>
2019-08-11 03:01     ` Rob Sargent <robjsargent@gmail.com>
2019-08-11 03:35       ` Souvik Bhattacherjee <kivuosb@gmail.com>
2019-08-11 15:11     ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-08-13 13:34       ` Souvik Bhattacherjee <kivuosb@gmail.com>
2019-08-13 15:08         ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-08-13 16:32           ` Souvik Bhattacherjee <kivuosb@gmail.com>

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