Question

I have a mysql database called key_word. It has 3 columns, 23 million rows.

Please have a look at the below query.

SELECT `indexVal` FROM `key_word` WHERE `hashed_word`='001'

This query takes around 6 seconds to display me the result. Result contains 169669 rows. This is very very slow. However, I have noticed that this occurs "only" if I trying to get records in indexVal. All the below queries are very fast.

SELECT `primary_key` FROM `key_word` WHERE `hashed_word`='001'
SELECT `hashed_word` FROM `key_word` WHERE `indexVal`=0

All these 3 columns are indexed separatly, so I don't understand why it is slow when I am trying to get data from indexVal by searching hashed_word

Below is my Table structure.

enter image description here

Below is some more information about the table

enter image description here

So, why this selecting from indexVal while searching from hashed_word is slow? How can I solve this?

UPDATE

As requested, I am posting the result of

EXPLAIN SELECT `indexVal` FROM `key_word` WHERE `hashed_word`='001'

below

enter image description here

UPDATE

The result of SHOW VARIABLES LIKE '%query_cache_size%'; SHOW VARIABLES LIKE '%query_cache_type%'; is below

enter image description here

Was it helpful?

Solution

SELECT primary_key FROM key_word WHERE hashed_word='001' is faster than SELECT indexVal FROM key_word WHERE hashed_word='001' because in InnoDB the primary key value is always included in any secondary index; this means that primary_key is read from the index. In the second query however, MySQL first reads the primary key from the index, than it reads the value of indexVal from the table row. In other words it persoms 2x disk reads.

OTHER TIPS

You could try to make an index over hashed_word and indexVal (in that order).

This index will be fast for selecting based on a criterion on hashed_word AND the index includes indexVal in its index data so the data does not need to be read from the disk.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top