views:

511

answers:

4

We're using MySQL with InnoDB storage engine and transactions a lot, and we've run into a problem: we need a nice way to emulate Oracle's SEQUENCEs in MySQL. The requirements are: - concurrency support - transaction safety - max performance (meaning minimizing locks and deadlocks)

We don't care if some of the values won't be used, i.e. gaps in sequence are ok. There is an easy way to archieve that by creating a separate InnoDB table with a counter, however this means it will take part in transaction and will introduce locks and waiting. I am thinking to try a MyISAM table with manual locks, any other ideas or best practices?

A: 

Won't the MySQL Identity column on the table handle this?

CREATE TABLE table_name ( id INTEGER AUTO_INCREMENT PRIMARY KEY )

Or are you looking to use it for something other than just inserting into another table?

If you're writing using a procedural language as well (instead of just SQL) then the other option would be to create a table containing a single integer (or long integer) value and a stored procedure which locked it, selected from it, incremented it and unlocked it before returning the value.

(Note - always increment before you return the value - it maximise the chance of not getting duplicates if there are errors - or wrap the whole thing in a transaction.)

You would then call this independently of your main insert / update (so it doesn't get caught in any transactions automatically created by the calling mechanism) and then pass it as a parameter to wherever you want to use it.

Because it's independent of the rest of the stuff you're doing it should be quick and avoid locking issues. Even if you did see an error caused by locking (unlikely unless you're overloading the database) you could just call it a second / third time.

Jon Hopkins
Thanks, your second approach is probably what I had in mind more or less, and the details help a lot. Any comments on what storage engine is best for such a table?
Michael Pliskin
I don't really know MySQL that well so I can't really advise on that (I've done it on SQL Server). What I would say is that I don't think it's that complex so the best approach would potentially be to use whatever you're using for the rest of your stuff and save adding complexity.
Jon Hopkins
A: 

The right way to do this is given in the MySQL manual:

UPDATE child_codes SET counter_field = LAST_INSERT_ID(counter_field + 1);
SELECT LAST_INSERT_ID();
qu1j0t3
+3  A: 

If auto-increment isn't good enough for your needs, you can create a atomic sequence mechanism with n named sequences like this:

Create a table to store your sequences:

CREATE TABLE sequence (
  seq_name varchar(20) unique not null,
  seq_current unsigned int not null
);

Assuming you have a row for 'foo' in the table you can atomically get the next sequence id like this:

UPDATE sequence SET seq_current = (@next := seq_current + 1) WHERE seq_name = 'foo';
SELECT @next;

No locks required. Both statements need to be executed in the same session, so that the local variable @next is actually defined when the select happens.

Arne Claassen
Thanks - that's more or less what we came up with after all - stored procedure that does nearly exactly that.
Michael Pliskin
+2  A: 

Know I am a bit late to the query but hopefully this is a solution you can use. We are a high transaction gaming company and need these sort of solutions for our needs. One of the features of Oracle sequences was also the increment value that could also be set.

The solution uses DUPLICATE KEY...

CREATE TABLE sequences ( id BIGINT DEFAULT 1, name CHAR(20), increment TINYINT, UNIQUE KEY(name) );

To get the next index:

Abstract the following with a stored procedure or a function sp_seq_next_val(VARCHAR):

INSERT INTO sequences (name) VALUES ("user_id") ON DUPLICATE KEY UPDATE id = id + increment;
SELECT id FROM sequences WHERE name = "user_id";

Hope that helps.

kopos
Hm interesting, thanks for the pointer. Is ON DUPLICATE KEY always atomic with INSERT in MySQL?
Michael Pliskin
@Michael: Yes, ON DUPLICATE KEY is atomic.
kopos