3 ms·
In MySQL and some other database systems, primary keys are clustered indexes. A clustered index defines the order in which data is physically stored on disk. I
by kijin 6y ago
In MySQL and some other database systems, primary keys are clustered indexes. A clustered index defines the order in which data is physically stored on disk.
If you have an auto-incrementing primary key like most sane people do, new rows will be sorted to the very end of the table. Inserting data to this table essentially becomes an append-only operation, which is very fast. On the other hand, inserting random values all over the place is like inserting data in the middle of a file and pushing back everything that comes afterward, over and over again. Even on modern SSD's, this is much slower than simply appending data to the end of a file.
For best performance, your primary keys should be as close as possible to auto-incrementing integers. In other words, each new value should be greater than all the values that currently exist in the table. Timestamps are quite useful for this purpose.
- pas 6y agoThis is an InnoDB thing for MySQL. (The old MyISAM was a simple append only thing with separate index file.)