|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 6:30 AM
Points: 3,192,
Visits: 4,151
|
|
As the column will hold only English language letters and digits, we don't need the additional bytes required by the national character data types Not quite correct. You don't need to use Unicode data types for non-English letters. For example, if the identifiers contain English letters, Russian letters, and digits, you can use Cyrillic collation:
CREATE TABLE #tmp (id CHAR(10) COLLATE Cyrillic_General_CS_AS) INSERT #tmp VALUES ('Привет1') -- Russian word for 'hello' INSERT #tmp VALUES ('Hello2') SELECT * FROM #tmp The result of this query is in the attached picture.
If you need to operate with more than one national alphabet, using of nchar/nvarchar is the only option. Otherwise (only English alphabet, English alphabet + one national alphabet), any of char/varchar/nchar/nvarchar data types can be used.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 1:32 AM
Points: 136,
Visits: 770
|
|
Firstly, we assume that the average is the result of an even spread (that the average length is in fact 8.5 - we only need an average of 8 for storage to be a moot point in char vs varchar). Secondly, within SQL Server, the efficiency may be best to use char in that instance, but take in to account the code operations of trimming and I think it becomes debatable.
I agree with you on a purist level, but practically, I would go with varchar still simply for the programmers' ease.
Nice question
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 5:02 AM
Points: 2,365,
Visits: 1,825
|
|
Nice question, but i agree with diamondgm. The average calculation need not be the same as you pointed out.
"Keep Trying"
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Thursday, October 04, 2012 4:26 AM
Points: 527,
Visits: 86
|
|
| The moment I checked the answer I knew there had to be a catch... While I don't agree on the assumption about distribution of the average I still like the question. More of these please!
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 5:31 AM
Points: 2,226,
Visits: 438
|
|
diamondgm (10/16/2009)
Firstly, we assume that the average is the result of an even spread (that the average length is in fact 8.5 - we only need an average of 8 for storage to be a moot point in char vs varchar). Secondly, within SQL Server, the efficiency may be best to use char in that instance, but take in to account the code operations of trimming and I think it becomes debatable. I agree with you on a purist level, but practically, I would go with varchar still simply for the programmers' ease. Nice question 
I agree with the author of the question when it comes to storage size (and IO performance), but there are more things to think of than storage, like you the trailing spaces that has to be stripped.
I got the question correct because I thought it had a catch, but personally, I would only use char in rare cases unless all the data for the column are equal.
/Håkan Winther MCITP:Database Developer 2008
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:22 AM
Points: 5,244,
Visits: 7,059
|
|
This was a great question. Well done, John! I hope you have more of these in store for us.
diamondgm (10/16/2009) Firstly, we assume that the average is the result of an even spread (that the average length is in fact 8.5 - we only need an average of 8 for storage to be a moot point in char vs varchar).
That's not an assumption, the even spread is explicitly mentioned in the question. Without it, I agree that the question would have been harder (if not impossible) to answer.
Secondly, within SQL Server, the efficiency may be best to use char in that instance, but take in to account the code operations of trimming and I think it becomes debatable.
I doubt it. Almost all business applications I have seen have their performance tied to an I/O bottleneck, and have the CPU twiddling it's virtual thumbs while waiting for more data to be read or written. Those few cycles the CPU spends to trim trailing spaces won't affect performance at all, it just means one less thumb to twiddle.
Oh, and if you really want to compare CPU times, then don't forget that SQL Server has to do extra work for each varying length column (like getting the length, then the data; or comparing length and adding trailing spaces before each comparison). So you'll probably find that the fixed length has lower CPU usage in spite of the extra trimming operations. Not that it will affect overall performance, though.
I agree with you on a purist level, but practically, I would go with varchar still simply for the programmers' ease.
How, exactly, is varchar easier for a programmer?
hakan.winther (10/16/2009) personally, I would only use char in rare cases unless all the data for the column are equal.
That's reallly a shame, as the only good reasons to use varchar instead of char are a difference of more than 2 between average and maximum length; or a need to preserve trailing spaces.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:46 AM
Points: 384,
Visits: 186
|
|
There is one more interesting thing. Fixed length columns are placed in the same place in the row, whilst all columns like varchar, nvarchar etc are at the end of the row and their position may vary. From the performance point of view it is better to have information stored in fixed length columns as they are on the same place in the row. Especially if you have 8,5 byte of total 10 used (~ lets agree with this assumption). When you try to find data from nvarchar, nchar etc you have to read extra two bytes (mentioned in the explanation) to know where the data is (looking for the offset in teh row) in the row.
Kindest Regards,
Damian Widera SQL Server MVP, MCT, MCITP-DBA, MCSD.NET
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Yesterday @ 8:01 PM
Points: 408,
Visits: 688
|
|
Good question, made me think about datatypes.
nVarchar can actually use more than 2 bytes for some characters e.g. chinese characters that use 3 bytes.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:22 AM
Points: 5,244,
Visits: 7,059
|
|
david.howell (10/16/2009) nVarchar can actually use more than 2 bytes for some characters e.g. chinese characters that use 3 bytes.
Do you have a source for that? Both Books Online and every website I've ever read say that nvarchar and nchar use 2 bytes per character, never less, never more.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|