Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V7orW-0003nQ-KR for pgsql-performance@arkaria.postgresql.org; Fri, 09 Aug 2013 15:44:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V7orV-0005sT-Vx for pgsql-performance@arkaria.postgresql.org; Fri, 09 Aug 2013 15:44:22 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V7orU-0005sK-NI for pgsql-performance@postgresql.org; Fri, 09 Aug 2013 15:44:20 +0000 Received: from nm28.bullet.mail.bf1.yahoo.com ([98.139.212.187]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V7orQ-0007ra-8o for pgsql-performance@postgresql.org; Fri, 09 Aug 2013 15:44:20 +0000 Received: from [98.139.212.152] by nm28.bullet.mail.bf1.yahoo.com with NNFMP; 09 Aug 2013 15:44:15 -0000 Received: from [98.139.212.223] by tm9.bullet.mail.bf1.yahoo.com with NNFMP; 09 Aug 2013 15:44:15 -0000 Received: from [127.0.0.1] by omp1032.mail.bf1.yahoo.com with NNFMP; 09 Aug 2013 15:44:15 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 403188.64163.bm@omp1032.mail.bf1.yahoo.com Received: (qmail 25706 invoked by uid 60001); 9 Aug 2013 15:44:15 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s1024; t=1376063055; bh=vZx9npsjK+TP/yQKn69vs1cemd84TojYNTmBprKwTtE=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=Ul2CkQCmffaTve1Tf85Qmwv6DMuGMtBeMGpYNq2zol+fnu/Ij5P/Lwt+WXS22nA4cAvADtBeGMHHSrt3WoB4siF+FXrl0919wFencbCCd5WQkkudlYdIbLl+b1XYq8eA0r2ak1X4Rg4eC4JUREQqto3VFe3Adzv8pQNpvtHrIX0= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=ymail.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=TfFWbBwMCAetWvM3s86S2PdvW8ulB9QtBfgutvNTb9j/NACic3mNNRXCUsvoTp8vBmHctJytXLAdPnerSB2CREgbSsMEYNPcNw4bGCXrs4NKDI6119SnofsomUwCMA859ZUw5fPtEcYYAWJs9T4lHWGGF5J8QfIP4PEnYJRCNEg=; X-YMail-OSG: 9.ERHe0VM1nbGS_RZ6FbuBtUd4CCIZiGyfA0m9m5K.0aRPD bxwdmxqK.hn9FKuhEE66E8EsPcIZhtjgJ2OsxUMULxKjIGZ4c01tZCSLzQkT ghNvqTxbpkGG9D4Q9m0aQfum3..UbjFlJr1wL3qHuCarBY.2z6sHLPIaoMLB viCAjqoC6zjo55ycI1UiHmyySJCjM7TDH0SQlciyRONMkrZRVtteGzsGW5S1 ncPyyNmjaqNKol0f1P0t7mEQG02mK2CWWf3qToOsH7o4KPSdKlkwK0ln_smD 5SuQw2tjOAcFeQLYG3WwJo8nv74_Qr.oa3jwRBKGVyXda3vDUyzoovodvg7k GthAEUcH_yqRRk4QsDJ8G4f0AyMc88XsozUbEemYDj.iQTq70hgyMZ_FEYTy tobbchqCYbn0oUV.58.5qCQ_tblnXn4nUsKNe2g1mHYV1s79NtFTaJWFzbAP U71jTSnYnmbI.N5_TklLRJNeORlgnFt.6EojGJONrp2IsotwDylScw4S21OT xzwctp7crsixl8aitivSfTRIcgVKT7pXRs14Enqy2VX_U4MzZWOAyTUVAfXB Xm0z1vBHhE1tKCjDxOO9ZflIG0cfLbO4xH4G6UHEHv3HmF0agiozPMkUkCOj l20zAIw6.UifcwKlG1TigRpawFb7KY31N4UKCnVBVai589VnPzhviammFyO1 R36g9LcM- Received: from [76.255.18.237] by web162902.mail.bf1.yahoo.com via HTTP; Fri, 09 Aug 2013 08:44:15 PDT X-Rocket-MIMEInfo: 002.001, Um9iZXJ0IERpRmFsY28gPHJvYmVydC5kaWZhbGNvQGdtYWlsLmNvbT4gd3JvdGU6Cgo.IEluIG15IHN5c3RlbSBhIHVzZXIgY2FuIGhhdmUgZXh0ZXJuYWwgY29udGFjdHMuIFdoZW4gSSBhbQo.IGJyaW5naW5nIGluIGV4dGVybmFsIGNvbnRhY3RzIEkgd2FudCB0byBjb3JyZWxhdGUgYW55IG90aGVyCj4gZXhpc3RpbmcgdXNlcnMgaW4gdGhlIHN5c3RlbSB3aXRoIHRob3NlIGV4dGVybmFsIGNvbnRhY3RzLiBBCj4gdXNlcnMgZXh0ZXJuYWwgY29udGFjdHMgbWF5IG9yIG1heSBub3QgYmUgdXNlcnMgaW4gbXkBMAEBAQE- X-Mailer: YahooMailWebService/0.8.153.572 References: Message-ID: <1376063055.23129.YahooMailNeo@web162902.mail.bf1.yahoo.com> Date: Fri, 9 Aug 2013 08:44:15 -0700 (PDT) From: Kevin Grittner Reply-To: Kevin Grittner Subject: Re: Efficient Correlated Update To: Robert DiFalco , "pgsql-performance@postgresql.org" In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: 0.7 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Robert DiFalco wrote: > In my system a user can have external contacts. When I am > bringing in external contacts I want to correlate any other > existing users in the system with those external contacts. A > users external contacts may or may not be users in my system. I > have a user_id field in "contacts" that is NULL if that contact > is not a user in my system > > Currently I do something like this after reading in external > contacts: > >=A0=A0 UPDATE contacts SET user_id =3D u.id >=A0=A0 FROM my_users u=20 >=A0=A0 JOIN phone_numbers pn ON u.phone_significant =3D pn.significant=20 >=A0=A0 WHERE contacts.owner_id =3D 7 >=A0=A0 AND contacts.user_id IS NULL >=A0=A0 AND contacts.id =3D pn.ref_contact_id; > > If any of the fields are not self explanatory let me know. > "Significant" is just the right 7 most digits of a raw phone > number.=20 > > I'm more interested in possible improvements to my relational > logic than the details of the "significant" condition. IOW, I'm > start enough to optimize the "significant" query but not smart > enough to know if this is the best approach for the overall > correlated UPDATE query. :) > > So yeah, is this the best way to update a contact's user_id > reference based on a contacts phone number matching the phone > number of a user? > > One detail from the schema -- A contact can have many phone > numbers but a user in my system will only ever have just one > phone number. Hence the JOIN to "phone_numbers" versus the column > in "my_users". =A0 In looking it over, nothing jumped out at me as a problem.=A0 Are you having some problem with it, like poor performance or getting results different from what you expected? --=20 Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance