Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxYQ0-0000d6-64 for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 15:09:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hxYPz-0008AO-18 for pgsql-general@arkaria.postgresql.org; Tue, 13 Aug 2019 15:08:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hxYPx-00088Q-6Q for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 15:08:58 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hxYPu-0006wu-82 for pgsql-general@lists.postgresql.org; Tue, 13 Aug 2019 15:08:56 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 6E3D638C; Tue, 13 Aug 2019 11:08:52 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 13 Aug 2019 11:08:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=+ FdtrJK8wrsXrMggEUNWT3anmClKaUwBMgxm3JZW2s4=; b=Iv1/iEzaaWnzP9hri dvfdTkcee5wCMJDw4f5f9du2FnjF7cmaFaPF5Oe9j/yLcWp8qclaAQFyHA8/YsYs IV3Ep6y/TZnGwYRjMA5XwLWj4UVl2PPHDTMrBfvAKQd01yK8uFt6bSg/dGq7XX79 8kSIEQHyyPjtgNtPmfJ4LzBd3PmNJU0ppGti8XY2j9CD65+QwlBxcZNRlQsof/OI nfm1uWRUAYESQRnfKbquNKBMZKvEheaESopCJuZkM/qYLyFGlI705/J5RD7Jkvd6 SJHeTKhKflr/+yTJ/t6C+VAMxm1B6wCInnxqF0nVHvq2EMI4/qd2NOqC1RW6crPR BE8Rg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=+FdtrJK8wrsXrMggEUNWT3anmClKaUwBMgxm3JZW2 s4=; b=MCaA2R+gCPbQ1YpsihCMQ+U+pU/T1OOPlBj/1OE/mAB9FGIvXQU/Bdhl8 LZkqgDHvBgC8lOWzpgrGNSlVxH/j6mp6NlW/9y73IilSMWoz3S6dyVyGUUonlGC5 1FGRjC8IhLnZfvLdWHPlmimbf0SypaPsNHW8bHXtbiQQFBh6JNsKFMHkobD0zZrN bfaTb4hFpRVnZMjlWDD21AUpNxIO1NYJJfG1ipMnktkmxhkXC9ezW+ZeNATrvvVU /c7+dAIxQhZv5+hM+VSAU6ixUgf3DaHyNpK+gCVWXDqaot99KstRShiZC8dDD2pW lLokO9BNgCDOQutPBgm+cfnMQSbTA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddruddviedgkeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepjedurddvuddvrdduhedtrddvtddvnecurfgrrhgrmhepmhgrihhlfhhr ohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsth gvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 0C4818005A; Tue, 13 Aug 2019 11:08:50 -0400 (EDT) Subject: Re: Bulk Inserts To: Souvik Bhattacherjee Cc: "pgsql-generallists.postgresql.org" References: <9ffb5586-4217-b131-bed8-53b16feb5740@aklaver.com> From: Adrian Klaver Message-ID: <5cd58ca7-ea45-01ee-bd80-c9366b872291@aklaver.com> Date: Tue, 13 Aug 2019 08:08:50 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 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