Composite Primary Key performance drawback in MySQL

后端 未结 3 1261
-上瘾入骨i
-上瘾入骨i 2020-11-30 01:38

We have a table with a composite Primary key consisting of three fields (and it is in MySQL 5.1). There are near 200 inserts and 200 selects per second on this table, and th

3条回答
  •  半阙折子戏
    2020-11-30 02:10

    If it's InnoDB, the composite primary key will be included in each entry in each of the secondary indexes.

    This means that

    • Your secondary indexes will take up as much space as those columns + all the columns in the primary key
    • You can use a secondary index as a covering index if all the columns required are contained in the secondary index + pk

    These are of course, a disadvantage and an advantage respectively.

    Composite primary keys are not necessarily bad, sometimes they can be really helpful because InnoDB clusters them - which means that (disc-bound) range scans over the PK can be satisfied using far fewer IO operations than would be required on a non-clustered index.

    Of course if you've got foreign keys in other tables, they're wider as well as they need to include the whole key from your main table.

    But I'd say on balance, generally, no. Having a composite primary key does NOT cause a problem by itself. Having a "big" primary key (e.g. big varchars) may do however, if that outweighs the advantages of clustering and being able to use covering indexes.

提交回复
热议问题