Primary key best practices #366
-
I have not found any clear documentation on best practices for performance when choosing primary keys for PlanetScale. I am curious if PlanetScale suffers the same performance issues as MySQL when using completely random (not lexicographical generated) primary keys. Would using a nanoid for a primary key affect high volume insert table performance due to the id having to be randomly inserted into the btree instead of appended to the end? Thanks in advance for any insight on this matter. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @CTOJoe , PlanetScale databases are Vitess clusters of Innodb backed MySQL 8 instances so the same general guidelines apply. Use integers and AUTO_INCREMENT for primary keys where possible. If you have to use strings, they should be as short as possible and sequential. Entirely random primary keys have the potential for performance problems on large or busy tables. We use NanoIDs in our own application as external identifiers but the primary keys for those records are integers: |
Beta Was this translation helpful? Give feedback.
Hi @CTOJoe ,
PlanetScale databases are Vitess clusters of Innodb backed MySQL 8 instances so the same general guidelines apply.
Use integers and AUTO_INCREMENT for primary keys where possible. If you have to use strings, they should be as short as possible and sequential. Entirely random primary keys have the potential for performance problems on large or busy tables.
We use NanoIDs in our own application as external identifiers but the primary keys for those records are integers:
https://planetscale.com/blog/why-we-chose-nanoids-for-planetscales-api