Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id B0D7D1337BB7 for ; Sat, 30 Apr 2011 18:15:37 -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 34814-02 for ; Sat, 30 Apr 2011 21:15:30 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-wy0-f174.google.com (mail-wy0-f174.google.com [74.125.82.174]) by mail.postgresql.org (Postfix) with ESMTP id 9862F1337B92 for ; Sat, 30 Apr 2011 18:15:29 -0300 (ADT) Received: by wya21 with SMTP id 21so3503985wya.19 for ; Sat, 30 Apr 2011 14:15:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:subject:mime-version:content-type:from :in-reply-to:date:cc:content-transfer-encoding:message-id:references :to:x-mailer; bh=2JqwARVFMT2MYrRo7bHF59L763XhR62qdMxbzPuEdFI=; b=xW0jWe9ymMbCW2XRuwBfk+0llrZY07lvs4/uMIRW8eTA5sKwU3pd9ymokfcYDA2uBs a+OWOKQjsG5ZQwZIIScsbxMoCCtyAtOMEAH05YnJbPVJyK3UVANaco2i28QBFsAYOISV vU+UZuilDrIDZFIhqtdN3mc6oWQbfoMQbcRSc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; b=dfPZ3NOGtJj5fAvuIBl7VO2WwxoOJrn6V5+M1zUjqbxe8Pw4LG+YjbOnOVQ+sDsjcl 2hVe33i+aeh9lcVfvRGzyQLVc5X8Ih97LL2JcrWCO4f+HEPyBTd5ODPUz4hca/q4LrTS KyRYugIy9bNZ7zT73h1R5cKd4yzm6l4Wfx3Qg= Received: by 10.227.139.90 with SMTP id d26mr2406109wbu.71.1304198128004; Sat, 30 Apr 2011 14:15:28 -0700 (PDT) Received: from [192.168.0.196] ([95.60.213.7]) by mx.google.com with ESMTPS id ed10sm2461956wbb.66.2011.04.30.14.15.25 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 30 Apr 2011 14:15:26 -0700 (PDT) Subject: Re: stored proc and inserting hundreds of thousands of rows Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Joel Reymont In-Reply-To: <4DBC0D7C020000250003D09E@gw.wicourts.gov> Date: Sat, 30 Apr 2011 22:15:23 +0100 Cc: "pgsql-performance@postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: References: <4DBC004C020000250003D099@gw.wicourts.gov> <4881277622113933088@unknownmsgid> <4DBC0D7C020000250003D09E@gw.wicourts.gov> To: "Kevin Grittner" X-Mailer: Apple Mail (2.1084) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.345 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_ENVFROM_END_DIGIT=1.553, FREEMAIL_FROM=0.001, RFC_ABUSE_POST=0.001 X-Spam-Level: X-Archive-Number: 201104/442 X-Sequence-Number: 43504 On Apr 30, 2011, at 7:24 PM, Kevin Grittner wrote: > If this is where most of the time is, the next thing is to run it > with EXPLAIN ANALYZE, and post the output. I was absolutely wrong about the calculation taking < 1s, it actually = takes about 30s for 2 million rows. Still, the difference between 5 minutes and 30s must be the insert. SELECT (t).doc_id, (t).distance FROM (SELECT docs_within_distance('{ 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.586099770475, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.167233562858, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667, 0.00166666666667, 0.00166666666667, 0.00166666666667, = 0.00166666666667 }', 50.0) as t) AS x; This takes 27.44 seconds EXPLAIN ANALYZE VERBOSE Subquery Scan on x (cost=3D0.00..0.27 rows=3D1 width=3D32) (actual = time=3D22422.418..23835.468 rows=3D2000002 loops=3D1) Output: (t).doc_id, (t).distance -> Result (cost=3D0.00..0.26 rows=3D1 width=3D0) (actual = time=3D22422.410..23184.086 rows=3D2000002 loops=3D1) Output: docs_within_distance(('{}'::double precision[])::topics, 50::double precision) Total runtime: 23948.563 ms Topics is defined thusly: CREATE DOMAIN topics AS float[150]; Digging deeper into the distance function, EXPLAIN ANALYZE VERBOSE SELECT *=20 FROM (SELECT id, divergence(, topics) AS distance FROM = docs) AS tab WHERE tab.distance <=3D 50.0; Subquery Scan on tab (cost=3D0.00..383333.00 rows=3D666653 width=3D12) = (actual time=3D0.027..20429.299 rows=3D2000002 loops=3D1) Output: tab.id, tab.distance Filter: (tab.distance <=3D 50::double precision) -> Seq Scan on public.docs (cost=3D0.00..358333.50 rows=3D1999960 = width=3D36) (actual time=3D0.025..19908.200 rows=3D2000002 loops=3D1) Output: docs.id, divergence((::double = precision[])::topics, docs.topics) Total runtime: 20550.019 ms I can't dig any deeper because divergence is a C function. Thanks, Joel = --------------------------------------------------------------------------= - for hire: mac osx device driver ninja, kernel extensions and usb = drivers = ---------------------+------------+---------------------------------------= http://wagerlabs.com | @wagerlabs | = http://www.linkedin.com/in/joelreymont = ---------------------+------------+---------------------------------------=