简体   繁体   中英

Cluster Primary Key in MySQL InnoDB

We have a MySQL Database with a InnoDB table containing two integers fields let say A and B . Now suppose we create a clustered Primary Key using both indexes A and B. Everytime the pair A,B is populated we'll have a different index in our DB, so

  • A=123 and B=456 will produce a unique index,
  • A=123 and B=457 will produce another unique index,
  • A=124 and B= 456 will produce another index and so on...

I was wondering if the situation A=123, B=456 will generate the same index (so is in collision) with A=456, B=123 . If the answer is NO (as I guess) how MySQL combine those fields in order to obtain a unique index? To generate the unique index, the sum A+B is not good for the reason above, the product A*B will waste a lot of space in variables furthermore will fail when just A or B is zero, so what's the algorithm used by MySQL (or any other DB) in order to achieve this result?

MySQL indexes preserve column order, and store the value for each column separately.

You can think of it being like a telephone book. The phone book is like an index over two columns: last name, then first name.

In the phone book, they don't add the names together. They store both, separated by a comma. So "Thomas Jay" is a different entry from "Jay Thomas." It's clear that they are separate fields.

It's the same in a multi-column index. The position of the columns matters. They are not added together or multiplied or anything else.

PS: Your use of "clustered" is a little bit confusing, because a clustered index already has a different meaning in databases. It means the table is stored in index order. I would suggest using "compound index" when you mean an index with more than one column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM