You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Amir Michail <am...@gmail.com> on 2008/10/19 22:52:44 UTC

Most efficient way to get max row id?

Hi,

I was wondering what is the most efficient way to get the max
automatically generated row id.

select max(...) is slow.

Amir

-- 
http://chatbotgame.com
http://numbrosia.com
http://twitter.com/amichail

Re: Most efficient way to get max row id?

Posted by vadali <sh...@gmail.com>.
Thank you Rick!

That was really helpful!

I need to use option 2, since i cannot retrieve automatically generated
identity numbers on a multi-columns insert
(https://issues.apache.org/jira/browse/DERBY-3609?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel).

that select you posted is exactly what I need in order to maintain the
identities myself for the time being, starting with the last one (+ inc
size) every time the program restarts (or the connection was lost).



Rick Hillegas-2 wrote:
> 
> Hi Amir,
> 
> 1) If what you need to know is the max value on disk right now, then you 
> will need to issue a "select max(...)" statement.
> 
> 2) Maybe, however, all you need to know is the max value that was ever 
> on disk. Of course, this can be a different number than (1) because you 
> may have deleted rows at the upper end.
> 
> If all you need to know is (2), then you can get the answer from the 
> system catalogs. Here's a little script which creates a table with an 
> identity column, inserts and deletes some rows, and then selects the max 
> value that was ever in the identity column. The last query gives you the 
> answer to (2):
> 
> drop table s;
> create table s
> (
>    a int generated always as identity (start with 3, increment by 3),
>    b int
> );
> 
> insert into s( b ) values ( 1 ), ( 2 ), ( 3 ), ( 4 );
> delete from s where b = 4;
> insert into s( b ) values ( 11 ), ( 12 ), ( 13 ), ( 14 );
> 
> select * from s order by b;
> 
> -- now find the max value that was ever put in the identity column
> select c.autoincrementvalue - c.autoincrementinc
> from sys.syscolumns c, sys.systables t
> where t.tablename = 'S'
> and t.tableid = c.referenceid
> and c.columnname = 'A';
> 
> For more information, please see the Reference Guide section describing 
> the SYSCOLUMNS system catalog.
> 
> Hope this helps,
> -Rick
> 
> Amir Michail wrote:
>> Hi,
>>
>> I was wondering what is the most efficient way to get the max
>> automatically generated row id.
>>
>> select max(...) is slow.
>>
>> Amir
>>
>>   
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Most-efficient-way-to-get-max-row-id--tp20060117p22700688.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Most efficient way to get max row id?

Posted by Rick Hillegas <Ri...@Sun.COM>.
Hi Amir,

1) If what you need to know is the max value on disk right now, then you 
will need to issue a "select max(...)" statement.

2) Maybe, however, all you need to know is the max value that was ever 
on disk. Of course, this can be a different number than (1) because you 
may have deleted rows at the upper end.

If all you need to know is (2), then you can get the answer from the 
system catalogs. Here's a little script which creates a table with an 
identity column, inserts and deletes some rows, and then selects the max 
value that was ever in the identity column. The last query gives you the 
answer to (2):

drop table s;
create table s
(
   a int generated always as identity (start with 3, increment by 3),
   b int
);

insert into s( b ) values ( 1 ), ( 2 ), ( 3 ), ( 4 );
delete from s where b = 4;
insert into s( b ) values ( 11 ), ( 12 ), ( 13 ), ( 14 );

select * from s order by b;

-- now find the max value that was ever put in the identity column
select c.autoincrementvalue - c.autoincrementinc
from sys.syscolumns c, sys.systables t
where t.tablename = 'S'
and t.tableid = c.referenceid
and c.columnname = 'A';

For more information, please see the Reference Guide section describing 
the SYSCOLUMNS system catalog.

Hope this helps,
-Rick

Amir Michail wrote:
> Hi,
>
> I was wondering what is the most efficient way to get the max
> automatically generated row id.
>
> select max(...) is slow.
>
> Amir
>
>   


Re: Most efficient way to get max row id?

Posted by Craig L Russell <Cr...@Sun.COM>.
Echoing what Mike Segal said earlier,

Why do you want to know? You might be using the wrong tool for the job.

Craig

On Oct 19, 2008, at 1:52 PM, Amir Michail wrote:

> Hi,
>
> I was wondering what is the most efficient way to get the max
> automatically generated row id.
>
> select max(...) is slow.
>
> Amir
>
> -- 
> http://chatbotgame.com
> http://numbrosia.com
> http://twitter.com/amichail

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: Most efficient way to get max row id?

Posted by Amir Michail <am...@gmail.com>.
On Tue, Oct 21, 2008 at 2:32 PM, Andrew McIntyre <mc...@gmail.com> wrote:
> On Sun, Oct 19, 2008 at 1:52 PM, Amir Michail <am...@gmail.com> wrote:
>> Hi,
>>
>> I was wondering what is the most efficient way to get the max
>> automatically generated row id.
>>
>> select max(...) is slow.
>
> If this is an identity column, perhaps IDENTITY_VAL_LOCAL() suits your needs?
>
> http://db.apache.org/derby/docs/10.4/ref/rrefidentityvallocal.html
>
> andrew
>

I ended up storing the max id in another table (which I get
efficiently via IDENTITY_VAL_LOCAL() ).

Amir


-- 
http://chatbotgame.com
http://numbrosia.com
http://twitter.com/amichail

Re: Most efficient way to get max row id?

Posted by Andrew McIntyre <mc...@gmail.com>.
On Sun, Oct 19, 2008 at 1:52 PM, Amir Michail <am...@gmail.com> wrote:
> Hi,
>
> I was wondering what is the most efficient way to get the max
> automatically generated row id.
>
> select max(...) is slow.

If this is an identity column, perhaps IDENTITY_VAL_LOCAL() suits your needs?

http://db.apache.org/derby/docs/10.4/ref/rrefidentityvallocal.html

andrew

RE: Most efficient way to get max row id?

Posted by de...@segel.com.

> -----Original Message-----
> From: Kai Ponte [mailto:kai@perfectreign.com]
> Sent: Monday, October 20, 2008 4:48 PM
> To: Derby Discussion
> Subject: Re: Most efficient way to get max row id?
> 
> On Monday 20 October 2008 08:52:34 am derby@segel.com wrote:
> > The table would consist of two columns. A label column and a counter
> > column. Your stored procedure has two options. lastVal() and
> getNextVal(),
> > getNextVal(n). (The last one is optional or could be incrementValTo(n) )
> 
> I've done this also in the past. It simply keeps track of the last value
> of an
> inserted item. That way a web page - disconnected recordset - can insert a
> new item then another page can grab the information from that item.
> 
> 
> 
Yes.

This kind of mimics how Informix's Serial column works or Sybase/Oracle's
sequence numbers work. (There's more to it on their part but you can always
make this simple thing more advanced.)

It's an "older technique" that still has its uses.

 



Re: Most efficient way to get max row id?

Posted by Kai Ponte <ka...@perfectreign.com>.
On Monday 20 October 2008 08:52:34 am derby@segel.com wrote:
> The table would consist of two columns. A label column and a counter
> column. Your stored procedure has two options. lastVal() and getNextVal(),
> getNextVal(n). (The last one is optional or could be incrementValTo(n) )

I've done this also in the past. It simply keeps track of the last value of an 
inserted item. That way a web page - disconnected recordset - can insert a 
new item then another page can grab the information from that item.



-- 
kai
www.filesite.org || www.perfectreign.com

 
Clean out a corner of your mind and creativity will instantly fill it. - Dee 
Hock

RE: Most efficient way to get max row id?

Posted by de...@segel.com.
I think the question that hasn't been asked is why do you want to know the
max id of a row?

If you're using an identity column then you shouldn't have to worry about
the id of the row since the identity column will create and maintain the id
for you.

If you want to, you have another option.
You can create a id table and a couple of stored procedures to access this
table.

The table would consist of two columns. A label column and a counter column.
Your stored procedure has two options. lastVal() and getNextVal(),
getNextVal(n). (The last one is optional or could be incrementValTo(n) )

The table will be very small. A couple of rows and the lable,counter would
be unique.

This works well if you have a distributed system and you want to guarantee
unique ids across the databases. (There's more to the design, but the basic
concept is enough to answer this question.)

You can then use the id in your table, and you can always index the column
where the id is being used.

I think there should be a pattern of this somewhere...

HTH

-Mike


> -----Original Message-----
> From: Kai Ponte [mailto:kai@perfectreign.com]
> Sent: Monday, October 20, 2008 12:28 AM
> To: Derby Discussion
> Subject: Re: Most efficient way to get max row id?
> 
> On Sunday 19 October 2008 09:15:55 pm Amir Michail wrote:
> > > Do you have an index on the identity column?  What's your datatype?
> >
> > ~60,000 rows
> >
> > So if I put an index on the identity column, max will be fast?
> 
> It will certainly be faster than without.
> 
> --
> kai
> www.filesite.org || www.perfectreign.com
> 
> 
> Clean out a corner of your mind and creativity will instantly fill it. -
> Dee
> Hock



Re: Most efficient way to get max row id?

Posted by Kai Ponte <ka...@perfectreign.com>.
On Sunday 19 October 2008 09:15:55 pm Amir Michail wrote:
> > Do you have an index on the identity column?  What's your datatype?
>
> ~60,000 rows
>
> So if I put an index on the identity column, max will be fast?

It will certainly be faster than without.

-- 
kai
www.filesite.org || www.perfectreign.com

 
Clean out a corner of your mind and creativity will instantly fill it. - Dee 
Hock

Re: Most efficient way to get max row id?

Posted by Suavi Ali Demir <de...@yahoo.com>.
You should have an index.


--- On Sun, 10/19/08, Amir Michail <am...@gmail.com> wrote:

> From: Amir Michail <am...@gmail.com>
> Subject: Re: Most efficient way to get max row id?
> To: "Derby Discussion" <de...@db.apache.org>
> Date: Sunday, October 19, 2008, 9:18 PM
> On Mon, Oct 20, 2008 at 12:15 AM, Amir Michail
> <am...@gmail.com> wrote:
> > On Sun, Oct 19, 2008 at 11:25 PM, Kai Ponte
> <ka...@perfectreign.com> wrote:
> >> On Sunday 19 October 2008 02:04:29 pm Amir Michail
> wrote:
> >>> On Sun, Oct 19, 2008 at 5:00 PM, Peter
> Ondruška
> >>>
> >>> <pe...@gmail.com> wrote:
> >>> > You want to use identity: Defining an
> identity column
> >>>
> >>> Yes, but how do you get the max row id
> efficiently without adding another
> >>> row?
> >>
> >> In the particular table, how many rows are you
> referring to? A few thousand? A
> >> few million?  A hundred million?
> >>
> >> Do you have an index on the identity column? 
> What's your datatype?
> >
> > ~60,000 rows
> 
> But there's also a where in the query, so the number of
> rows
> considered by max will vary.  A slow case involves ~ 16000
> rows.
> 
> Amir
> 
> >
> > So if I put an index on the identity column, max will
> be fast?
> >
> > Amir
> >
> >>
> >> --
> >> kai
> >> www.filesite.org || www.perfectreign.com
> >>
> >>
> >> Clean out a corner of your mind and creativity
> will instantly fill it. - Dee
> >> Hock
> >>
> >
> >
> >
> > --
> > http://chatbotgame.com
> > http://numbrosia.com
> > http://twitter.com/amichail
> >
> 
> 
> 
> -- 
> http://chatbotgame.com
> http://numbrosia.com
> http://twitter.com/amichail

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: Most efficient way to get max row id?

Posted by Amir Michail <am...@gmail.com>.
On Mon, Oct 20, 2008 at 12:15 AM, Amir Michail <am...@gmail.com> wrote:
> On Sun, Oct 19, 2008 at 11:25 PM, Kai Ponte <ka...@perfectreign.com> wrote:
>> On Sunday 19 October 2008 02:04:29 pm Amir Michail wrote:
>>> On Sun, Oct 19, 2008 at 5:00 PM, Peter Ondruška
>>>
>>> <pe...@gmail.com> wrote:
>>> > You want to use identity: Defining an identity column
>>>
>>> Yes, but how do you get the max row id efficiently without adding another
>>> row?
>>
>> In the particular table, how many rows are you referring to? A few thousand? A
>> few million?  A hundred million?
>>
>> Do you have an index on the identity column?  What's your datatype?
>
> ~60,000 rows

But there's also a where in the query, so the number of rows
considered by max will vary.  A slow case involves ~ 16000 rows.

Amir

>
> So if I put an index on the identity column, max will be fast?
>
> Amir
>
>>
>> --
>> kai
>> www.filesite.org || www.perfectreign.com
>>
>>
>> Clean out a corner of your mind and creativity will instantly fill it. - Dee
>> Hock
>>
>
>
>
> --
> http://chatbotgame.com
> http://numbrosia.com
> http://twitter.com/amichail
>



-- 
http://chatbotgame.com
http://numbrosia.com
http://twitter.com/amichail

Re: Most efficient way to get max row id?

Posted by Amir Michail <am...@gmail.com>.
On Sun, Oct 19, 2008 at 11:25 PM, Kai Ponte <ka...@perfectreign.com> wrote:
> On Sunday 19 October 2008 02:04:29 pm Amir Michail wrote:
>> On Sun, Oct 19, 2008 at 5:00 PM, Peter Ondruška
>>
>> <pe...@gmail.com> wrote:
>> > You want to use identity: Defining an identity column
>>
>> Yes, but how do you get the max row id efficiently without adding another
>> row?
>
> In the particular table, how many rows are you referring to? A few thousand? A
> few million?  A hundred million?
>
> Do you have an index on the identity column?  What's your datatype?

~60,000 rows

So if I put an index on the identity column, max will be fast?

Amir

>
> --
> kai
> www.filesite.org || www.perfectreign.com
>
>
> Clean out a corner of your mind and creativity will instantly fill it. - Dee
> Hock
>



-- 
http://chatbotgame.com
http://numbrosia.com
http://twitter.com/amichail

Re: Most efficient way to get max row id?

Posted by Kai Ponte <ka...@perfectreign.com>.
On Sunday 19 October 2008 02:04:29 pm Amir Michail wrote:
> On Sun, Oct 19, 2008 at 5:00 PM, Peter Ondruška
>
> <pe...@gmail.com> wrote:
> > You want to use identity: Defining an identity column
>
> Yes, but how do you get the max row id efficiently without adding another
> row?

In the particular table, how many rows are you referring to? A few thousand? A 
few million?  A hundred million?

Do you have an index on the identity column?  What's your datatype?

-- 
kai
www.filesite.org || www.perfectreign.com

 
Clean out a corner of your mind and creativity will instantly fill it. - Dee 
Hock

Re: Most efficient way to get max row id?

Posted by Amir Michail <am...@gmail.com>.
On Sun, Oct 19, 2008 at 5:00 PM, Peter Ondruška
<pe...@gmail.com> wrote:
> You want to use identity: Defining an identity column

Yes, but how do you get the max row id efficiently without adding another row?

Amir

>
> On Sun, Oct 19, 2008 at 10:52 PM, Amir Michail <am...@gmail.com> wrote:
>>
>> Hi,
>>
>> I was wondering what is the most efficient way to get the max
>> automatically generated row id.
>>
>> select max(...) is slow.
>>
>> Amir
>>
>> --
>> http://chatbotgame.com
>> http://numbrosia.com
>> http://twitter.com/amichail
>
>



-- 
http://chatbotgame.com
http://numbrosia.com
http://twitter.com/amichail

Re: Most efficient way to get max row id?

Posted by Peter Ondruška <pe...@gmail.com>.
You want to use identity: Defining an identity
column<http://db.apache.org/derby/docs/10.4/devguide/cdevtricks21248.html>

On Sun, Oct 19, 2008 at 10:52 PM, Amir Michail <am...@gmail.com> wrote:

> Hi,
>
> I was wondering what is the most efficient way to get the max
> automatically generated row id.
>
> select max(...) is slow.
>
> Amir
>
> --
> http://chatbotgame.com
> http://numbrosia.com
> http://twitter.com/amichail
>