Pergunta

I was wondering, which indices take longer to be updated, when an entry is added to a MySQL-table. Those indices with a high cardinality or those with a low cardinality.

Is the a general rule?

Foi útil?

Solução

Both types of tables generally have the same amount of changes. If you change N rows, you need to update N keys.

However, high cardinality has many unique values (such as a key with an UNIQUE constraint), and low cardinality has few unique values.

Few values take less space than many values, so a larger proportion (presumably all) of the index will fit into caches/buffers. Both updates and reads will therefore be served from RAM rather than accessing disk, which means it will be faster.

On the other hand, if cardinality is low, an index is not really all that much useful. After all, you want an index to look up rows quickly. Ideally, 1-5 probes into a tree or hash structure return exactly one row (out of some million/billion rows). Or, a subset of rows, corresponding to some range.
If the index that you use to look up rows returns a set of 10,000 rows every time, this is usually not very useful (there are exceptions, of course).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top