Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 061871337990 for ; Sat, 30 Apr 2011 15:24:24 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 62184-05 for ; Sat, 30 Apr 2011 18:24:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gw.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by mail.postgresql.org (Postfix) with ESMTP id 04D621337B43 for ; Sat, 30 Apr 2011 15:24:15 -0300 (ADT) Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Sat, 30 Apr 2011 13:24:15 -0500 Message-Id: <4DBC0D7C020000250003D09E@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.1 Date: Sat, 30 Apr 2011 13:24:12 -0500 From: "Kevin Grittner" To: "Joel Reymont" Cc: "pgsql-performance@postgresql.org" Subject: Re: stored proc and inserting hundreds of thousands of rows References: <4DBC004C020000250003D099@gw.wicourts.gov> <4881277622113933088@unknownmsgid> In-Reply-To: <4881277622113933088@unknownmsgid> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/440 X-Sequence-Number: 43502 [rearranging to correct for top-posting] Joel Reymont wrote: > Kevin Grittner wrote: >> Joel Reymont wrote: >> >>> We have 2 million documents now and linking an ad to all of them >>> takes 5 minutes on my top-of-the-line SSD MacBook Pro. >> >> How long does it take to run just the SELECT part of the INSERT >> by itself? > Are you suggesting eliminating the physical linking and > calculating matching documents on the fly? I'm not suggesting anything other than it being a good idea to determine where the time is being spent before trying to make it faster. You showed this as the apparent source of the five minute delay: INSERT INTO doc_ads (doc_id, ad_id, distance) SELECT doc, (t).ad_id, (t).distance FROM (SELECT ads_within_distance(topics, threshold) AS t FROM docs WHERE id = doc) AS x; What we don't know is how much of that time is due to writing to the doc_ads table, and how much is due to reading the other tables. We can find that out by running this: SELECT doc, (t).ad_id, (t).distance FROM (SELECT ads_within_distance(topics, threshold) AS t FROM docs WHERE id = doc) AS x; If this is where most of the time is, the next thing is to run it with EXPLAIN ANALYZE, and post the output. It's a whole different set of things to try to tune if that part is fast and the INSERT itself is slow. Of course, be aware of caching effects when you time this. -Kevin