Mike Flynn
Mike Flynn

Reputation: 24325

MySQL Primary Keys and VARCHAR(255)

I have a database using VARCHAR(255) as its primary key on tables and they look like GUIDs. Wouldnt an Int be better for performance?

Upvotes: 2

Views: 2264

Answers (2)

Assaf Karmon
Assaf Karmon

Reputation: 923

It depends on your storage engine, but generally speaking an int/bigint would be better. If you are using innodb, a uuid/guid is a bad choice for a primary key because of the way a clustered index works. read this blog to learn more about it. To sum it up, keys are stored by range and since uuids' are random they would make inserts and lookups less efficient since you would thrash the cache with reading and writing whole memory blocks for each row.

Upvotes: 2

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39991

Ints take less space on disk so you need less I/O when searching. As long as the range suits your need I would say that an int would be faster.

Upvotes: 0

Related Questions