Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 73968 invoked from network); 20 Oct 2008 12:36:26 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 20 Oct 2008 12:36:25 -0000 Received: (qmail 66783 invoked by uid 500); 20 Oct 2008 12:36:27 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 66751 invoked by uid 500); 20 Oct 2008 12:36:26 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 66740 invoked by uid 99); 20 Oct 2008 12:36:26 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Oct 2008 05:36:26 -0700 X-ASF-Spam-Status: No, hits=-4.0 required=10.0 tests=RCVD_IN_DNSWL_MED,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [192.18.43.132] (HELO sca-es-mail-1.sun.com) (192.18.43.132) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Oct 2008 12:35:16 +0000 Received: from fe-sfbay-10.sun.com ([192.18.43.129]) by sca-es-mail-1.sun.com (8.13.7+Sun/8.12.9) with ESMTP id m9KCZiLV024779 for ; Mon, 20 Oct 2008 05:35:44 -0700 (PDT) Received: from conversion-daemon.fe-sfbay-10.sun.com by fe-sfbay-10.sun.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28 2007)) id <0K9100G01EVPR900@fe-sfbay-10.sun.com> (original mail from Richard.Hillegas@Sun.COM) for derby-user@db.apache.org; Mon, 20 Oct 2008 05:35:44 -0700 (PDT) Received: from richard-hillegas-computer.local ([129.150.17.0]) by fe-sfbay-10.sun.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28 2007)) with ESMTPSA id <0K91004SMEZJOZ40@fe-sfbay-10.sun.com> for derby-user@db.apache.org; Mon, 20 Oct 2008 05:35:43 -0700 (PDT) Date: Mon, 20 Oct 2008 05:35:44 -0700 From: Rick Hillegas Subject: Re: Most efficient way to get max row id? In-reply-to: Sender: Richard.Hillegas@Sun.COM To: Derby Discussion Message-id: <48FC7B20.3030800@sun.com> MIME-version: 1.0 Content-type: text/plain; format=flowed; charset=ISO-8859-1 Content-transfer-encoding: 7BIT References: User-Agent: Thunderbird 2.0.0.17 (Macintosh/20080914) X-Virus-Checked: Checked by ClamAV on apache.org 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 > >