We had discussion today which involved benchmarks of Join speed for MyISAM and Innodb storage engines for CPU bound workload, this is when data size is small enough to fit in memory and so buffer pool.
I tested very simple table, having with about 20.000 rows in it on 32bit Linux. The columns “id” “i” and “c” were populated with same integers so we can allow the same job to be done using different kinds of columns – primary key, integer indexed column and indexed char column. The query is also trivial – the point was to make sure it is not index covered query so it reads the rows and it does not return many rows. I varied the join clause to be id, i and C columns appropriately.
CREATE TABLE `t1` (
`id` int(10) unsigned NOT NULL default '0',
`i` int(10) unsigned NOT NULL default '0',
`c` char(15) default NULL,
`pad` char(8) default NULL,
PRIMARY KEY (`id`),
KEY `i` (`i`),
KEY `c` (`c`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
select count(t1.pad),count(t2.pad) from t1,t1 t2 where t1.id=t2.id;
The result I’ve got are as follows
As you see in such circumstances Innodb is actually faster than MyISAM in 2 cases out of 3. I guess the reasons are the following:
- Innodb primary key joins are very fast as data is clustered together with index and generally highly optimized
- Innodb builds hash indexes which helps to speed up lookup by indexes by passing BTREE index and using hash, which is faster
- MyISAM does compression for character keys which makes it perform slower for random lookups
- MyISAM generally has lower processing overhead due to its simplicity
- MyISAM still a bit better by primary key join than for secondary key join. I guess because it knows for sure there is no more than one row which matches the index, so there is no need for MySQL to request next row matching index
Note: This applies to CPU bound workload with all content fitting in memory. In other cases situation is very different and MyISAM compression for char keys could frequently positevely impact performance.