Sizeof NULL value of uniqueidentifier

Jan Vávra 71 Reputation points
2022-09-20T07:40:12.783+00:00

I am designing a table and thinking about sizeof null value of uniqueidentifier type. The table will have 10 such columns and they will be often NULL.
Will be the size 10*16 B, or (10 - count of NULL columns) * 16 B?

Maybe it would be better having rather columns of type varchar(32).

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,671 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,623 questions
{count} votes

4 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 26,706 Reputation points
    2022-09-20T09:25:22.403+00:00

    Hi @Jan Vávra
    Each row has a null bitmap for columns that allow nulls. If the row in that column is null then a bit in the bitmap is 1 else it's 0.
    For variable size datatypes the acctual size is 0 bytes.
    For fixed size datatype the acctual size is the default datatype size in bytes set to default value (0 for numbers, '' for chars).
    In my opinion, uniqueidentifier is a fixed size datatype.
    Check this sample:

    declare @guid1 uniqueidentifier = null  
           ,@guid2 uniqueidentifier = '728B7419-6E97-4F54-B06B-536B31AE954E'  
    	   ,@guid3 uniqueidentifier = '728B7419-6E97-4F54-B06B-536B31AE954E4578RT'  
    select DATALENGTH(@guid1)  
          ,DATALENGTH(@guid2)  
    	  ,DATALENGTH(@guid3)  
    

    Best regards,
    LiHong

    2 people found this answer helpful.
    0 comments No comments

  2. Erland Sommarskog 110.3K Reputation points
    2022-09-20T21:50:02.65+00:00

    As Li says, each value will take up 16 bytes, no matter if it's NULL or not. However, there is a twist: if you apply row compression, the NULL values will not take up any bytes beside the bit in the NULL bitmap.

    If row compression is not an alternative to you for some reason, varbinary(16) may be a better alternative. I will have to admit that I cannot really decide what I think in this case. I would probably consider the complexity of programming. One thing to keep in mind is that if index these columns and you do:

    DECLARE @v uniqueidentifier  
    ...  
    SELECT ... FROM tbl WHERE indexedbincol = @v  
    

    The rules of type precedence in SQL Server will convert the binary column to uniqueidentifier, which most likely will prevent an Index Seek. (I have not actually tested to verify.) So if you go for varbinary(16), you should so throughout your application.

    Viorel is perfectly right in that varchar(32) is not an alternative.

    1 person found this answer helpful.
    0 comments No comments

  3. Olaf Helper 44,301 Reputation points
    2022-09-20T07:45:02.673+00:00

    Does this answer your question?

    declare @guid1 uniqueidentifier = null, @guid2 uniqueidentifier = newid();  
    select DATALENGTH(@guid1), DATALENGTH(@guid2)  
    

    If a nullable column is null, then SQL Server stores this information in a table bitmask = 1 bit required for NULL values.


  4. Tom Phillips 17,731 Reputation points
    2022-09-21T20:48:06.39+00:00

    Let me ask the question, why are you using uniqueidentifier?

    uniqueidentifier sounds good, but you need to understand it is really for cross server uniqueness. If you don't need that, you are much better off simply using a properly sized numeric and an IDENTITY field.

    See:
    https://www.mssqltips.com/sqlservertip/1600/auto-generated-sql-server-keys-with-the-uniqueidentifier-or-identity/


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.