I have a table with two specific columns, one is auto-incremented number, the other is a custom number. These two columns will be fetched together in the where condition frequently. Now I have 2 choices with InnoDB engine:
-
Use the auto-incremented number as the primary key, while create an index on the custom number column. this choice will be very quickly on inserting because the new record will be inserted one by one althoug it will cause some merge or split on the clustered index. But it will take extra time and space to construct the index on the custom number column.
-
I can use the auto-incremented number and the custom number column together as the primary key. On inserting, the record will not be placed one after another, so it will cause frequent B Tree primary index split and merge.
These two choice will both speed the query which use the auto-incremented number and custom number column as filter condition. But if we consider the insertion and space, which one would be prefered?
Thanks very much!
The composite primary key seems to be better in this case, since the key becomes a coverage index. And why do you think it will require splits? The autoincremented field has to be the first in the composite primary key in InnoDB, that is all new rows will be appended to the end.
And an autoincremented field in InnoDB is unique across the table even in a composite primary key, as opposed to that in MyIsam where it restarts for each new rest part of the primary key.