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 1fd1B4-0003CC-3c for pgsql-general@arkaria.postgresql.org; Tue, 10 Jul 2018 22:32:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fd1B2-0004wB-FY for pgsql-general@arkaria.postgresql.org; Tue, 10 Jul 2018 22:32:08 +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 1fd1B2-0004w4-61 for pgsql-general@lists.postgresql.org; Tue, 10 Jul 2018 22:32:08 +0000 Received: from mail-pf0-x244.google.com ([2607:f8b0:400e:c00::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fd1Ay-0005MV-Se for pgsql-general@lists.postgresql.org; Tue, 10 Jul 2018 22:32:07 +0000 Received: by mail-pf0-x244.google.com with SMTP id q7-v6so15929345pff.2 for ; Tue, 10 Jul 2018 15:32:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=hcppVv15sQ20I5xw3pkgOCxS2WvJkwQorWVVtfmALiQ=; b=uPKQxejE1GwBjx/xVR+WdN5m0bt0kd3iJKH86KUWDNEm551L9vllSzXR3s0OjpwVHN q8rWXJdYiDOYvci8bIi9RYEfYTmfqPsftn9Irrw26xYWnDWwSB7JSLeEwp65pEiux0Ia IRc1ou6SP9nQ9kfgu4QxSo1y1CFq6KTL2DgFOMD2cZgrrgxN3FomD865OvEi9ljMUYJ6 yYoEqG0cRlxtIE5MROiuxud+URInm0BVcHRHrvp/LOspMZBGm/oilLa5wmq2dBP6eu77 aFHOqhH+oAESfpSQsJ4j3po1PRg8eLE3CrJ9C+c7CD2ER170FvmigPOnAtFwGn8MzooV A2BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=hcppVv15sQ20I5xw3pkgOCxS2WvJkwQorWVVtfmALiQ=; b=nUoxNO07K7CBzgPst/GF0WlsPEgh9p3P2uB28JqDU9wp+/ETJ5aAMO1SX9wRVY/tPA TOvQuElah8NnI8QXsKD9rvRmApT8F9vYG7uFT1LH+bbkPNSJVQ+QfN976MYKJwctzsDm fp1T5AohFOObVHjzBuyb6uc/348wsWKSNaaurjynLAVGHEOzAtezrIlGDVCS1C1+4RUr E83nMLJ/IGW/c1DrcM1pehu29SPxJuacyt33rJXOyaGOIrqd1/ayuczsq8YGAthtb0HD 2GWZ8ZbMESk1HplALX8XzDDtVdFym3K5Ulqh1kITs7525VOkmQYKzazgZFLgruP7gSI6 qGKQ== X-Gm-Message-State: APt69E284dnQ9fLvYVVwjl9nFqFo2hX7iR9JHpIUUANKHlpg7chIulhZ +EEiwBfhy0GhfaYqvtFd6UKZmG6l X-Google-Smtp-Source: AAOMgpeSF/P+7hGmVROU80+F9Wu528W3+xy8h8F+0IFkJ5QlubyfItTy8xJhV+J9R2k3tD5eJpJRhA== X-Received: by 2002:a63:fc0a:: with SMTP id j10-v6mr24211627pgi.1.1531261921172; Tue, 10 Jul 2018 15:32:01 -0700 (PDT) Received: from tim-desktop (2001-44b8-31f2-bb00-55b2-bbbb-021e-5c36.static.ipv6.internode.on.net. [2001:44b8:31f2:bb00:55b2:bbbb:21e:5c36]) by smtp.gmail.com with ESMTPSA id j2-v6sm19443923pff.35.2018.07.10.15.31.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 10 Jul 2018 15:32:00 -0700 (PDT) References: <67D408BB-5BF7-4607-A730-6F262F65811A@yahoo.com> User-agent: mu4e 0.9.18; emacs 26.1 From: Tim Cross To: Ravi Krishna Cc: PG mailing List Subject: Re: Disabling/Enabling index before bulk loading In-reply-to: <67D408BB-5BF7-4607-A730-6F262F65811A@yahoo.com> Date: Wed, 11 Jul 2018 08:31:56 +1000 Message-ID: <877em2racj.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ravi Krishna writes: > 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. > This is very system dependent. On our system, when we tested a similar approach, we found that the time saved through dropping the indexes before copy was lost when rebuilding the indexes afterwards. In fact, it ended up being slightly slower. I suspect a lot depends on the number and types of indexes you have. Your example had a lot more indexes than ours. We were loading 22 batches with 2.5M records per batch. While copy was significantly faster than transaction based inserts (minutes vs hours), the differences between indexes and no indexes was measured in minutes. We only had 3 or 4 indexes. > 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). > We are loading data via Javascript using pg and pg-copy-streams modules. It is pretty straight forward to drop the indexes and recreate them afterwards via sql, so we didn't look for a tool as such. As data is only inserted into this table and only by this process, we also turned off autovacuum for this table, performing vacuum and analyze manually after load. Tim -- Tim Cross