I am pulling my hair out trying to figure out what I am doing wrong. The table is very simple:
CREATE TABLE `icd_index` (
`icd` char(5) NOT NULL,
`core_id` int(11) NOT NULL,
`dx_order` tinyint(4) NOT NULL,
PRIMARY KEY (`icd`,`dx_order`,`core_id`),
KEY `core` (`core_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
As you can see, I've created a covering index that spans all three columns of the table, plus an additional index on the core_id
for potential joins. This is a one-to-many link table with each core_id
mapped to one or more icd
. The table contains 65 million rows.
So, here's the issue. Let's say I want to know how many people have an icd code of '25000'. [That's diabetes, in case you were wondering]. I write a query that looks like this:
SELECT COUNT(core_id) FROM icd_index WHERE icd='25000'
This takes over 60 seconds to execute. I had thought that since the icd column was first in the covered index, counting it would be fast.
More confusing, once I've run the query once, it now runs very quickly. I assumed that was because the query was cached, but even if I RESET QUERY CACHE
, the query now runs in fractions of a second. But, if I wait long enough, it seems to slow down again -- and I can't figure out why.
I am missing something obvious. Do I need an index on icd
alone? Is this the best performance I can get with 65M rows? Why does running the query then resetting the cache affect speed? Are the results being stored in the index?
EDIT: I'm running MySQL 5.6 (in case that matters).
Here's the EXPLAIN
of the query:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE icd_index ref PRIMARY PRIMARY 15 const 910104 Using where; Using index