Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 1238863303A for ; Thu, 29 Oct 2009 14:50:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 33217-01 for ; Thu, 29 Oct 2009 17:50:38 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-gx0-f211.google.com (mail-gx0-f211.google.com [209.85.217.211]) by mail.postgresql.org (Postfix) with ESMTP id 14B356342F1 for ; Thu, 29 Oct 2009 14:50:39 -0300 (ADT) Received: by gxk3 with SMTP id 3so2546606gxk.15 for ; Thu, 29 Oct 2009 10:50:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=19WqOw3YTdn2H5gty6TU4papXqHr/pdniTHjigV9mZU=; b=u3VPvVV2WJ9s6qgJYWGexmbEycqh4aYqoTJgCyVzlkVX/tFa25FikNHN/0pPyDaZ7p mGzjRgyGXW/VZcnd9sOtAul4hAWwpugQSSspG5m93WtwBruVtGkdpDxFcSsC4a7uHCX7 aVQCWc7ifxsZgGhxCxbi50c06X6CINzYs47V0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=rhJvfnRayulcUk4bN+RB+zgHq0FJBgUOfP4b6oTIuIQW1Q/Bo2nvNkNuaUwb5JM7zu Q3F9/zrXmmCjhwhJXCyl+8Rhhj0Yi3/9oBVrIEL/4CPTUfaHa9QYxUArscE0yMYbKiud 4ogp4MdLqO7TUmIyAZlyyePL4m7FbOH+JgP5A= MIME-Version: 1.0 Received: by 10.90.37.37 with SMTP id k37mr1171534agk.64.1256838638634; Thu, 29 Oct 2009 10:50:38 -0700 (PDT) Date: Thu, 29 Oct 2009 13:50:38 -0400 Message-ID: <5ab13580910291050v26bea7e5j83d1a07f11956d39@mail.gmail.com> Subject: Speed up UPDATE query? From: Lee Hachadoorian To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/137 X-Sequence-Number: 33336 I'm trying to update several tables (all child tables of the same parent), and as the number of records increases, the length of time it takes to run the update is shooting up exponentially. I have imported the new data to an import table, and then join the import table to the update table. The update statement looks like: UPDATE household_2000 h SET hhincome = new_hhincome FROM ( SELECT serial, new_hhincome FROM import WHERE year = 2000 ) r WHERE h.serial = r.serial household_2000 is a child table of a household table that, as you might guess, only contains records from the year 2000. I am putting a year = 2000 restriction on the import table and then linking on the unique identifier. For different child tables, this is how long the update takes to run (numbers are approximate): Records Cost (via EXPLAIN) Actual time 460,000 300,000 23 seconds 510,000 320,000 26 seconds 1.2 million 670,000 3:16 1.3 million 820,000 3:25 6.2 million 2.7 million ~2.5 hours So, the cost estimate given by EXPLAIN seems to be roughly proportional to the number of records in the dataset, but the actual time it takes to run seems to increase faster than the cost, even for the small and medium tables, and shoots through the roof for the large tables. Since I need to run this on additional child tables that are larger (the largest is 14 million records), I want to know what I can do to speed up the query. Here's the EXPLAIN for the query. Note that the query plan is the same for the small, medium, and large tables. Hash Join (cost=1268532.36..2379787.06 rows=5465837 width=1128) Hash Cond: (import_6_17_rev_hh.serial = h.serial) -> Bitmap Heap Scan on import_6_17_rev_hh (cost=126551.72..308495.69 rows=5465837 width=8) Recheck Cond: (year = 1990) -> Bitmap Index Scan on import_6_17_rev_hh_pkey (cost=0.00..125185.26 rows=5465837 width=0) Index Cond: (year = 1990) -> Hash (cost=295596.06..295596.06 rows=5527406 width=1124) -> Seq Scan on household_1990 h (cost=0.00..295596.06 rows=5527406 width=1124) Any help will be appreciated. Thanks, --Lee -- Lee Hachadoorian PhD Student, Geography Program in Earth & Environmental Sciences CUNY Graduate Center