Re: Index not being used in MAX function (7.2.3)

From: Ernest E Vogelsinger <ernest(at)vogelsinger(dot)at>
To: Paulo Jan <admin(at)mail(dot)ddnet(dot)es>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Index not being used in MAX function (7.2.3)
Date: 2003-06-10 21:52:21
Message-ID: 5.1.1.6.2.20030610234755.03b44698@mail.vogelsinger.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 18:39 10.06.2003, Paulo Jan said:
--------------------[snip]--------------------
>Hi all:
>
> I have here a table belonging to a message board (Phorum 3.3), and
>there's an index in it that is not being used for reasons that I don't
>understand. The table is:
>
> ...
>
> select max(id) from todocinetv;
--------------------[snip]--------------------

Your question has already been answered, but:

I suspect that you're doing this to retrieve the row ID of a newly inserted
row. This may not be foolproof since others could already have inserted
rows in between your insertion and this ID lookup.

Assuming that you're using a sequence to provide the primary key (which you
should) you may safely query its current value:
SELECT currval('todocinetv_id_seq') as "newid"

This is guaranteed to return the last value **for your connection** only,
regardless if the sequence has actually been incremented by others or not.
And it's lightning fast.

Just my 2c :)

--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-06-10 21:55:00 Re: increment_by@
Previous Message Darko Prenosil 2003-06-10 21:49:03 Re: DBCS