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 the size of the table is around 1 million rows and it is increasing.
My question is: does the "Composite Primary Key" decrease the performance of the Inserts and Selects on this table?
Should I be using a simple Auto-Increasing INT ID field instead of a Composite Primary Key? (I think the answer is very much related to the way MySQL handles the Indexes on multiple columns)
INSERT
andUPDATE
performance varies little: it will be almost same for(INT)
and(INT, INT)
keys.SELECT
performance of compositePRIMARY KEY
depends on many factors.If your table is
InnoDB
, then the table is implicitly clustered on thePRIMARY KEY
value.That means that searches for both values will be faster if the both values comprise the key: no extra key lookup will be required.
Assuming your query is something like this:
and the table layout is this:
, the engine will just need to lookup the exact key value in the table itself.
If you use an autoincrement field as a fake id:
, then the engine will need, first, to lookup the values of
(col1, col2)
in the indexix_mytable_col1_col2
, retrieve the row pointer from the index (the value ofid
) and make another lookup byid
in the table itself.For
MyISAM
tables, however, this makes no difference, becauseMyISAM
tables are heap organized and the row pointer is just file offset.In both cases, a same index will be created (for
PRIMARY KEY
or forUNIQUE KEY
) and will be used in same way.SELECT
s a tiny bit, though the effect is pretty much negligible and not worth worrying about.INSERT
s, and you certainly are doing enoughINSERT
s to worry about it. This is much more of a concern if it's a MyISAM table, where anINSERT
locks the table, than if it's an InnoDB table. If, by going with the auto_increment primary key, you would be able to leave those columns unindexed, you would benefit from the change. If you would still need to keep those three columns indexed, though (for example, if you need to enforce uniqueness on the combination of them), it isn't going to do anything for you performance-wise.If it's InnoDB, the composite primary key will be included in each entry in each of the secondary indexes.
This means that
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.