Re: Database
| From: | Joe Conway | Date: | Thu, 09 Nov 2000 05:37:51 +0000 |
| Subject: | Re: Database | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24429@lists.php.net to get a copy of this message | ||
> Anyway, what I'm wondering is when you insert into the database, is the
new
> record necessarily always going to be the last record in the database
table?
> In otherwords, to find out the last directory I wrote to can I simply pull
> the last row out of the table? Is it sequential?
You should never count on the most recent insert to be the "last" record.
Instead you should use a sequence to generate a unique, incremented, series
of numbers. If you specify an "id" field as "serial" datatype in PostgreSQL
when you create the table, a sequence is automagically created for you and
the default value of the field is set to the nextval of the sequence. If the
table already exists, you could still create the sequence and add an insert
trigger to accomplish the same thing. Then you can query for the max(id) to
get the last record. If this isn't clear you can email me off-list and I'll
give you some more concrete examples.
>
> Also, if I can just pull out the last row to find out what the last one is
> that I wrote to, is there anyway to do this on the order of magnitude of
> O(1)? or is the only way O(n)? Now I am using ODBC for this and can't
use
> any of the functionality with the pg_... calls. I need an extremely
> efficient way of determining this as it is time critical and I don't want
to
> have to wait.
>
Not sure what you're asking here. If you mean, is there any way to avoid a
serial read through the table, the above suggestion with the addition of an
index on the id field will work.
Hope this helps,
Joe