Background
I have a lot of different "things" (a domain specific item/entity/subject) that are visible to the "thing" owners (humans). The owners are going to identify their "things" with a number. Instead showing a big "random" number I want to show them a small number (preferably a sequence starting from 1) that is easier for humans. The owners are very comfortable to talk about "my foo 37" and "her bar 128". There "sequence" can have gaps but the attached number have to remain the same during the "thing" intance's lifetime. So I need a way to generate "thing" + owner specific id (currently called as "visible id").
The number of "thing" + owner combinations is in scale of 10k+. Currently new "things" can't be dynamically generated but the owners can be.
The number of one "thing" instances per owner is a relatively small, about tens per owner, but there is no hard cap that can be derived from the business rules. New "thing" instances are created and deleted frequently.
Considered options
I found a good discussion in a SO question Oracle Partitioned Sequence that addresses pretty much the same issue I do have.
So far I have considered the following options:
- I think a standard database sequence would be a perfectly fine but that would require me to dynamically create a big number of "thing" + owner specific sequences and also resolve the sequence name during insert. (And drop the sequences when the owner is gone.) I'm not sure if creating a huge number of sequences is a good practice at all (to me 10k+ database objects is a huge number).
- I also considered notorious
max(visible_id) + 1
but we'll run into normal concurrency issues with that, so it's a no-go. - Don't save the owner specific id into the database at all but instead generate it in the select like suggested by Adam Musch. This is a wonderful idea but unfortunately the id needs to be the same during the "thing" instance lifetime.
- Avoid the whole problem by letting the owners name the "thing". But they didn't like the idea at all - "Why I should bother, it's just so easy to say foo 16." !
Question
Is there some other way to address this issue or should I start creating sequences dynamically ? If the sequences are the answer please elaborate what pitfalls there might be (like implicit commits in DDL).
I'm interested in both Oracle 11gR2 and 12c solutions (if they are different).
Pseudo-code to illustrate the question
create table foo (
id number primary key -- the key for computers
,owner_id number
,visible_id number -- the key for humans
,data_ varchar2(20)
);
create constraint foo_u1 unique foo(owner_id, visible_id);
-- primary key sequence
create sequence foo_id_seq;
insert into foo values(
foo_id_seq.nextval
,1
,1 -- what to put here ?
,'lorem ipsum'
);
insert into foo values(
foo_id_seq.nextval
,2
,1 -- what to put here ?
,'dolor sit amet'
);
select visible_id, data_ from foo where owner = 2 order by visible_id;