R
R
Roman Yanchuk2015-12-18 23:57:13
MySQL
Roman Yanchuk, 2015-12-18 23:57:13

MySQL: Sorting by primary key or index?

Started optimizing the database. Approached to studying of a question of indexes in MySQL.
As you know, the capacity of queries is determined by the EXPLAIN command.
There is a table ID MEMBER VIP
Query:
SELECT * FROM vip WHERE member='1'
The explain command shows rows - 8500 . That is, to execute the request, you have to check all the rows.
It turns out that I need to set an index on the member field and the problem will be solved.
But! Why with such a request

SELECT * FROM vip WHERE member='1' ORDER BY id DESC LIMIT 0,1

The explain command shows rows - 1 . I mean, everything is great. The note was found immediately.
ID in this case is a PRIMARY KEY, that is, the main index.
It turns out that for unique values, just sorting the field by the primary key is enough?
For non-unique, of course, it will not work, since LIMIT is unknown.
Or do I still misunderstand something and the index on the member field is required in this case?
Thanks in advance for your time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wol_fi, 2015-12-19
@AmorMD

Each leaf of the btree index contains the primary key value for a particular value. (sorry for the tautology)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question