Disk space when varchar to nvarchar change

  • How do write a query to figure how much more disk space i will need when i change all my column from varchar to nvarchar.

    As part of new change in my application, we require to change all varchar to nvarchar and before we go ahead we need to know abt disk space.

    We have abt 400 tables in the db and almost all the tables have either one or many varchar columns.

  • I don't have a query for this but in normal circumstances nvarchar takes double the size as varchar.

    Storage Size

    Varchar - Actual Length (in bytes)

    NVarchar - 2 times Actual Length (in bytes)

    According to this you can have an assumption of the increase in Storage space.

    Hope this helps.

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

  • Yeah, but you can simply multiply varchar column sizes by two, as it will give you much larger number than it will really take.

    Saying that current column varchar(100), changing it to nvarchar(100) will not guarantee that you will need extra 100 bytes straight away, it will depend on the size of data you have in it.

    But, for simplicity you can do just that:

    select sum(sc.max_length) as current_max_length_in_varchar

    ,sum(sc.max_length)*2 as required_max_length_in_nvarchar

    from sys.tables st

    join sys.columns sc

    on sc.[object_id] = st.[object_id]

    where st.type = 'U'

    and sc.system_type_id = 167 -- varchar

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • lol....that's a really good way of doing it.

    Nice 1 Eugine.

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

  • Yea.. i know

    nvarchar = 2(varchar)+2

    But i was mainly looking for query.. since I have abt 800 tables and each table has 3/4 varchar columns, with each row count abt 10000.

    So to traverse thru them is a big task

    Was thinking of picking varchar column from each table and then applying formula (but too tedious and lengthy)

    eg. table has 3 varchar column of length 3,4,6 each and row count is 20

    so total space needed is = 2(3)+ 2(20); 2(4) + 2(20) ; 2(6)+20 ..

    Hope i'm right

  • khushbu (4/24/2012)


    Yea.. i know

    nvarchar = 2(varchar)+2

    where +2 coming from?

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (4/24/2012)


    khushbu (4/24/2012)


    Yea.. i know

    nvarchar = 2(varchar)+2

    where +2 coming from?

    From BOL: http://msdn.microsoft.com/en-us/library/ms186939.aspx

    nvarchar [ ( n | max ) ]

    Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes.

  • khushbu (4/24/2012)


    Eugene Elutin (4/24/2012)


    khushbu (4/24/2012)


    Yea.. i know

    nvarchar = 2(varchar)+2

    where +2 coming from?

    From BOL: http://msdn.microsoft.com/en-us/library/ms186939.aspx

    nvarchar [ ( n | max ) ]

    Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes.

    That's right: the actual length of data entered + 2 bytes

    It's not 2(varchar)+2!

    As varchar also takes: "the actual length of data entered + 2 bytes" :

    http://msdn.microsoft.com/en-us/library/ms176089.aspx

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (4/24/2012)


    khushbu (4/24/2012)


    Eugene Elutin (4/24/2012)


    khushbu (4/24/2012)


    Yea.. i know

    nvarchar = 2(varchar)+2

    where +2 coming from?

    From BOL: http://msdn.microsoft.com/en-us/library/ms186939.aspx

    nvarchar [ ( n | max ) ]

    Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes.

    That's right: the actual length of data entered + 2 bytes

    It's not 2(varchar)+2!

    As varchar also takes: "the actual length of data entered + 2 bytes" :

    http://msdn.microsoft.com/en-us/library/ms176089.aspx

    Okies got it, so its always 2 times the stored data.

    However my earlier query still holds true , when removing +2 (no of rows) part?

  • Eugene Elutin (4/24/2012)


    nvarchar [ ( n | max ) ]

    Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes.

    That's right: the actual length of data entered + 2 bytes

    It's not 2(varchar)+2!

    As varchar also takes: "the actual length of data entered + 2 bytes" :

    http://msdn.microsoft.com/en-us/library/ms176089.aspx

    The quoted BoL extract states "The storage size, in bytes, is two times the actual length of data entered + 2 bytes."

    Varchar storage size = 2 bytes + length of data stored

    NVarchar storage size = 2 bytes + 2*(length of data stored) because with nvarchar each character takes 2 bytes.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • A quick way to do this might be to calculate the average row length (via sys.dm_db_index_physical_stats) and subtract the length of any fixed lenght columns within the tables (like int, datetime, decimal, etc). That will give you the average length of varchar columns per table. You can roughly double that to get the average length of nvarchar columns. That'll tell you at a minimum how much extra space you need for the tables.

    It's not completely accurate, you will need to take indexes into account as well, but it's a fairly easy rough figure.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • How about using DATALENGTH?

    Do you think it should be more good than stats DMV?

    declare @v1 varchar(10),@v2 nvarchar (10)

    set @v1= '885'

    set @v2= '885'

    select DATALENGTH (@v1) as varchar1

    select DATALENGTH (@v2) as nvarchar1

    select len (@v1) as varchar1

    select len (@v2) as nvarchar1

  • All great information ...

    Just do not forget about any indexes that have varchar columns as well !

    RegardsRudy KomacsarSenior Database Administrator"Ave Caesar! - Morituri te salutamus."

  • Yea.. Indexes is something my next problem is now..

    There are few columns that are part of index, how can i calculate (even approx) space for them?

  • If you use this method, you can calculate nonclustered indexes as well, much the same way.

    GilaMonster (4/24/2012)


    A quick way to do this might be to calculate the average row length (via sys.dm_db_index_physical_stats) and subtract the length of any fixed lenght columns within the tables (like int, datetime, decimal, etc). That will give you the average length of varchar columns per table. You can roughly double that to get the average length of nvarchar columns. That'll tell you at a minimum how much extra space you need for the tables.

    It's not completely accurate, you will need to take indexes into account as well, but it's a fairly easy rough figure.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic. Login to reply