You are here

Stored Function to generate Sequences

MySQL Performance Blog - Thu, 03/04/2008 - 3:09am

Today a customer asked me to help them to convert their sequence generation process to the stored procedure and even though I have already seen it somewhere I did not find it with two minutes of googling so I wrote a simple one myself and posting it here for public benefit or my later use :)

  1. delimiter //
  2. CREATE FUNCTION seq(seq_name char (20)) returns int
  3. begin
  4.  UPDATE seq SET val=last_insert_id(val+1) WHERE name=seq_name;
  5.  RETURN last_insert_id();
  6. end
  7. //
  8. delimiter ;
  10. CREATE TABLE `seq` (
  11.   `name` varchar(20) NOT NULL,
  12.   `val` int(10) UNSIGNED NOT NULL,
  13.   PRIMARY KEY  (`name`)
  16. INSERT INTO seq VALUES('one',100);
  17. INSERT INTO seq VALUES('two',1000);

This implementation uses single table to maintain multiple sequences which application can use in desired way, though you of course could have them using different tables.

I use MyISAM tables here which allows to use such sequences in transactions without serializing transactions which require access to same sequence, though it is not as safe as if you use Innodb table in this case.

Even though implementation is just 2 lines of code it seems to confuse a lot of people because last_insert_id() is used rather unusual way - with argument. This way of using this function allows you to "inject" the value to be returned next time this function is called.

Sometimes people wonder why you would like to use sequences instead of MySQL auto_increment columns ? Leaving aside more exotic ways of sequences even pure sequential value as in the case above can be quite helpful - in MySQL 5.0 you may with to use them instead of auto_increment with Innodb tables to avoid short term "table level locks" which innodb sets when Insert is happening in the table with auto_increment values. It is also helpful if you need to decouple ID generation from storing the data - for example IDs are generated on central server and when stored on number of servers or to number of individual tables.
Finally your own sequences allow you to generate multiple sequence values on demand with single statement for one or more sequences, which also can be helpful:

  1. mysql> SELECT seq('one'),seq('two'),seq('one'),seq('one');
  2. +------------+------------+------------+------------+
  3. | seq('one') | seq('two') | seq('one') | seq('one') |
  4. +------------+------------+------------+------------+
  5. |        102 |       1002 |        103 |        104 |
  6. +------------+------------+------------+------------+
  7. 1 row IN SET (0.00 sec)

I should note this sequence generation requires serialization, though it is short term but it may still become the bottleneck for application with high sequence use rate. For such heavy duty apps I would use another approaches - in particular allocating "ranges" of sequences and caching them in applications, using UUID_SHORT() and other methods which do not require global lock for each time new sequence value needs to be retrieved.

Entry posted by peter | No comment

Add to: delicious | digg | reddit | netscape | Google Bookmarks