Using Multiple Sequences in a Table

Recently, I wanted to use multiple sequences in a table.  I wanted to generate a new sequence for every event for the id of its registrants. The table structure was something like this :

CREATE TABLE registrants (
event_id INT NOT NULL,
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(100),
email VARCHAR(100),
address VARCHAR(100),
PRIMARY KEY ( event_id, id )
) ;

But I ran into a problem. This works only for MyISAM and BDB tables. You can use AUTO_INCREMENT on a secondary column in a multiple-column index only for MyISAM and BDB tables. BDB isn’t supported any longer from MySQL 5.1 and I didn’t want to use MyISAM as my whole schema is based on InnoDB engine and also for MyISAM having its own table-corruption issues.  After lot of trials, debates and finally referring Planet MySQL, I went ahead with this suggestion by Peter from MySQL Performance Blog and some help from here.

CREATE TABLE sequences (
event_id INT(10)  NOT NULL,
val int(10)  NOT NULL,
PRIMARY KEY  (event_id)
) ENGINE=MyISAM DEFAULT CHARACTER SET = utf8  COLLATE = utf8_general_ci;

delimiter $$

CREATE FUNCTION generate_registrant_id(event_id INT)
RETURNS INT
BEGIN

INSERT INTO sequences VALUES (event_id, LAST_INSERT_ID(1))
ON DUPLICATE KEY UPDATE val=LAST_INSERT_ID(val+1);

RETURN IF(LAST_INSERT_ID()=0, 1, LAST_INSERT_ID());

END$$

delimiter ;

I removed AUTO_INCREMENT from the table and call this function to generate the id. The same could be used for maintaining any other sequences in your schema.

Community to the rescue…  again.

For more help refer : Using AUTO_INCREMENT

Leave a Comment

‘EXISTS’ is better than ‘IN’

While working on my last project, I was facing a query which was taking 30 second to execute. The number of tables involved were 10 and total records were around 20k.  It was a search functionality for a web-app project.  There were lot of sub-queries involved in the first draft, all of them using ‘IN’. After doing some research ( within MySQL Community ) , I just replaced all ‘IN’  by ‘EXISTS’ and voila !  The query took 11 seconds to execute.

For example, you could change this query  :

SELECT *

FROM invitees i

WHERE i.event_id IN ( SELECT e.id

FROM event e

WHERE e.city LIKE = ‘ABC’ )

TO

SELECT *

FROM invitees i

WHERE EXISTS ( SELECT 1

FROM event e

WHERE e.city LIKE = ‘ABC’

AND e.id = i.event_id ).

Try it and let me know if you find the same difference in performance as I have. There were many more modifications  to the original query to bring it further down to 1 sec. But I will talk about that later.  For more information on  EXISTS and IN Sub Queries see Reference below.

References :

http://dev.mysql.com/doc/refman/5.0/en/optimizing-subqueries.html

http://dev.mysql.com/doc/refman/5.0/en/in-subquery-optimization.html

Comments (9)

Use ANSI Style for MySQL

I prefer to use ANSI style for writing MySQL queries.  When I started working on SQL, I used the old style aka Theta style for all my queries. Example of a query in  Theta style :

SELECT  e.title, i.first_name, i.response

FROM invitees i,

events e

WHERE e.id = i.event_id

AND e.date = ‘2008-07-04′;

which , in ANSI style is equivalent to :

SELECT e.title, i.name, i.response

FROM invitees i

INNER JOIN events e on e.id = i.event_id

WHERE e.date = ‘2008-07-04′

Many developers prefer using the old style. But believe me, ANSI style is lot better and advantageous.

First, MySQL  supports OUTER JOINs in ANSI style. Second, ANSI style forces you to think about your joins  clearly. Your code is lot more readable in ANSI style. For example, all the joins go in the FROM clause, and the WHERE clause is left with only statements which filter out the result set of the tables in the join.  You get a clear picture of how the tables are linked with each other.

For more information on Joins, see Reference below.

Reference : Join Syntax in MySQL

Comments (2)

Follow

Get every new post delivered to your Inbox.