Search code examples
mysqlperformanceprimary-keyinnodbvarchar

MySQL: size of VARCHAR as PK similarly as fast as an INT?


What would be a good/safe maximum length for a VARCHAR column as primary key not being much/any slower than an INTEGER ID using MySQL 5 + InnoDB on a 64 bit system? Note, that this PK should be assumed to be referenced by other tables, so it will appear in a number of JOINs.

Would a VARCHAR(7) be a good length? 6? 8? 10? More? Less? Why?

It might be hard to answer, but there should at least be an upper limit based on facts, e.g. based on the inner workings of MySQL/InnoDB (index structures, ... ?).

Edit: Assume the ASCII character encoding, case sensitive.


Solution

  • Using an int field is generally preferable for keys. Two numbers can be compared in a single assembler instruction on pretty much any platform in existence. Comparing two strings will invariably require a loop and extra setup steps in advance, or take multiple cpu cycles even if the cpu has string comparison instructions built in.