Re: Moving from MySQL to PGSQL....some questions (multilevel

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Chaney <mdchaney(at)michaelchaney(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Moving from MySQL to PGSQL....some questions (multilevel
Date: 2004-03-04 00:12:57
Message-ID: 19484.1078359177@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Chaney <mdchaney(at)michaelchaney(dot)com> writes:
> begin;
> lock table test in exclusive mode;
> insert into test values (1,(select max(id2) from test where id1=1)+1);
> commit;

> It's not pretty, and it'll probably slow down as the table grows.

As-is, that will definitely get pretty slow on large tables. You could
avoid the slowdown with the standard hack for replacing max() with an
index probe:

insert into test values (1,
(select id2+1 from test where id1=1 order by id1 desc, id2 desc limit 1)
);

This will be fast if there is a double-column index on (id1, id2).

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message phil campaigne 2004-03-04 00:17:52 Setting up Postgresql in Linux
Previous Message Karl O. Pinc 2004-03-03 23:22:44 Re: Moving from MySQL to PGSQL....some questions (multilevel