Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

InnoDB COUNT(id) - Why so slow?

Avatar of VoteyDisciple
VoteyDisciple asked on
MySQL Server
5 Comments1 Solution2880 ViewsLast Modified:
I'm clearly missing something fundamental about how InnoDB tables work.  I've created a table...

CREATE TABLE t (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
    -- whole bunch of other other fields
) ENGINE=InnoDB;


I've batch inserted the first, say, 300,000 records (so they all carry distinct and sequential IDs at this point).  No problem.

Then I run the following (highly sophisticated) query:

SELECT COUNT(id) FROM t;

It takes around 20 seconds to run.  EXPLAIN indicates it's using the primary key (i.e. the index), but that with the index it still has to contemplate just under (or sometimes over) 300,000 rows.

OPTIMIZE TABLE has no impact.  Explicitly creating an index has no impact.  Me whacking the server with a hammer has no impact but to summon the systems group to get me out of the machine room.


Seriously, what am I missing that would make this (and, for that matter, other "real" queries) run so slow when they seem so simple?

(I note, incidentally, that after running ALTER TABLE t ENGINE MyISAM, queries run as fast as I want them to. I realize MyISAM inherently allows faster SELECT access, but surely not a 20 second vs. sub-millisecond difference on a query for the total number of rows in the table?)
ASKER CERTIFIED SOLUTION
Avatar of todd_farmer
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers