Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 1A7E11F6FE32 for ; Wed, 11 Jul 2012 19:10:13 -0300 (ADT) Received: from mail-ob0-f174.google.com ([209.85.214.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Sp56o-0006jr-Lr for pgsql-performance@postgresql.org; Wed, 11 Jul 2012 22:10:12 +0000 Received: by obbuo13 with SMTP id uo13so2278912obb.19 for ; Wed, 11 Jul 2012 15:09:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:x-gm-message-state; bh=LbzCpau5wDw2f0UtoLhioX+m9Z7lyI3FDzAYrjAWDSQ=; b=f6QDG+3KyaywOll4+Cy+913kFSVhoT5EcU/nTliEd8Hqd9Wh3fzfqwhjVOjMjQlawb SYcUVxQU+4FBHJkF7N79gj9S4GjeJA0eVSIuxaG+5b9vHw8u7a3x/5Y7B9LX7aPIabWs ISDSzI1KJtq5FNpBnC1sUsqjDRITHuIduiATbyccanvcldowNDTO2YlM+07+ZquMY/aj Fepvw44dgSezgPsDWTTA/NcnMvmlDPKthsXYVLCBrfAm89qvNL6+Env6a43hJrs+Cg2I 3HgHUEfuDewibxCBUPMRGqxaButdBtYu4+zGqfZJ1QQbwdvAqJA2oXspvEGooIV0eiMi lQMA== MIME-Version: 1.0 Received: by 10.182.110.37 with SMTP id hx5mr15355211obb.48.1342044596438; Wed, 11 Jul 2012 15:09:56 -0700 (PDT) Received: by 10.182.196.41 with HTTP; Wed, 11 Jul 2012 15:09:56 -0700 (PDT) In-Reply-To: <4FFDF0F1.9010005@optrak.com> References: <4FFCCAC4.4030503@ringerc.id.au> <23853.1342015548@sss.pgh.pa.us> <4FFDF0F1.9010005@optrak.com> Date: Wed, 11 Jul 2012 15:09:56 -0700 Message-ID: Subject: Re: DELETE vs TRUNCATE explanation From: Craig James To: Mark Thornton Cc: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=f46d044518117184d004c4951b43 X-Gm-Message-State: ALoCoQlRgD+8sY+XPoBiLXuA/0dW5muQxQXxVGFgLs1F7wAKoacSjk/kptjMDq1Bl6dzwevtpjNG X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201207/141 X-Sequence-Number: 47322 --f46d044518117184d004c4951b43 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jul 11, 2012 at 2:32 PM, Mark Thornton wrote: > On 11/07/12 21:18, Craig James wrote: > >> >> It strikes me as a contrived case rather than a use case. What sort of >> app repeatedly fills and truncates a small table thousands of times ... >> other than a test app to see whether you can do it or not? >> > If I have a lot of data which updates/inserts an existing table but I > don't know if a given record will be an update or an insert, then I write > all the 'new' data to a temporary table and then use sql statements to > achieve the updates and inserts on the existing table. > > Is there a better way of doing this in standard SQL? > If it's a single session, use a temporary table. It is faster to start with (temp tables aren't logged), and it's automatically dropped at the end of the session (or at the end of the transaction if that's what you specified when you created it). This doesn't work if your insert/update spans more than one session. Another trick that works (depending on how big your tables are) is to scan the primary key before you start, and build a hash table of the keys. That instantly tells you whether each record should be an insert or update. Craig > > Mark > > > --f46d044518117184d004c4951b43 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Wed, Jul 11, 2012 at 2:32 PM, Mark Th= ornton <mthornton@optrak.com> wrote:
On 11/07/12 21:18, Craig James wrote:

It strikes me as a contrived case rather than a use case. =A0What sort of a= pp repeatedly fills and truncates a small table thousands of times ... othe= r than a test app to see whether you can do it or not?
If I have a lot of data which updates/inserts an existing table but I don&#= 39;t know if a given record will be an update or an insert, then I write al= l the 'new' data to a temporary table and then use sql statements t= o achieve the updates and inserts on the existing table.

Is there a better way of doing this in standard SQL?=

If it's= a single session, use a temporary table.=A0 It is faster to start with (te= mp tables aren't logged), and it's automatically dropped at the end= of the session (or at the end of the transaction if that's what you sp= ecified when you created it).=A0 This doesn't work if your insert/updat= e spans more than one session.

Another trick that works (depending on how big your tables are) is to s= can the primary key before you start, and build a hash table of the keys.= =A0 That instantly tells you whether each record should be an insert or upd= ate.

Craig
=A0

Mark



--f46d044518117184d004c4951b43--