Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 498011336FA7 for ; Sat, 30 Apr 2011 13:57:03 -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 11117-04 for ; Sat, 30 Apr 2011 16:56:56 +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 C98D01337B80 for ; Sat, 30 Apr 2011 13:56:55 -0300 (ADT) Received: by wya21 with SMTP id 21so3434481wya.19 for ; Sat, 30 Apr 2011 09:56:54 -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=ZyR4anj/f/15mUvwCVXhZnZI+NkeyMxhHcjSQVOuL4g=; b=T63UWFi0MsfAIwOYJgwEEru1vopIE6/T+x16PlGKPJmesr/yXm2Ex+4zcSTrV+tFS/ MJyjCBnu5n12AYhpVK3RtqN4aqJ5lmPv4boWlRUYROaEMnDPdreaEBDTCtycmcIj7em8 e7SCX7uydYDtS7WKexqBn6zXSSpzXJPE9UQzQ= 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=PJoJBZHAU58ZaCIPdN12tXiz9UTO6FiChQWScFd5gK3U8W6hsF+yZoRgg1/GElOD8R RY9M/crsTvpAnZviOx9uQQ9zXUWfvbJA3ah8MjyW4EuVeFRxgyx3Caxqzw49zlzTR0Ry IRrn0hmtfhW3fIqdEF7QttxntZJ9n4L27IFLw= Received: by 10.216.221.158 with SMTP id r30mr770924wep.50.1304182614124; Sat, 30 Apr 2011 09:56:54 -0700 (PDT) Received: from [192.168.0.196] ([95.60.213.7]) by mx.google.com with ESMTPS id n2sm1871465wej.46.2011.04.30.09.56.52 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 30 Apr 2011 09:56:53 -0700 (PDT) From: Joel Reymont Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: stored proc and inserting hundreds of thousands of rows Date: Sat, 30 Apr 2011 17:56:50 +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/435 X-Sequence-Number: 43497 I have a stored proc that potentially inserts hundreds of thousands, = potentially millions, of rows (below). This stored proc is part of the the sequence of creating an ad campaign = and links an ad to documents it should be displayed with. A few of these stored procs can run concurrently as users create ad = campaigns. 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. Last but not least, the system has to quickly serve ads while documents = are being linked which is a problem at the moment. What can I do to make linking documents to ads faster or have less = impact on the system. I would like the system to be as responsive with = serving ads while the linking itself is allowed to take a few minutes.=20= One thing I'm concerned with, for example, is the whole multi-million = row insert running within the stored proc transaction. I think inserting = rows one by one or in small batches may be an improvement. I don't know = how to accomplish this, though. Thanks, Joel --- CREATE DOMAIN doc_id AS varchar(64); CREATE DOMAIN id AS int; CREATE TABLE doc_ads ( doc_id id NOT NULL REFERENCES docs, ad_id id NOT NULL REFERENCES ads, distance float NOT NULL ); CREATE INDEX doc_ads_idx ON doc_ads(doc_id); CREATE OR REPLACE FUNCTION link_doc_to_ads(doc id, threshold float)=20 RETURNS void AS $$ BEGIN 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 =3D doc) AS x; ANALYZE doc_ads; 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 = ---------------------+------------+---------------------------------------=