InnoDB: SELECT COUNT(*) using primary key 25 times slower on Windows

innodbprimary-key

MySql 5.6, InnoDB. I have a table with 8 million records. It has an integer primary key, and a unique index u1 CHAR(40). It also has a few small fields and a VARCHAR(1000).

SELECT COUNT(*) FROM my_table FORCE INDEX(u1) takes 1.0 s

SELECT COUNT(*) FROM my_table FORCE INDEX(PRIMARY) takes 1.2 s

This is on Linux boxen. But on Windows boxen the times are 2 s and 45 s!

The cnf/ini files are roughly the same. Tweaking buffer sizes etc has been unsuccessful so far. There seems to be a big problem with primary key lookups on Windows. Perhaps a bug in InnoDB on Windows? Or what?

Best Answer

It turns out that the difference was the size of innodb_buffer_pool_size. On the Win box it was too low.

It appears that, if you need the primary key, all the records (8 million times 1000 chars) need to be read. InnoDB seems to use the primary key as a "clustered index", meaning that it's welded to each record. That's why reading just the values of the secondary key takes so much less memory, and thus less use of the memory buffer. If it's too small, the read will take a long time.

Increasing innodb_buffer_pool_size to 8G helped speed up operations.

I certainly don't understand the minute details, please correct me if this is nonsense.