pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Which update action quicker?
3+ messages / 2 participants
[nested] [flat]

* Which update action quicker?
@ 2014-09-23 20:37  Emi Lu <emilu@encs.concordia.ca>
  0 siblings, 1 reply; 3+ messages in thread

From: Emi Lu @ 2014-09-23 20:37 UTC (permalink / raw)
  To: pgsql-performance

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-text-flowed" style="font-size: 18px;"
      lang="x-western"><small>Hello list,
        <br>
        <br>
        For a big table with more than 1,000,000 records, may I know
        which update is quicker please?
        <br>
        <br>
        (1) update t1
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set c1 = a.c1
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from a
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where pk and
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t1.c1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&gt; a.c1;
        <br>
        &nbsp;......
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; update t1
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set c_N = a.c_N
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from a
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where pk and
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t1.c_N&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&gt; a.c_N;
        <br>
        <br>
        <br>
        (2)&nbsp; update t1
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set c1 = a.c1 ,
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c2&nbsp; = a.c2,
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c_N = a.c_N
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp; from a
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp; where pk AND
        <br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (&nbsp; t1.c1 &lt;&gt; a.c1 OR t1.c2 &lt;&gt;
        a.c2..... t1.c_N &lt;&gt; a.c_N)
        <br>
        <br>
        <br>
        Or other quicker way for update action?
        <br>
        <br>
        Thank you
        <br>
        Emi
      </small><br>
    </div>
  </body>
</html>




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Which update action quicker?
@ 2014-09-24 13:48  Heikki Linnakangas <hlinnakangas@vmware.com>
  parent: Emi Lu <emilu@encs.concordia.ca>
  0 siblings, 1 reply; 3+ messages in thread

From: Heikki Linnakangas @ 2014-09-24 13:48 UTC (permalink / raw)
  To: emilu@encs.concordia.ca; pgsql-performance

On 09/23/2014 11:37 PM, Emi Lu wrote:
> Hello list,
>
> For a big table with more than 1,000,000 records, may I know which update is
> quicker please?
>
> (1) update t1
>         set c1 = a.c1
>         from a
>         where pk and
>                    t1.c1       <> a.c1;
>    ......
>         update t1
>         set c_N = a.c_N
>         from a
>         where pk and
>                    t1.c_N       <> a.c_N;
>
>
> (2)  update t1
>         set c1 = a.c1 ,
>               c2  = a.c2,
>               ...
>               c_N = a.c_N
>        from a
>        where pk AND
>                  (  t1.c1 <> a.c1 OR t1.c2 <> a.c2..... t1.c_N <> a.c_N)

Probably (2). <> is not indexable, so each update will have to perform a 
sequential scan of the table. With (2), you only need to scan it once, 
with (1) you have to scan it N times. Also, method (1) will update the 
same row multiple times, if it needs to have more than one column updated.

> Or other quicker way for update action?

If a large percentage of the table needs to be updated, it can be faster 
to create a new table, insert all the rows with the right values, drop 
the old table and rename the new one in its place. All in one transaction.

- Heikki


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Which update action quicker?
@ 2014-09-24 14:13  Emi Lu <emilu@encs.concordia.ca>
  parent: Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Emi Lu @ 2014-09-24 14:13 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; pgsql-performance

Hello,

> For a big table with more than 10 Million records, may I know which update is
> quicker please?
> (1) update t1
>         set c1 = a.c1
>         from a
>         where pk and
>                    t1.c1       <> a.c1;
>    ......
>         update t1
>         set c_N = a.c_N
>         from a
>         where pk and
>                    t1.c_N       <> a.c_N;
>
>
> (2)  update t1
>         set c1 = a.c1 ,
>               c2  = a.c2,
>               ...
>               c_N = a.c_N
>        from a
>        where pk AND
>        (t1.c1, c2...c_N) <> (a.c1, c2... c_N)

Probably (2). <> is not indexable, so each update will have to perform a
sequential scan of the table. With (2), you only need to scan it once,
with (1) you have to scan it N times. Also, method (1) will update the
same row multiple times, if it needs to have more than one column updated.

> Or other quicker way for update action?

If a large percentage of the table needs to be updated, it can be faster
to create a new table, insert all the rows with the right values, drop
the old table and rename the new one in its place. All in one transaction.

The situation is:
(t1.c1, c2, ... c_N) <> (a.c1, c2...c_N) won't return too many diff records. So, the calculation will only be query most of the case.

But if truncate/delete and copy will cause definitely write all more than 10 million data.

If for situation like this, will it still be quicker to delete/insert quicker?
Thank you
Emi



-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2014-09-24 14:13 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-09-23 20:37 Which update action quicker? Emi Lu <emilu@encs.concordia.ca>
2014-09-24 13:48 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2014-09-24 14:13   ` Emi Lu <emilu@encs.concordia.ca>

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