Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How InnoDB sorts multi column primary keys

I know that the primary key in InnoDB is clustered. So that using an auto increment integer as the primary key has the benefit of already being sorted and improves the insert performance.

But the issue is I have a table like this with 2 columns id and hash (and some other but not important );

id int auto increment
hash Guid

I need to partition this table on the hash column, that means I have to put it in the primary key.

So I'm thinking of creating a primary key as (id,hash).The id column will be used in the joins with the other tables.

Problem is if I have the PK as (id,hash) how will InnoDB sort it ?

Will it use the id column first and then hash or will it use some other way to determine the order of the key ?. The former will be better as it benefits from the already sorted auto inc column but I could not find any info about how the sorting will be done on the composite columns.

Any insight into this is greatly appreciated :)

like image 795
Amila Avatar asked Nov 14 '22 21:11

Amila


1 Answers

A primary key on multiple colums is managed by MySQL as a concatenation of the chosen columns values. Thus, the first column can be sorted faster, as it will be at the beginning of the aggregate.

like image 198
Mathieu Rodic Avatar answered Dec 10 '22 07:12

Mathieu Rodic