0

I'm wondering the differences or comparisons of having an Auto Increment int Id column or an indexed VARCHAR(32) column. Which one would be faster if I had a second table that references the first table by joining? Like in the second table, would I store the Auto Increment INT id or the indexed VARCHAR column? What are the advantages or disadvantages, if any?

Which would be the more common practice? Create Auto Increment id column for almost every table? Or we can simply index a column that has a unique 36-char ID (like in SugarCRM) so the other tables can just use that when doing joins? I hope I explained my question fairly clear.

2
  • Sounds like you have UUIDs. Use BINARY(16), not VARCHAR(36). If used as foreign key, it won't be as good as INT (4 bytes) or BIGINT (8 bytes) but still 16 is far better than 36. Commented Jun 24, 2016 at 10:26
  • Ok. Yea I remember now about the hexadecimal id's. Commented Jun 24, 2016 at 10:51

1 Answer 1

1

UUIDs are terrible when the table becomes too large to be cached. (If your table will never get very big, you can ignore the rest of my rant.)

The problem comes with the randomness of UUIDs. You fetch one row; it's in one place. You fetch another row, it is extremely likely to be in some other block. This leads to lots of I/O.

In contrast, AUTO_INCREMENT ids are inserted in roughly chronological order. A bunch (say, 100) rows will land in one block before moving on to the next block. This makes INSERTs efficient. It often makes reads efficient, too -- because often you are looking for records of similar vintage -- such as with a date range.

Think of News articles. Most people look at the latest news; the older records collect dust in the cache until they are bumped out. So, the cache tends to keep the latest rows. With UUIDs, old and new articles share the same block.

More on UUIDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.