O
O
Optimus2015-12-03 00:18:23
MySQL
Optimus, 2015-12-03 00:18:23

Performance of indexes in mysql?

From an article on Habré:

The uniqueness of the values ​​in a column affects the performance of the index. In general, the more duplicates you have in a column, the worse the index performs. On the other hand, the more unique values, the better the health of the index. Use a unique index whenever possible.
When you create an index, you must try to reduce the number of duplicates in your key columns. Or more precisely: try to keep the duplicate value ratio as low as possible.

Question: if I have only 0 or 1, or 1,2,3 in a column, i.e. almost all values ​​are duplicates, (and this column is included in the search condition) does it make sense then to create an index on such columns?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nelson, 2015-12-03
Pyan @marrk2

Depends on the distribution of values ​​and queries.
For example, if you have 0 and 1 evenly distributed over the rows, and in the query you are also looking for both 0 and 1, then the index is not particularly needed.
If you have 80% zeros and 20% ones, and in queries you only check for =1, then the index can improve the performance of such a query.
I would be guided by a figure of 20% - if the column has values ​​\u200b\u200bthat are less common than 20% of the rows, and these values ​​are searched, then the index can speed it up.
If I were you, I would start by adding this index and look at the query plan with the EXPLAIN command - if the index is superfluous, then the mysql optimizer will not use it (see the key column in the explain result).
By the way, when executing a query, the mysql optimizer usually selects only one index. It looks among all the columns that are in the where or order by clause and selects the most selective one (this is, if simplified, without details).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question