473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

GENERATED ALWAYS AS IDENTITY clause

I have a table defined as follows:

CREATE TABLE "LOGINS" (
-- UID
"PK_NUMBER" INT GENERATED ALWAYS AS IDENTITY (START WITH 1),
-- Login Date
"DATE" DATE NOT NULL,
...

After doing some inserts with JDBC I have this sequence for PK_NUMBER:

1,2,21,22,23,24,41,42

As far as I understand the docs, the sequence is unique this table and
should be 1,2,3,4,5,...

DB2 OS/2

D:\DB2DATA\$SCRIPTS>db2level
DB21085I Exemplar "DB2" verwendet DB2-Codefreigabe "SQL07026" mit
Aktualitäts-ID "03070105" und den Information-Tokens "DB2 v7.1.0.72",
"n021110" und "WR21312".

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin
Nov 12 '05 #1
10 14098
Ian
Bernd Hohmann wrote:
I have a table defined as follows:

CREATE TABLE "LOGINS" (
-- UID
"PK_NUMBER" INT GENERATED ALWAYS AS IDENTITY (START WITH 1),
-- Login Date
"DATE" DATE NOT NULL,
...

After doing some inserts with JDBC I have this sequence for PK_NUMBER:

1,2,21,22,23,24,41,42

As far as I understand the docs, the sequence is unique this table and
should be 1,2,3,4,5,...


By default DB2 caches 20 values for the identity for performance
reasons. When the database is deactivated this cache is lost, thus
the break in sequence.

Nov 12 '05 #2
Ian wrote:
As far as I understand the docs, the sequence is unique this table and
should be 1,2,3,4,5,...


By default DB2 caches 20 values for the identity for performance
reasons. When the database is deactivated this cache is lost, thus
the break in sequence.


Any idea to avoid this caching? This table isn't time critical but the
sequence mission critical.

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin
Nov 12 '05 #3
Bernd Hohmann wrote:
Ian wrote:
As far as I understand the docs, the sequence is unique this table
and should be 1,2,3,4,5,...

By default DB2 caches 20 values for the identity for performance
reasons. When the database is deactivated this cache is lost, thus
the break in sequence.

Any idea to avoid this caching? This table isn't time critical but the
sequence mission critical.

Use the NO CACHE option.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
AK
> sequence mission critical.

even with the NO CACHE, if you insert, then rollback - you've just got
a gap, correct? Is it acceptable in your situation?

Nov 12 '05 #5
just FYI:
If you use NO CACHE option the performance can degrade , as each
generation of new identity value will force writing to the log.

PS. What if smbdy delete some rows? rollback? crash recovery? If you
really want to prevent gaps and have it sequential write your own
function which will serialize generation (until you commit nobody else
can generate new value) -- it will be very slow, but you can get what
you want.

Nov 12 '05 #6
Artur wrote:
PS. What if smbdy delete some rows? rollback? crash recovery?
Never. Its just a logfile for userlogins and I need the sequence for a
rownumber emulation for web-browsing through the records.
If you really want to prevent gaps and have it sequential write your own
function which will serialize generation (until you commit nobody else
can generate new value) -- it will be very slow, but you can get what
you want.


Define "very slow". Usually I use a 20 digit timestamp (similar to
GENERATE_UNIQUE()) for this which is fast, accurate and collision free.
And for heavier tasks I have a great number generator which can handle
this task too.

But why wake the mice if the DB2 dinosaur is ready to go? I like to to
do the hard way <g>

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin
Nov 12 '05 #7
Ian
Bernd Hohmann wrote:
Artur wrote:
PS. What if smbdy delete some rows? rollback? crash recovery?


Never. Its just a logfile for userlogins and I need the sequence for a
rownumber emulation for web-browsing through the records.


So why are you using an identity column to just get a row number?

select user_id, login_timestamp, ..., rownumber() over (order by
login_timestamp)
from table...
Nov 12 '05 #8
Ian wrote:
So why are you using an identity column to just get a row number?

select user_id, login_timestamp, ..., rownumber() over (order by
login_timestamp)


because I like to scroll through the table in blocks (10 or 20 rows) so
I need a sequence for positioning. Everything else like a WHERE-clause
with the last displayed row as begin marker needs more programming.

Bernd
--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin
Nov 12 '05 #9
Ian
Bernd Hohmann wrote:
Ian wrote:
So why are you using an identity column to just get a row number?

select user_id, login_timestamp, ..., rownumber() over (order by
login_timestamp)


because I like to scroll through the table in blocks (10 or 20 rows) so
I need a sequence for positioning. Everything else like a WHERE-clause
with the last displayed row as begin marker needs more programming.


I understand what you are trying to do. Sorry if I was terse. You app
would execute a statement like:

select * from
(select
...,
rownumber() over (order by login_timestamp) as rn
from ...
) as a
where rn between 10 and 30

Understand? This also gives you the ability to sort data by different
columns.
Nov 12 '05 #10
Ian wrote:
select * from
(select
...,
rownumber() over (order by login_timestamp) as rn
from ...
) as a
where rn between 10 and 30


Well, sounds good. But I have no idea what is the cost of rownumber()
and the subselect if the table grows.

Or better: does it cost more time to create the identity value on every
login (not very often, about 1-2 times per minute) or does it cost more
time to create a subselect on a table with 50.000 entries to fetch 10
rows at the end.

Bernd
Nov 12 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jan van Veldhuizen | last post by:
I have an application which is running fine with MS SqlServer, but it should be working with Oracle as weel. At a lot of places we rely upon the ADO Recordset to return incremented identity...
2
by: db2group88 | last post by:
i would like to know when i create a table with identity column, should i used generated by default or generated as always. since when i create this table, i might copy some data from another table...
2
by: valerio | last post by:
Hi all, I've some problem to import data to DB2 using the db2move and db2look tools. Follow the problem : I have exported data from db2 v. 7 database on windows server, using the db2look and...
17
by: Darek | last post by:
Hi, I have a table, something similar to: create table my_table ( id char(32) not null primary key, num integer not null, code varchar(2) not null, name varchar(60) not null,
13
by: bevanward | last post by:
Hi All I am finding unexpected results when inserted into a newly created table that has a field of datatype int identity (1,1). Basically the order I sort on when inserting into the table is...
5
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as...
0
by: Hiep Trinh | last post by:
What would an Insert look like when a column GENERATED ALWAYS AS IDENTITY ? Thanks.
4
by: Bernard Dhooghe | last post by:
Table definition: CREATE TABLE "SCHEMA1 "."X2" ( "C1" CHAR(20) NOT NULL , "C2" CHAR(10) NOT NULL , "C3" CHAR(30) NOT NULL GENERATED ALWAYS AS (C1|| C2) ) IN "USERSPACE1" ; -- DDL...
1
by: Frank Swarbrick | last post by:
We're trying to take advantage of the new ROW CHANGE TIMESTAMP option. Here is a simple table: CREATE TABLE "ACCTASGN"."NUMBER_STATUS" ( "STATUS_CODE" CHAR(1) NOT NULL , "STATUS_DESCRIPTION"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.