Does MySQL index use int or varchar?

Does MySQL index use int or varchar?

This problem involves the use of indexes to store data.
We all know that MySQL uses the InnoDB storage engine by default. When creating indexes, the bottom layer uses B+Tree. Usually, B+Tree has a total of 3 or 4 layers. As shown below:

Insert picture description here
If it is a three-layer B+Treee, if each layer can hold 16kb. Each pair of pointers p and key occupies 10 bytes, and each data occupies 1kb. So how many pieces of data can this B+Tree store? The
answer is: the
first layer: 16 * 1024/10 the
second layer: 16 * 1024/10 The
third layer: 16/1 is
finally: 16 * 1024/10 * 16 * 1024/10 * 16/1≈4.2 * 10^8 data,
this data volume is already very large, so most In this case, a 3-layer B+Tree is sufficient.

In addition, it can be seen from the figure that the first and second layers do not store data. Therefore, if you want to increase the number of stored data, you must reduce the space occupied by the key

Back to the topic : Does MySQL index use int or varchar?

First add some knowledge:

  • int: Integer data (all numbers) from -2^31 (-2,147,483,648) to 2^31 – 1 (2,147,483,647). The storage size is 4 bytes (note: this is fixed).

    • Both int(3) or int(6) can display integers with more than 6 digits. However, when the number is less than 3 or 6 digits, the front will be filled with 0. In other words, the length of int does not affect the storage accuracy of the data, the length is only related to the display, in order to let everyone see more clearly.
      For details, please see: https://blog.csdn.net/xcymorningsun/article/details/73105691
  • varchar: First of all, the character length indicated in the definition of mysql 5.X and above. As in varchar(20) above, you can add 20 English characters or 20 Chinese characters. Represents the character length.

Haha, then it’s time to say the answer:
if you use varchar to express its length in bytes, if your varchar is less than 4 bytes, then use varchar, if varchar is greater than 4 bytes, then Use int.

Guess you like

Origin blog.csdn.net/weixin_42118981/article/details/111174607