Mysql – Auto increment id column vs index VARCHAR columns

indexMySQL

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.

Best Answer

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.