pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Reymont <joelr1@gmail.com>
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: stored proc and inserting hundreds of thousands of rows
Date: Sat, 30 Apr 2011 22:15:23 +0100
Message-ID: <DEBFB8EB-6F50-4462-8827-434E0CE308C7@gmail.com> (raw)
In-Reply-To: <4DBC0D7C020000250003D09E@gw.wicourts.gov>
References: <D5DE36B9-17CF-4A55-9119-CDC0BDC4E48D@gmail.com>
	<4DBC004C020000250003D099@gw.wicourts.gov>
	<4881277622113933088@unknownmsgid>
	<4DBC0D7C020000250003D09E@gw.wicourts.gov>


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=0.00..0.27 rows=1 width=32) (actual time=22422.418..23835.468 rows=2000002 loops=1)
  Output: (t).doc_id, (t).distance
  ->  Result  (cost=0.00..0.26 rows=1 width=0) (actual time=22422.410..23184.086 rows=2000002 loops=1)
        Output: docs_within_distance(('{<array above goes here>}'::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 * 
FROM (SELECT id, divergence(<array above>, topics) AS distance FROM docs) AS tab
WHERE tab.distance <= 50.0;

Subquery Scan on tab  (cost=0.00..383333.00 rows=666653 width=12) (actual time=0.027..20429.299 rows=2000002 loops=1)
  Output: tab.id, tab.distance
  Filter: (tab.distance <= 50::double precision)
  ->  Seq Scan on public.docs  (cost=0.00..358333.50 rows=1999960 width=36) (actual time=0.025..19908.200 rows=2000002 loops=1)
        Output: docs.id, divergence((<array above>::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
---------------------+------------+---------------------------------------






view thread (14+ messages)  latest in thread

Message-ID: <DEBFB8EB-6F50-4462-8827-434E0CE308C7@gmail.com>
Permalink:  ../DEBFB8EB-6F50-4462-8827-434E0CE308C7@gmail.com/
Also on:    postgresql.org/message-id/DEBFB8EB-6F50-4462-8827-434E0CE308C7@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: joelr1@gmail.com, Kevin.Grittner@wicourts.gov
  Subject: Re: stored proc and inserting hundreds of thousands of rows
  In-Reply-To: <DEBFB8EB-6F50-4462-8827-434E0CE308C7@gmail.com>
  References: <D5DE36B9-17CF-4A55-9119-CDC0BDC4E48D@gmail.com> <4DBC004C020000250003D099@gw.wicourts.gov> <4881277622113933088@unknownmsgid> <4DBC0D7C020000250003D09E@gw.wicourts.gov> <DEBFB8EB-6F50-4462-8827-434E0CE308C7@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox