Home » RDBMS Server » Server Administration » Why the value of sequence will change after reboot?
Why the value of sequence will change after reboot? [message #124267] Fri, 17 June 2005 03:05 Go to next message
rushing
Messages: 135
Registered: January 2004
Location: Beijing,China
Senior Member
I create a sequence in Oracle.
Every time after I reboot the system , the sequence value will change.
Why?
Re: Why the value of sequence will change after reboot? [message #124279 is a reply to message #124267] Fri, 17 June 2005 04:28 Go to previous messageGo to next message
JSI2001
Messages: 1016
Registered: March 2005
Location: Scotland
Senior Member
Hi
If you are caching values then when you shutdown the db, the cached values will be cleared, when you reboot, the seq will start at the first available number AFTER the last number that was in the previous set of cached values.

HTH
Jim
Re: Why the value of sequence will change after reboot? [message #124511 is a reply to message #124267] Sun, 19 June 2005 16:23 Go to previous messageGo to next message
DMcG
Messages: 51
Registered: May 2005
Location: Auckland, New Zealand
Member
The default option is for a new sequence to cache 20 values. These values are lost if the system is shutdown.
Try usning the nocache option when creating your sequences.

Dougie McGibbon
Re: Why the value of sequence will change after reboot? [message #124537 is a reply to message #124279] Sun, 19 June 2005 22:00 Go to previous messageGo to next message
rushing
Messages: 135
Registered: January 2004
Location: Beijing,China
Senior Member
I just use the db in client PC, use SQL statement, such as insert and select.
I do not know if there is cache in the server.
Re: Why the value of sequence will change after reboot? [message #124538 is a reply to message #124511] Sun, 19 June 2005 22:02 Go to previous messageGo to next message
rushing
Messages: 135
Registered: January 2004
Location: Beijing,China
Senior Member
How to set nocache option in Oracle ?
will this method slow down the db server?
Thanks!

Re: Why the value of sequence will change after reboot? [message #124581 is a reply to message #124538] Mon, 20 June 2005 06:28 Go to previous messageGo to next message
salmansh
Messages: 14
Registered: June 2005
Location: Al-Khobar
Junior Member

Use the ALTER SEQUENCE command to change the sequence.
No it will not slow down the db.
Re: Why the value of sequence will change after reboot? [message #124622 is a reply to message #124267] Mon, 20 June 2005 09:20 Go to previous messageGo to next message
smartin
Messages: 1803
Registered: March 2005
Location: Jacksonville, Florida
Senior Member
I think the correct question to be asking is why do you care if you cache a few sequence numbers and lose them when you reboot the server? You aren't going to run out of numbers.

And the reason you cache them is so that you don't have to generate them one at a time, so there can be a performance impact (on an active enough system) using nocache.

Re: Why the value of sequence will change after reboot? [message #124642 is a reply to message #124622] Mon, 20 June 2005 13:23 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Don't worry about missing sequence numbers. It's virtually impossible to create a gap-free sequence without doing some serious sacrifices to maintainability and scalability.

Search http://asktom.oracle.com for his opinion (and examples) on this matter. Look also at this discussion.

MHE
Re: Why the value of sequence will change after reboot? [message #124767 is a reply to message #124642] Tue, 21 June 2005 06:36 Go to previous messageGo to next message
rushing
Messages: 135
Registered: January 2004
Location: Beijing,China
Senior Member
My application do these things:

step 1 create a picture named by the (old max number + 1).jpg
step 2 insert the picture into the database
step 3 get the new number created by oracle
step 4 show the picture named by (new number).jpg

After reboot the system, there will be an error because the new number is not equal the old max number + 1 .

Re: Why the value of sequence will change after reboot? [message #124772 is a reply to message #124767] Tue, 21 June 2005 07:35 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
rushing wrote on Tue, 21 June 2005 10:36

After reboot the system, there will be an error because the new number is not equal the old max number + 1 .
Why?

Have you read the links I provided you? I don't see why name_NN must be following name_(NN-1) and must be preceding name_(NN+1). I really don't.

MHE
Re: Why the value of sequence will change after reboot? [message #124786 is a reply to message #124267] Tue, 21 June 2005 09:04 Go to previous messageGo to next message
smartin
Messages: 1803
Registered: March 2005
Location: Jacksonville, Florida
Senior Member
Run step 3 before step 1.

Then in step one change "old value + 1" to "new number" (the same number you use in step 4).
Re: Why the value of sequence will change after reboot? [message #124866 is a reply to message #124786] Tue, 21 June 2005 22:14 Go to previous messageGo to next message
rushing
Messages: 135
Registered: January 2004
Location: Beijing,China
Senior Member
The new number could not be got before you insert a new record.
I have to predict the value of new number.
Re: Why the value of sequence will change after reboot? [message #124867 is a reply to message #124866] Tue, 21 June 2005 22:16 Go to previous messageGo to next message
rushing
Messages: 135
Registered: January 2004
Location: Beijing,China
Senior Member
Maybe there is some method to get the value of sequence before insert SQL statement?

[Updated on: Tue, 21 June 2005 22:16]

Report message to a moderator

Re: Why the value of sequence will change after reboot? [message #124868 is a reply to message #124772] Tue, 21 June 2005 22:24 Go to previous messageGo to next message
rushing
Messages: 135
Registered: January 2004
Location: Beijing,China
Senior Member
Maaher wrote on Tue, 21 June 2005 08:05

rushing wrote on Tue, 21 June 2005 10:36

After reboot the system, there will be an error because the new number is not equal the old max number + 1 .
Why?

Have you read the links I provided you? I don't see why name_NN must be following name_(NN-1) and must be preceding name_(NN+1). I really don't.

MHE



Dear Maahr,

I read your link now, and found it is a serious problem of oracle.
These articles are helpful.
Thanks!
Re: Why the value of sequence will change after reboot? [message #124959 is a reply to message #124267] Wed, 22 June 2005 09:18 Go to previous messageGo to next message
smartin
Messages: 1803
Registered: March 2005
Location: Jacksonville, Florida
Senior Member
What serious problem does oracle have?

And yes, you can get both the current value and the next value from a sequence. Suggest you also read this:

http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10759/pseudocolumns002.htm#sthref685
Re: Why the value of sequence will change after reboot? [message #125020 is a reply to message #124267] Wed, 22 June 2005 16:05 Go to previous messageGo to next message
DMcG
Messages: 51
Registered: May 2005
Location: Auckland, New Zealand
Member
To quote my earlier post
Quote:

Try usning the nocache option when creating your sequences.

(sic)

create sequence my_new_sequence nocache;


... will stop oracle cacheing sequence numbers for this sequence.
This should achieve what you want.

Dougie McGibbon

[Updated on: Wed, 22 June 2005 16:06]

Report message to a moderator

Re: Why the value of sequence will change after reboot? [message #125108 is a reply to message #125020] Thu, 23 June 2005 06:05 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Consider rollbacks. Every time a sequence.NEXTVAL is called the no is incrmented, nomatter how your transaction ends.

A small test case. The code below is attached as a SQL*Plus script. Run it yourself.
CREATE TABLE nocache_table(the_id NUMBER)
/

CREATE SEQUENCE nocache_sequence
 START WITH 1 INCREMENT BY 1
NOCACHE
/

DECLARE
  the_number NUMBER;
BEGIN
  -- perform 20 inserts (not all are commited)
  FOR i IN 1..20 LOOP
    SELECT nocache_sequence.NEXTVAL
      INTO the_number
      FROM dual;

    INSERT INTO nocache_table(the_id)
    VALUES ( the_number );

    -- randomly commit or rollback
    IF MOD( 
            ROUND( dbms_random.value(1,4) )
          , 2
          ) = 0
    THEN
      COMMIT;
    ELSE
      ROLLBACK;
    END IF;
  END LOOP;
END;
/

SELECT the_id
  FROM nocache_table
/

DROP SEQUENCE nocache_sequence
/

DROP TABLE nocache_table
/


See for yourself. The result is clear: there are missing numbers!

And another thing about NOCACHE:
from asktom thread "SEQUENCES"


and setting nocache is like setting:


fast = false
slow = true
I_want_to_be_as_inefficient_as_possible = Please


I repeat my original question: why would it be a problem if there are gaps? It's not likely that you run out of sequence numbers so that can't be the reason.

And have you considered deletes and/or updates of your key column? If they are allowed, you have to create some resequencing mechanism.

If it's a business requirement and you cannot change it (sad but true), a nightly batch assigning consequetive numbers to the records that haven't received one yet could be one way to minimise gaps. ( No 100% foolproof solution )

Another solution, not so user friendly would be a single row table holding the last value and a function returning this value and incrementing it. That way, a lock is placed ( due to the update of the value ) and you're sure that no other user is fiddling with it. You can see immediately that this is not ideal in a multi user environment. ( foolproof, but performance killer in the worst kind of way ). This would be like putting a document in a version control system and each time check it out, add a page and check it back in. Every other user has to wait for the document to become available again. Imagine this at a site like a bank! Marvelous Very Happy

I have tried entirely different approach: add a timestamp column to your table holding the creation date and place a view on top of the original table, containing ROWNUM sorted by this timestamp column. (See script below)
CREATE TABLE the_table(the_id NUMBER, created_on TIMESTAMP)
/

CREATE VIEW the_view
AS
SELECT ROW_NUMBER() OVER ( ORDER BY created_on ) idx
     , the_id
  FROM the_table
/


CREATE SEQUENCE nocache_sequence
 START WITH 1 INCREMENT BY 1
NOCACHE
/

DECLARE
  the_number NUMBER;
BEGIN
  -- perform 20 inserts (not all are commited)
  FOR i IN 1..20 LOOP
    SELECT nocache_sequence.NEXTVAL
      INTO the_number
      FROM dual;

    INSERT INTO the_table(the_id, created_on)
    VALUES ( the_number, SYSTIMESTAMP );

    -- randomly commit or rollback
    IF MOD( 
            ROUND( dbms_random.value(1,4) )
          , 2
          ) = 0
    THEN
      COMMIT;
    ELSE
      ROLLBACK;
    END IF;
  END LOOP;
END;
/

SELECT the_id
  FROM the_table
/

SELECT idx
     , the_id
  FROM the_view
/

DROP SEQUENCE nocache_sequence
/

DROP view the_view
/

DROP TABLE the_table
/

The view seems to be a nice solution for this "problem". Perhaps this can help you out.

Just my 2 cents...


MHE

[Updated on: Sun, 26 June 2005 09:38]

Report message to a moderator

Re: Why the value of sequence will change after reboot? [message #125424 is a reply to message #125108] Fri, 24 June 2005 19:22 Go to previous message
bodger
Messages: 3
Registered: June 2005
Junior Member

Or just use a returning clause on the insert statement to get the number back and save it in a global variable or configuration table.

Personally I'm with the cache, don't care if the number is sequential crowd.
Previous Topic: problem starting oracle 8i and logging
Next Topic: Migration problem,from SQL Server to Oracle
Goto Forum:
  


Current Time: Fri Sep 27 00:19:05 CDT 2024