SunQuest
           DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 17th, 2005, 01:58 PM
AFD62 AFD62 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 7 AFD62 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 33 m 32 sec
Reputation Power: 0
Index question.

This is a basic one but I am not sure why it's working this way. Basically I have a table with an autonumber ID and I want to insert a row and then do a max(id) to get the id of what I just inserted. The id is the primary key so the DDL for it is

ALTER TABLE TBLDETAILTRANS
ADD CONSTRAINT "DETPRIM1" PRIMARY KEY
("DETAILTRANS_ID");

I ran an explain on the select max(id) and it is over 463,000 timerons .......a min only 50.

I thought the index could be read both ways. Do I have to set up a reverse index or can this contstaint be set up differently?

Reply With Quote
  #2  
Old March 17th, 2005, 02:15 PM
pmcnamee pmcnamee is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 19 pmcnamee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 55 sec
Reputation Power: 0
Send a message via ICQ to pmcnamee
what does you query look like?

Have you tried using
Code:
return @@identity
in your query?

Reply With Quote
  #3  
Old March 17th, 2005, 02:23 PM
AFD62 AFD62 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 7 AFD62 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 33 m 32 sec
Reputation Power: 0
The query is SELECT MAX(ID) FROM tblDetail

We are using Enterprise Java Beans to do the updating and on the return we need to send back the id with the data that has been updated. The select is taking about a minute to perform it for each insert. I would think DB2 would be able to go to the last record of the index just as quick as the first record??

Reply With Quote
  #4  
Old March 17th, 2005, 03:44 PM
pmcnamee pmcnamee is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 19 pmcnamee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 55 sec
Reputation Power: 0
Send a message via ICQ to pmcnamee
Sorry, I was thinking of MS SQL Server. In an insert statement you can return @@identity which is a built-in SQL Server variable that returns the id of the last inserted row when the primary key is an identity field. I'm not sure if there is anything like that for DB2.

Good luck.

Reply With Quote
  #5  
Old March 17th, 2005, 08:40 PM
fractalvibes fractalvibes is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Location: Waco, Texas
Posts: 632 fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 14 h 11 m 13 sec
Reputation Power: 22
I think what you are looking for is
select identity_val_local

See this article for more:
http://www.idug.org/idug/member/journal/may01/articl12.cfm

and try this search also:
http://www.ibm.com/Search?v=11&q=identity_val_local&lang=en&cc=us

fv
__________________
...because that is the way we have always done it. We've been doing it like that for 80 Years! (How do we change that mindset?)

Reply With Quote
  #6  
Old April 7th, 2005, 02:40 PM
AFD62 AFD62 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 7 AFD62 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 33 m 32 sec
Reputation Power: 0
select identity_val_local seemed to do it. I put that in the EJB and it returned. Thanks.

Reply With Quote
  #7  
Old April 7th, 2005, 04:41 PM
fractalvibes fractalvibes is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Location: Waco, Texas
Posts: 632 fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level)fractalvibes User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 14 h 11 m 13 sec
Reputation Power: 22
Anytime. Seems like in V 8 they added some new syntax that allows you to get it in the same SQL that does the insert, rather than a second query immediately after the insert. Don't have that reference handy, but it should be on IBM Db2 developerworks.

fv
Comments on this post
AFD62 agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Index question.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway