Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 294EE1337BD0 for ; Tue, 26 Apr 2011 12:21:06 -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 08569-05-5 for ; Tue, 26 Apr 2011 15:20:58 +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 EAB911337C23 for ; Tue, 26 Apr 2011 12:16:22 -0300 (ADT) Received: by wya21 with SMTP id 21so539907wya.19 for ; Tue, 26 Apr 2011 08:16:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:content-type:content-transfer-encoding :subject:date:message-id:to:mime-version:x-mailer; bh=qRr29fyg48UT+jHFRRrkUX1eGXqCWz6OlY8d1L9CCEc=; b=PCV3S81spTTwwmul1bp+bnDTKFngfFFK7IedSK695mcMEuuoPkrrJAADcvWSlk7SR5 aaFrm3dumIgry8qC7aNNp6+y+qqjZUlLn5oJhlVJqIyhvoC7XaxxefVx6wJuOfdGAIU3 7HjmBwrhvj7mTjcodxoT3ycBoq7B1Lur5xaiU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:content-type:content-transfer-encoding:subject:date:message-id :to:mime-version:x-mailer; b=fstSoOfD9OnOhbN1JXXDynt6lh29HOwcmi4ghkyDLnFHOQksF0+r/GxdFC/RhOPgOc 27QS+BxNiwRFZYkGktbC80OgUueD0aGOOJBGWFYvy72JuO8rfj8bGrWK2dT65ndPjonN gw7zhHY/V4X4FrThvdWWE3NHrX7triBDyIoQ0= Received: by 10.227.16.98 with SMTP id n34mr941964wba.31.1303830981451; Tue, 26 Apr 2011 08:16:21 -0700 (PDT) Received: from [192.168.0.196] ([95.60.213.7]) by mx.google.com with ESMTPS id p5sm3912566wbg.11.2011.04.26.08.16.19 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 26 Apr 2011 08:16:20 -0700 (PDT) From: Joel Reymont Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: optimizing a cpu-heavy query Date: Tue, 26 Apr 2011 16:16:19 +0100 Message-Id: To: pgsql-performance@postgresql.org Mime-Version: 1.0 (Apple Message framework v1084) X-Mailer: Apple Mail (2.1084) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.335 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, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201104/356 X-Sequence-Number: 43418 Folks, I'm trying to optimize the following query that performs KL Divergence = [1]. As you can see the distance function operates on vectors of 150 = floats.=20 The query takes 12 minutes to run on an idle (apart from pgsql) EC2 m1 = large instance with 2 million documents in the docs table. The CPU is = pegged at 100% during this time. I need to be able to both process = concurrent distance queries and otherwise use the database. I have the option of moving this distance calculation off of PG but are = there other options? Is there anything clearly wrong that I'm doing here? Would it speed things up to make the float array a custom data type = backed by C code? Thanks in advance, Joel [1] http://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence --- CREATE DOMAIN topics AS float[150]; CREATE DOMAIN doc_id AS varchar(64); CREATE TABLE docs ( id serial, doc_id doc_id NOT NULL PRIMARY KEY, topics topics NOT NULL ); CREATE OR REPLACE FUNCTION docs_within_distance(vec topics, threshold = float)=20 RETURNS TABLE(id doc_id, distance float) AS $$ BEGIN RETURN QUERY SELECT *=20 FROM (SELECT doc_id, (SELECT sum(vec[i] * ln(vec[i] / topics[i]))=20 FROM generate_subscripts(topics, 1) AS i WHERE topics[i] > 0) AS distance FROM docs) AS tab WHERE tab.distance <=3D threshold; END; $$ LANGUAGE plpgsql; = --------------------------------------------------------------------------= - for hire: mac osx device driver ninja, kernel extensions and usb = drivers = ---------------------+------------+---------------------------------------= http://wagerlabs.com | @wagerlabs | = http://www.linkedin.com/in/joelreymont = ---------------------+------------+---------------------------------------=