Re: Moving from MySQL to PGSQL....some questions (multilevel - Mailing list pgsql-general

From Tom Lane
Subject Re: Moving from MySQL to PGSQL....some questions (multilevel
Date
Msg-id 19484.1078359177@sss.pgh.pa.us
Whole thread Raw
In response to Re: Moving from MySQL to PGSQL....some questions (multilevel  (Michael Chaney <mdchaney@michaelchaney.com>)
List pgsql-general
Michael Chaney <mdchaney@michaelchaney.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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Mistake in my query or Index Scan on subquery failure? (7.4)
Next
From: phil campaigne
Date:
Subject: Setting up Postgresql in Linux