1

Basically what the title says: What are the differences in performance (if any) between using a clustered index on a GUID column vs a clustered index on an INT column, especially when those columns are part of your join predicates?

Edit: To clarify, I was always under the assumption GUIDs typically perform worse than INTs when used in join predicates. I understand a GUID is 4x the data size of an INT, but I was asking my question to really understand why and what's going on "under the hood" that causes GUIDs to perform worse, even after the indexes are rebuilt and there's minimal fragmentation differences between the two types.

  • 2
    This answer and this question might cover most of your question. – LowlyDBA Sep 7 '18 at 15:01
  • 2
    Even Microsoft seems to think so. – Erik Darling Sep 7 '18 at 15:15
  • 1
    @sp_BlitzErik interesting, though not sure I agree with the rule description. I haven't tested it, but I would not have thought that a VARCHAR(4) using a binary collation, or even a BINARY(4) would be slower than comparing an INT. Maybe I should test it ;-) – Solomon Rutzky Sep 7 '18 at 15:38
  • 2
    @SolomonRutzky oh I know, I just like throwing that out there. Heh. – Erik Darling Sep 7 '18 at 15:39
  • 1
    @J.D. I went looking. – Erik Darling Sep 7 '18 at 15:39
2

I understand a GUID is 4x the data size of an INT, but I was asking my question to really understand why and what's going on "under the hood" that causes GUIDs to perform worse, even after the indexes are rebuilt and there's minimal fragmentation differences between the two types.

I would say that the issue is primarily just what you started out with here, in what I quoted: UNIQUEIDENTIFIER is 4x bigger than INT (16 bytes compared to 4). This hurts performance in three ways (I am ignoring fragmentation since the question explicitly accounts for that):

  1. Every bit compared is another thing to do. More things to do for the CPU takes more time. That time won't be noticed on modern systems when looking at 100 rows. But over 1 million rows? Now you are talking about (in terms of bytes), comparing 16 million things vs. comparing only 4 million things.

  2. Memory is not free: neither in terms of money, nor in terms of time spent allocating and deallocating. In order to join rows, data pages containing those rows must be read into memory (the buffer pool). There is not an infinite amount of memory (especially if you are using Standard Edition or Express Edition). The more memory you take up in one operation is that much less left over for other concurrent operations. But even if memory were infinite, it still takes time to load those values into memory so that they can then be compared. Loading 16 million bytes naturally takes longer than loading 4 million bytes.

  3. Disk is not free: even with SSD, physical I/O is still a cost you pay, which is why things are cached in memory. Data pages are only 8kb. If you have a 16 byte value, it will reduce the number of rows that can fit on that 8kb data page as compared to having a 4 byte value. The more data pages that a table takes up, the more physical I/O is needed to read and write the same number of rows.

    A related issue is with indexes. You will likely index the columns that you are joining on. Same concern as with the main table: fewer index rows per page means more pages required for the index, meaning more time spent reading and writing to every index affected by any given operation (new row in a table will need to create that new row in all indexes on that table (minus filtered indexes that would exclude it), and assuming we are talking about the clustered index, the clustered index key is copied to all of the non-clustered indexes)

  • 1
    Great response, much appreciated! I have to dispute #2 though - specifically that you don't need money to get more RAM when you can just download it: downloadmoreram.com/download.html (you set yourself up for that one ;). – J.D. Sep 7 '18 at 15:57
  • 1
    Nice. A website that Rick Rolls folks. That video could not be more 80s ;-) – Solomon Rutzky Sep 7 '18 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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