Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fctZo-0005KK-0Y for pgsql-general@arkaria.postgresql.org; Tue, 10 Jul 2018 14:25:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fctZl-0001EW-8i for pgsql-general@arkaria.postgresql.org; Tue, 10 Jul 2018 14:25:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fctZl-0001EP-0l for pgsql-general@lists.postgresql.org; Tue, 10 Jul 2018 14:25:09 +0000 Received: from mout.kundenserver.de ([212.227.126.133]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fctZe-0004kR-8q for pgsql-general@lists.postgresql.org; Tue, 10 Jul 2018 14:25:08 +0000 Received: from [10.8.0.146] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue007 [212.227.15.129]) with ESMTPSA (Nemesis) id 0M0oJP-1fwE6d3LZP-00vAYW for ; Tue, 10 Jul 2018 16:25:00 +0200 Subject: Re: Disabling/Enabling index before bulk loading To: pgsql-general@lists.postgresql.org Newsgroups: gmane.comp.db.postgresql.general References: <67D408BB-5BF7-4607-A730-6F262F65811A@yahoo.com> From: Thomas Kellerer Message-ID: <426a4f2b-7bb3-8270-47d5-e1a71607cfa3@gmx.net> Date: Tue, 10 Jul 2018 16:24:59 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: <67D408BB-5BF7-4607-A730-6F262F65811A@yahoo.com> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:uF0MMjnN3Zxd9CAm/refBLcoUyG3KNcAz14M2xSXJ/sB0HSNlKi lHLynNBw+XpYJYna/KnzdQz9MrLpXVny5BcVY0Q3BKzNuCu0wbhuEOXKh6XPjfkryByzIxu w2I6E0EaIAx/Fp0I9HgHkvMvgcEy+N2yZAY/O5ay6+OYADjuHqERStBKHNb+k/KsMiLOGXi FFBtyEb5AwqoxTHqeohOQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:NyCLXC7Uc9I=:v77myDOv+Z4ZdYOkqfQ5S9 Q4RTa56k+WzLUyKpn3kZ/tvumt7IypcAny42XG7811ldx5xEBsBZsZQktIcdV746n0rg0yKL+ qTka4XnscX1vThxFxUcEMb5OwPquYM965+9dQfKuQcVrHIEPmBjvJpoEz7vjVNGrx3yo9r9OY VtsVBeUEGsCkm9swUWSo5xS7mUgaQ1WHlbRCP44kg8zZ4T6sGvXjHLNFs2mV0z6H9kUckq86q BY4tCryJjm43YkP9JQFe0f8jrHVaiurU71y1MAsyyBMBGpSquHTGhFoZ61piO8pvw2XYeuhpg s/Jl8eOlluEkEG0LfG3508LB+Td/fWLrMv+gurM4hjYBXF1k0UiFqSWP7IziM/uHupNAK3/Cb IRJ5bNHsmbQJSgye2kNAoAnZfV2ROc647eyVVyDbL+9oFIP+vFdzSBf+EnPus+EBHnhHL1L1o RTlYPQpKOiqWUV2jbGMdN8QeLItyKrTaNvkImhbi3+NvGxyTV7rWPtHSCK2PPlVi/AqSP1IXn 7cCs6Wb8h5zDnw3P9H1nTmxOPwjnDEpkHfmT/h3bS6FPf0Y32vrB9UMobdGvozx4+uWlpZOoE GgPsV3x5VcEEVHa7N5iTrM+DF3sJKA24q7pFWZiwMFuck18fJPOtbfSrsuRTrOf3XHVT1Px4c 0Mn1J+bZzqKoRqG5xwd77g3rtp4iaXh69yHsM6EZzz3nTEMxsMdzFvR6Ij0s7K5OUupxJp8lN Hf9KXmEyX3pvCAsf List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ravi Krishna schrieb am 10.07.2018 um 16:08: > > We recently did a test on COPY and found that on large tables (47 million rows , 20GB of raw data) the > difference in COPY with 16 indexes and COPY without any index is 1:14. That is, COPY is 14 times slower > when data is ingested with all indexes as opposed to COPY first without index and then create all index. > > I googled for earlier posting on this and it looks like this has been asked before too. > > This is what I am thinking to do: > > 1 - Extract index definition and save it as a SQL somewhere, either a file or a table. > 2 - Drop all indexes. > 3 - Ingest data via COPY > 4 - Recreate all indexes saved in (1). > > Is there a generic sql or script or tool to accomplish (1). You can do this with a script like this: -- First store the definitions of all the indexes in a table: create table index_backup as select * from pg_indexes where schemaname = 'public' and tablename = 'the_table'; -- now drop all the indexes: do $$ declare l_rec record; begin for l_rec in select schemaname, indexname from index_backup loop execute format('drop index %I.%I', l_rec.schemaname, l_rec.indexname); end loop; end; $$ -- now insert the data ... -- and restore all indexes do $$ declare l_rec record; begin for l_rec in select indexdef from index_backup loop execute l_rec.indexdef; end loop; end; $$