D
D
dummyQuestions2018-04-17 20:38:39
MySQL
dummyQuestions, 2018-04-17 20:38:39

Why is a composite index in MySQL not fully utilized?

There is a similar table:

CREATE TABLE `test` (
  `id` int(10) unsigned,
  `price` int(10) NOT NULL,
  `product_id` int(10),
  KEY `IdxProductIdPrice` (`product_id`,`price`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

There is a request like this:
SELECT `price`, `id`, `product_id`
FROM `test`
WHERE `product_id` = 1100 
ORDER BY `price` ASC;

explain returns:
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	test	ref	IdxProductIdPrice	IdxProductIdPrice	5	const	38000	Using where

Why is the composite index underused?
If we transfer these fields to the WHERE part, then key_len=9, but if we transfer them to ORDER BY, then it does not use indexes at all, although the table has 1.5 million records.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2018-04-17
@dummyQuestions

The index is stored in a B-tree .
When =you find the right entry is very easy.
With >finding the desired branch is much longer, but still faster than without an index.
With order byall the same it is necessary to touch EVERYTHING. And it's easier to do without an index.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question