Using column size much larger than necessarySQL Server Database Size much higher than the backup size after restore“Variant” values structure and their use for multiple columnsIndex not being used in SELECT queryWhy the backup file size is much larger than the restored databaseSpeeding up insert in SQL ServerInteger ID vs varchar ID with JOINbackup log larger than log fileSQL Server 2008 R2 differential backups much larger than expectedStoring NULL versus storing '' in a varchar columnTSQL - create a function that takes a multi-row query result as an argument

How Car Rear View Mirrors Work

How does Howard Stark know this?

about the word 地図

How to select certain lines (n, n+4, n+8, n+12...) from the file?

We are two immediate neighbors who forged our own powers to form concatenated relationship. Who are we?

Does Lawful Interception of 4G / the proposed 5G provide a back door for hackers as well?

How are Core iX names like Core i5, i7 related to Haswell, Ivy Bridge?

How can I make a vertical rule that extends to the edges of an fbox?

Remove everything except csv file Bash Script

What are the ramifications of setting ARITHABORT ON for all connections in SQL Server?

How could we transfer large amounts of energy sourced in space to Earth?

Can you book a one-way ticket to the UK on a visa?

How to pronounce "r" after a "g"?

What can cause a never-frozen indoor copper drain pipe to crack?

Was this a power play by Daenerys?

How to deal with inappropriate comments during interviews for academic positions?

How to slow yourself down (for playing nice with others)

How did Thanos not realise this had happened at the end of Endgame?

Thesis' "Future Work" section – is it acceptable to omit personal involvement in a mentioned project?

How can this pool heater gas line be disconnected?

Why does a C.D.F need to be right-continuous?

International Code of Ethics for order of co-authors in research papers

Why can't RGB or bicolour LEDs produce a decent yellow?

What does it mean with the ask price is below the last price?



Using column size much larger than necessary


SQL Server Database Size much higher than the backup size after restore“Variant” values structure and their use for multiple columnsIndex not being used in SELECT queryWhy the backup file size is much larger than the restored databaseSpeeding up insert in SQL ServerInteger ID vs varchar ID with JOINbackup log larger than log fileSQL Server 2008 R2 differential backups much larger than expectedStoring NULL versus storing '' in a varchar columnTSQL - create a function that takes a multi-row query result as an argument






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








15















I'm creating an SQL Server database with someone else. One of the tables is small (6 rows) with data that will probably remain constant. There is a remote possibility that a new row will be added. The table looks something like this:



CREATE TABLE someTable (
id int primary key identity(1,1) not null,
name varchar(128) not null unique
);
INSERT INTO someTable values ('alice', 'bob something', 'charles can dance', 'dugan was here');


I'm looking at the char length of that name column, and I think that its values are probably never going to be larger than, say, 32 characters, maybe not even larger than 24. Is there any advantage to my changing this column to, for example, varchar(32)?



Also, is there any advantage to keeping default column sizes to multiples of 4, 8, 32, etc?










share|improve this question



















  • 3





    Yes make fields with reasonable size, but that’s more of a problem for 2000 vs. 100 and not 128 vs. 32. Having said that there are naming standards which recommend (for person names) 35+35 or even 100 chars. 128 does not look far off.

    – eckes
    May 1 at 16:09







  • 3





    Might be worth noting that 128 is kind of a special number in SQL Server because that's the max length of certain object names. So if you're storing object names in this table, it would make sense to use exactly 128. Don't ask me how I thought of this >.>

    – Jacob H
    May 1 at 17:03












  • @JacobH storing object names is what sysname is for.

    – AakashM
    May 2 at 13:55






  • 2





    Please ignore the downvote. You've asked a good question, I think someone is just unhappy about the result and is taking it out on everyone.

    – Aaron Bertrand
    May 2 at 15:32







  • 2





    @Joe The defined size (and not the actual populated size) dictates memory grants, which can definitely become a performance/concurrency issue.

    – Aaron Bertrand
    May 2 at 15:51

















15















I'm creating an SQL Server database with someone else. One of the tables is small (6 rows) with data that will probably remain constant. There is a remote possibility that a new row will be added. The table looks something like this:



CREATE TABLE someTable (
id int primary key identity(1,1) not null,
name varchar(128) not null unique
);
INSERT INTO someTable values ('alice', 'bob something', 'charles can dance', 'dugan was here');


I'm looking at the char length of that name column, and I think that its values are probably never going to be larger than, say, 32 characters, maybe not even larger than 24. Is there any advantage to my changing this column to, for example, varchar(32)?



Also, is there any advantage to keeping default column sizes to multiples of 4, 8, 32, etc?










share|improve this question



















  • 3





    Yes make fields with reasonable size, but that’s more of a problem for 2000 vs. 100 and not 128 vs. 32. Having said that there are naming standards which recommend (for person names) 35+35 or even 100 chars. 128 does not look far off.

    – eckes
    May 1 at 16:09







  • 3





    Might be worth noting that 128 is kind of a special number in SQL Server because that's the max length of certain object names. So if you're storing object names in this table, it would make sense to use exactly 128. Don't ask me how I thought of this >.>

    – Jacob H
    May 1 at 17:03












  • @JacobH storing object names is what sysname is for.

    – AakashM
    May 2 at 13:55






  • 2





    Please ignore the downvote. You've asked a good question, I think someone is just unhappy about the result and is taking it out on everyone.

    – Aaron Bertrand
    May 2 at 15:32







  • 2





    @Joe The defined size (and not the actual populated size) dictates memory grants, which can definitely become a performance/concurrency issue.

    – Aaron Bertrand
    May 2 at 15:51













15












15








15








I'm creating an SQL Server database with someone else. One of the tables is small (6 rows) with data that will probably remain constant. There is a remote possibility that a new row will be added. The table looks something like this:



CREATE TABLE someTable (
id int primary key identity(1,1) not null,
name varchar(128) not null unique
);
INSERT INTO someTable values ('alice', 'bob something', 'charles can dance', 'dugan was here');


I'm looking at the char length of that name column, and I think that its values are probably never going to be larger than, say, 32 characters, maybe not even larger than 24. Is there any advantage to my changing this column to, for example, varchar(32)?



Also, is there any advantage to keeping default column sizes to multiples of 4, 8, 32, etc?










share|improve this question
















I'm creating an SQL Server database with someone else. One of the tables is small (6 rows) with data that will probably remain constant. There is a remote possibility that a new row will be added. The table looks something like this:



CREATE TABLE someTable (
id int primary key identity(1,1) not null,
name varchar(128) not null unique
);
INSERT INTO someTable values ('alice', 'bob something', 'charles can dance', 'dugan was here');


I'm looking at the char length of that name column, and I think that its values are probably never going to be larger than, say, 32 characters, maybe not even larger than 24. Is there any advantage to my changing this column to, for example, varchar(32)?



Also, is there any advantage to keeping default column sizes to multiples of 4, 8, 32, etc?







sql-server database-design varchar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 3 at 14:36







elbillaf

















asked May 1 at 14:05









elbillafelbillaf

1817




1817







  • 3





    Yes make fields with reasonable size, but that’s more of a problem for 2000 vs. 100 and not 128 vs. 32. Having said that there are naming standards which recommend (for person names) 35+35 or even 100 chars. 128 does not look far off.

    – eckes
    May 1 at 16:09







  • 3





    Might be worth noting that 128 is kind of a special number in SQL Server because that's the max length of certain object names. So if you're storing object names in this table, it would make sense to use exactly 128. Don't ask me how I thought of this >.>

    – Jacob H
    May 1 at 17:03












  • @JacobH storing object names is what sysname is for.

    – AakashM
    May 2 at 13:55






  • 2





    Please ignore the downvote. You've asked a good question, I think someone is just unhappy about the result and is taking it out on everyone.

    – Aaron Bertrand
    May 2 at 15:32







  • 2





    @Joe The defined size (and not the actual populated size) dictates memory grants, which can definitely become a performance/concurrency issue.

    – Aaron Bertrand
    May 2 at 15:51












  • 3





    Yes make fields with reasonable size, but that’s more of a problem for 2000 vs. 100 and not 128 vs. 32. Having said that there are naming standards which recommend (for person names) 35+35 or even 100 chars. 128 does not look far off.

    – eckes
    May 1 at 16:09







  • 3





    Might be worth noting that 128 is kind of a special number in SQL Server because that's the max length of certain object names. So if you're storing object names in this table, it would make sense to use exactly 128. Don't ask me how I thought of this >.>

    – Jacob H
    May 1 at 17:03












  • @JacobH storing object names is what sysname is for.

    – AakashM
    May 2 at 13:55






  • 2





    Please ignore the downvote. You've asked a good question, I think someone is just unhappy about the result and is taking it out on everyone.

    – Aaron Bertrand
    May 2 at 15:32







  • 2





    @Joe The defined size (and not the actual populated size) dictates memory grants, which can definitely become a performance/concurrency issue.

    – Aaron Bertrand
    May 2 at 15:51







3




3





Yes make fields with reasonable size, but that’s more of a problem for 2000 vs. 100 and not 128 vs. 32. Having said that there are naming standards which recommend (for person names) 35+35 or even 100 chars. 128 does not look far off.

– eckes
May 1 at 16:09






Yes make fields with reasonable size, but that’s more of a problem for 2000 vs. 100 and not 128 vs. 32. Having said that there are naming standards which recommend (for person names) 35+35 or even 100 chars. 128 does not look far off.

– eckes
May 1 at 16:09





3




3





Might be worth noting that 128 is kind of a special number in SQL Server because that's the max length of certain object names. So if you're storing object names in this table, it would make sense to use exactly 128. Don't ask me how I thought of this >.>

– Jacob H
May 1 at 17:03






Might be worth noting that 128 is kind of a special number in SQL Server because that's the max length of certain object names. So if you're storing object names in this table, it would make sense to use exactly 128. Don't ask me how I thought of this >.>

– Jacob H
May 1 at 17:03














@JacobH storing object names is what sysname is for.

– AakashM
May 2 at 13:55





@JacobH storing object names is what sysname is for.

– AakashM
May 2 at 13:55




2




2





Please ignore the downvote. You've asked a good question, I think someone is just unhappy about the result and is taking it out on everyone.

– Aaron Bertrand
May 2 at 15:32






Please ignore the downvote. You've asked a good question, I think someone is just unhappy about the result and is taking it out on everyone.

– Aaron Bertrand
May 2 at 15:32





2




2





@Joe The defined size (and not the actual populated size) dictates memory grants, which can definitely become a performance/concurrency issue.

– Aaron Bertrand
May 2 at 15:51





@Joe The defined size (and not the actual populated size) dictates memory grants, which can definitely become a performance/concurrency issue.

– Aaron Bertrand
May 2 at 15:51










2 Answers
2






active

oldest

votes


















15














SQL Server uses column lengths when allocating memory for query processing. So, yes, in short, you should always size columns appropriately for the data.



Memory allocations are based on the number of rows returned by the query multiplied by half the declared length of the column.



Having said that, in this case where you've got 6 rows you probably don't want to over optimize prematurely. Unless you JOIN this table to another with millions of rows, there won't be a massive difference between a varchar(24) and a varchar(32), or even a varchar(128).



Your second question asks about aligning column lengths on binary multiples. That's not required at all since SQL Server stores all data in 8KB pages, regardless of the length of each column.






share|improve this answer


















  • 1





    I have found no performance difference when using Varchar(8000) vs. Varchar(20). This is on joins to multiple tables with rowcounts on the largest tables being 500 million and 100 million. Total size of the two tables is 300GB. Of course my join columns are kept short and almost always int or bigint.

    – Joe
    May 2 at 15:46






  • 2





    @Joe Just because you haven't observed performance differences doesn't mean they can't exist. This sounds like the Bear Patrol.

    – Aaron Bertrand
    May 2 at 15:54







  • 1





    @joe - fantastic. You'll keep people like me employed with lots of performance tuning work.

    – Max Vernon
    May 2 at 15:56







  • 1





    @AaronBertrand, thanks I learned something new today. In future scenarios I might need to reconsider. Either the hardware I work on or the specific use cases haven't shown an impact to date but I see from this article that it might be a factor in the future. sqlperformance.com/2017/06/sql-plan/…

    – Joe
    May 2 at 16:27







  • 3





    @Joe nothing is ever a performance issue until it is. I find it is very hard to find cases where testing an app has fully reflected the same scale and concurrency the app eventually hits in production ("worked on my machine" / "worked in dev"), and it's at those levels of scale and concurrency where this specific issue becomes a problem. Your scenario, even though you've tested it, will have a breaking point. You may be lucky enough to never hit it, but again, that doesn't mean it can't be hit. Tell me where your app is and I'll give it a shot. :-)

    – Aaron Bertrand
    May 2 at 16:41



















14














With 6 rows, no, there will be no observable benefit. That entire table will fit on a single page so lowering the maximum potential space you’ll use on that page while still occupying that entire page is really no different in all practical sense.



On larger tables, though, right-sizing is crucial. The reason is that memory estimates will be based on the assumption that every value will be 50% populated. So if you have varchar(128), every value will expect to occupy 64 bytes, regardless of the actual data, therefore memory grants will be 64b * number of rows. If all the values will be 32 characters or less, making it a varchar(64) or even varchar(32) is probably a better choice. If a large percentage of values are close to or at the cap, you could even argue for char to take volatility out of it.



As for benefits of having string lengths capped at powers of 2, I don’t think on today’s hardware anyone could demonstrate any obvious advantages.






share|improve this answer


















  • 2





    @jpmc26 -1 all you like. Sometimes char is a valid choice, and sometimes what a developer has to learn how to handle (or that you have to obscure from them) isn't top priority. Anyway if you read closely my answer wasn't "use char" it was mentioned as an aside with a very specific use case as a disclaimer.

    – Aaron Bertrand
    May 2 at 13:39







  • 4





    @jpmc26 When was the last time you had the freedom to just add RAM to a production server that was wasting memory? I would -1 anywhere where you said the best approach is always to just throw hardware at something.

    – Aaron Bertrand
    May 2 at 13:41






  • 2





    @jpmc26 Agree to disagree. Adding hardware might sound like the simpler thing in some cases, but adding memory isn't always possible. It's not always your hardware. There aren't always free RAM slots. Sometimes you're already using the highest capacity chips available. I think in theory that argument works but not always in practice. Just like fragmentation, wasting memory has downstream effects too (think readable secondaries that are less equipped). And just to re-state one more time: I am not advocating char here, just mentioning it as a possibility in very specific cases.

    – Aaron Bertrand
    May 2 at 13:55







  • 2





    @jpmc26 I also don't believe that changing a varchar column to char(32) because it always or almost always contains all 32 characters is either illogical or requires extra work. Tell me all about the extra work a developer has to perform, or bugs that might be created, if they are dealing with char instead of varchar. Maybe we should petition Microsoft to remove the char type if it is so useless and dangerous?

    – Aaron Bertrand
    May 2 at 15:23






  • 2





    @jpmc26 an app that is writing data doesn't have to trim anything. An app that is displaying data also doesn't have to trim anything. An app that is comparing data shouldn't have to worry about trailing spaces unless it is building literals inside of ad hoc queries. All of those cases are moot anyway if we're talking about character data that uses the entire column width. I repeated it twice already but, once more, this is not my first string pick, it is a use case that may be appropriate sometimes. Stop talking to me like I told everyone they all have to abandon varchar tomorrow.

    – Aaron Bertrand
    May 2 at 20:33











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f237128%2fusing-column-size-much-larger-than-necessary%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









15














SQL Server uses column lengths when allocating memory for query processing. So, yes, in short, you should always size columns appropriately for the data.



Memory allocations are based on the number of rows returned by the query multiplied by half the declared length of the column.



Having said that, in this case where you've got 6 rows you probably don't want to over optimize prematurely. Unless you JOIN this table to another with millions of rows, there won't be a massive difference between a varchar(24) and a varchar(32), or even a varchar(128).



Your second question asks about aligning column lengths on binary multiples. That's not required at all since SQL Server stores all data in 8KB pages, regardless of the length of each column.






share|improve this answer


















  • 1





    I have found no performance difference when using Varchar(8000) vs. Varchar(20). This is on joins to multiple tables with rowcounts on the largest tables being 500 million and 100 million. Total size of the two tables is 300GB. Of course my join columns are kept short and almost always int or bigint.

    – Joe
    May 2 at 15:46






  • 2





    @Joe Just because you haven't observed performance differences doesn't mean they can't exist. This sounds like the Bear Patrol.

    – Aaron Bertrand
    May 2 at 15:54







  • 1





    @joe - fantastic. You'll keep people like me employed with lots of performance tuning work.

    – Max Vernon
    May 2 at 15:56







  • 1





    @AaronBertrand, thanks I learned something new today. In future scenarios I might need to reconsider. Either the hardware I work on or the specific use cases haven't shown an impact to date but I see from this article that it might be a factor in the future. sqlperformance.com/2017/06/sql-plan/…

    – Joe
    May 2 at 16:27







  • 3





    @Joe nothing is ever a performance issue until it is. I find it is very hard to find cases where testing an app has fully reflected the same scale and concurrency the app eventually hits in production ("worked on my machine" / "worked in dev"), and it's at those levels of scale and concurrency where this specific issue becomes a problem. Your scenario, even though you've tested it, will have a breaking point. You may be lucky enough to never hit it, but again, that doesn't mean it can't be hit. Tell me where your app is and I'll give it a shot. :-)

    – Aaron Bertrand
    May 2 at 16:41
















15














SQL Server uses column lengths when allocating memory for query processing. So, yes, in short, you should always size columns appropriately for the data.



Memory allocations are based on the number of rows returned by the query multiplied by half the declared length of the column.



Having said that, in this case where you've got 6 rows you probably don't want to over optimize prematurely. Unless you JOIN this table to another with millions of rows, there won't be a massive difference between a varchar(24) and a varchar(32), or even a varchar(128).



Your second question asks about aligning column lengths on binary multiples. That's not required at all since SQL Server stores all data in 8KB pages, regardless of the length of each column.






share|improve this answer


















  • 1





    I have found no performance difference when using Varchar(8000) vs. Varchar(20). This is on joins to multiple tables with rowcounts on the largest tables being 500 million and 100 million. Total size of the two tables is 300GB. Of course my join columns are kept short and almost always int or bigint.

    – Joe
    May 2 at 15:46






  • 2





    @Joe Just because you haven't observed performance differences doesn't mean they can't exist. This sounds like the Bear Patrol.

    – Aaron Bertrand
    May 2 at 15:54







  • 1





    @joe - fantastic. You'll keep people like me employed with lots of performance tuning work.

    – Max Vernon
    May 2 at 15:56







  • 1





    @AaronBertrand, thanks I learned something new today. In future scenarios I might need to reconsider. Either the hardware I work on or the specific use cases haven't shown an impact to date but I see from this article that it might be a factor in the future. sqlperformance.com/2017/06/sql-plan/…

    – Joe
    May 2 at 16:27







  • 3





    @Joe nothing is ever a performance issue until it is. I find it is very hard to find cases where testing an app has fully reflected the same scale and concurrency the app eventually hits in production ("worked on my machine" / "worked in dev"), and it's at those levels of scale and concurrency where this specific issue becomes a problem. Your scenario, even though you've tested it, will have a breaking point. You may be lucky enough to never hit it, but again, that doesn't mean it can't be hit. Tell me where your app is and I'll give it a shot. :-)

    – Aaron Bertrand
    May 2 at 16:41














15












15








15







SQL Server uses column lengths when allocating memory for query processing. So, yes, in short, you should always size columns appropriately for the data.



Memory allocations are based on the number of rows returned by the query multiplied by half the declared length of the column.



Having said that, in this case where you've got 6 rows you probably don't want to over optimize prematurely. Unless you JOIN this table to another with millions of rows, there won't be a massive difference between a varchar(24) and a varchar(32), or even a varchar(128).



Your second question asks about aligning column lengths on binary multiples. That's not required at all since SQL Server stores all data in 8KB pages, regardless of the length of each column.






share|improve this answer













SQL Server uses column lengths when allocating memory for query processing. So, yes, in short, you should always size columns appropriately for the data.



Memory allocations are based on the number of rows returned by the query multiplied by half the declared length of the column.



Having said that, in this case where you've got 6 rows you probably don't want to over optimize prematurely. Unless you JOIN this table to another with millions of rows, there won't be a massive difference between a varchar(24) and a varchar(32), or even a varchar(128).



Your second question asks about aligning column lengths on binary multiples. That's not required at all since SQL Server stores all data in 8KB pages, regardless of the length of each column.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 1 at 14:21









Max VernonMax Vernon

53.2k13116234




53.2k13116234







  • 1





    I have found no performance difference when using Varchar(8000) vs. Varchar(20). This is on joins to multiple tables with rowcounts on the largest tables being 500 million and 100 million. Total size of the two tables is 300GB. Of course my join columns are kept short and almost always int or bigint.

    – Joe
    May 2 at 15:46






  • 2





    @Joe Just because you haven't observed performance differences doesn't mean they can't exist. This sounds like the Bear Patrol.

    – Aaron Bertrand
    May 2 at 15:54







  • 1





    @joe - fantastic. You'll keep people like me employed with lots of performance tuning work.

    – Max Vernon
    May 2 at 15:56







  • 1





    @AaronBertrand, thanks I learned something new today. In future scenarios I might need to reconsider. Either the hardware I work on or the specific use cases haven't shown an impact to date but I see from this article that it might be a factor in the future. sqlperformance.com/2017/06/sql-plan/…

    – Joe
    May 2 at 16:27







  • 3





    @Joe nothing is ever a performance issue until it is. I find it is very hard to find cases where testing an app has fully reflected the same scale and concurrency the app eventually hits in production ("worked on my machine" / "worked in dev"), and it's at those levels of scale and concurrency where this specific issue becomes a problem. Your scenario, even though you've tested it, will have a breaking point. You may be lucky enough to never hit it, but again, that doesn't mean it can't be hit. Tell me where your app is and I'll give it a shot. :-)

    – Aaron Bertrand
    May 2 at 16:41













  • 1





    I have found no performance difference when using Varchar(8000) vs. Varchar(20). This is on joins to multiple tables with rowcounts on the largest tables being 500 million and 100 million. Total size of the two tables is 300GB. Of course my join columns are kept short and almost always int or bigint.

    – Joe
    May 2 at 15:46






  • 2





    @Joe Just because you haven't observed performance differences doesn't mean they can't exist. This sounds like the Bear Patrol.

    – Aaron Bertrand
    May 2 at 15:54







  • 1





    @joe - fantastic. You'll keep people like me employed with lots of performance tuning work.

    – Max Vernon
    May 2 at 15:56







  • 1





    @AaronBertrand, thanks I learned something new today. In future scenarios I might need to reconsider. Either the hardware I work on or the specific use cases haven't shown an impact to date but I see from this article that it might be a factor in the future. sqlperformance.com/2017/06/sql-plan/…

    – Joe
    May 2 at 16:27







  • 3





    @Joe nothing is ever a performance issue until it is. I find it is very hard to find cases where testing an app has fully reflected the same scale and concurrency the app eventually hits in production ("worked on my machine" / "worked in dev"), and it's at those levels of scale and concurrency where this specific issue becomes a problem. Your scenario, even though you've tested it, will have a breaking point. You may be lucky enough to never hit it, but again, that doesn't mean it can't be hit. Tell me where your app is and I'll give it a shot. :-)

    – Aaron Bertrand
    May 2 at 16:41








1




1





I have found no performance difference when using Varchar(8000) vs. Varchar(20). This is on joins to multiple tables with rowcounts on the largest tables being 500 million and 100 million. Total size of the two tables is 300GB. Of course my join columns are kept short and almost always int or bigint.

– Joe
May 2 at 15:46





I have found no performance difference when using Varchar(8000) vs. Varchar(20). This is on joins to multiple tables with rowcounts on the largest tables being 500 million and 100 million. Total size of the two tables is 300GB. Of course my join columns are kept short and almost always int or bigint.

– Joe
May 2 at 15:46




2




2





@Joe Just because you haven't observed performance differences doesn't mean they can't exist. This sounds like the Bear Patrol.

– Aaron Bertrand
May 2 at 15:54






@Joe Just because you haven't observed performance differences doesn't mean they can't exist. This sounds like the Bear Patrol.

– Aaron Bertrand
May 2 at 15:54





1




1





@joe - fantastic. You'll keep people like me employed with lots of performance tuning work.

– Max Vernon
May 2 at 15:56






@joe - fantastic. You'll keep people like me employed with lots of performance tuning work.

– Max Vernon
May 2 at 15:56





1




1





@AaronBertrand, thanks I learned something new today. In future scenarios I might need to reconsider. Either the hardware I work on or the specific use cases haven't shown an impact to date but I see from this article that it might be a factor in the future. sqlperformance.com/2017/06/sql-plan/…

– Joe
May 2 at 16:27






@AaronBertrand, thanks I learned something new today. In future scenarios I might need to reconsider. Either the hardware I work on or the specific use cases haven't shown an impact to date but I see from this article that it might be a factor in the future. sqlperformance.com/2017/06/sql-plan/…

– Joe
May 2 at 16:27





3




3





@Joe nothing is ever a performance issue until it is. I find it is very hard to find cases where testing an app has fully reflected the same scale and concurrency the app eventually hits in production ("worked on my machine" / "worked in dev"), and it's at those levels of scale and concurrency where this specific issue becomes a problem. Your scenario, even though you've tested it, will have a breaking point. You may be lucky enough to never hit it, but again, that doesn't mean it can't be hit. Tell me where your app is and I'll give it a shot. :-)

– Aaron Bertrand
May 2 at 16:41






@Joe nothing is ever a performance issue until it is. I find it is very hard to find cases where testing an app has fully reflected the same scale and concurrency the app eventually hits in production ("worked on my machine" / "worked in dev"), and it's at those levels of scale and concurrency where this specific issue becomes a problem. Your scenario, even though you've tested it, will have a breaking point. You may be lucky enough to never hit it, but again, that doesn't mean it can't be hit. Tell me where your app is and I'll give it a shot. :-)

– Aaron Bertrand
May 2 at 16:41














14














With 6 rows, no, there will be no observable benefit. That entire table will fit on a single page so lowering the maximum potential space you’ll use on that page while still occupying that entire page is really no different in all practical sense.



On larger tables, though, right-sizing is crucial. The reason is that memory estimates will be based on the assumption that every value will be 50% populated. So if you have varchar(128), every value will expect to occupy 64 bytes, regardless of the actual data, therefore memory grants will be 64b * number of rows. If all the values will be 32 characters or less, making it a varchar(64) or even varchar(32) is probably a better choice. If a large percentage of values are close to or at the cap, you could even argue for char to take volatility out of it.



As for benefits of having string lengths capped at powers of 2, I don’t think on today’s hardware anyone could demonstrate any obvious advantages.






share|improve this answer


















  • 2





    @jpmc26 -1 all you like. Sometimes char is a valid choice, and sometimes what a developer has to learn how to handle (or that you have to obscure from them) isn't top priority. Anyway if you read closely my answer wasn't "use char" it was mentioned as an aside with a very specific use case as a disclaimer.

    – Aaron Bertrand
    May 2 at 13:39







  • 4





    @jpmc26 When was the last time you had the freedom to just add RAM to a production server that was wasting memory? I would -1 anywhere where you said the best approach is always to just throw hardware at something.

    – Aaron Bertrand
    May 2 at 13:41






  • 2





    @jpmc26 Agree to disagree. Adding hardware might sound like the simpler thing in some cases, but adding memory isn't always possible. It's not always your hardware. There aren't always free RAM slots. Sometimes you're already using the highest capacity chips available. I think in theory that argument works but not always in practice. Just like fragmentation, wasting memory has downstream effects too (think readable secondaries that are less equipped). And just to re-state one more time: I am not advocating char here, just mentioning it as a possibility in very specific cases.

    – Aaron Bertrand
    May 2 at 13:55







  • 2





    @jpmc26 I also don't believe that changing a varchar column to char(32) because it always or almost always contains all 32 characters is either illogical or requires extra work. Tell me all about the extra work a developer has to perform, or bugs that might be created, if they are dealing with char instead of varchar. Maybe we should petition Microsoft to remove the char type if it is so useless and dangerous?

    – Aaron Bertrand
    May 2 at 15:23






  • 2





    @jpmc26 an app that is writing data doesn't have to trim anything. An app that is displaying data also doesn't have to trim anything. An app that is comparing data shouldn't have to worry about trailing spaces unless it is building literals inside of ad hoc queries. All of those cases are moot anyway if we're talking about character data that uses the entire column width. I repeated it twice already but, once more, this is not my first string pick, it is a use case that may be appropriate sometimes. Stop talking to me like I told everyone they all have to abandon varchar tomorrow.

    – Aaron Bertrand
    May 2 at 20:33















14














With 6 rows, no, there will be no observable benefit. That entire table will fit on a single page so lowering the maximum potential space you’ll use on that page while still occupying that entire page is really no different in all practical sense.



On larger tables, though, right-sizing is crucial. The reason is that memory estimates will be based on the assumption that every value will be 50% populated. So if you have varchar(128), every value will expect to occupy 64 bytes, regardless of the actual data, therefore memory grants will be 64b * number of rows. If all the values will be 32 characters or less, making it a varchar(64) or even varchar(32) is probably a better choice. If a large percentage of values are close to or at the cap, you could even argue for char to take volatility out of it.



As for benefits of having string lengths capped at powers of 2, I don’t think on today’s hardware anyone could demonstrate any obvious advantages.






share|improve this answer


















  • 2





    @jpmc26 -1 all you like. Sometimes char is a valid choice, and sometimes what a developer has to learn how to handle (or that you have to obscure from them) isn't top priority. Anyway if you read closely my answer wasn't "use char" it was mentioned as an aside with a very specific use case as a disclaimer.

    – Aaron Bertrand
    May 2 at 13:39







  • 4





    @jpmc26 When was the last time you had the freedom to just add RAM to a production server that was wasting memory? I would -1 anywhere where you said the best approach is always to just throw hardware at something.

    – Aaron Bertrand
    May 2 at 13:41






  • 2





    @jpmc26 Agree to disagree. Adding hardware might sound like the simpler thing in some cases, but adding memory isn't always possible. It's not always your hardware. There aren't always free RAM slots. Sometimes you're already using the highest capacity chips available. I think in theory that argument works but not always in practice. Just like fragmentation, wasting memory has downstream effects too (think readable secondaries that are less equipped). And just to re-state one more time: I am not advocating char here, just mentioning it as a possibility in very specific cases.

    – Aaron Bertrand
    May 2 at 13:55







  • 2





    @jpmc26 I also don't believe that changing a varchar column to char(32) because it always or almost always contains all 32 characters is either illogical or requires extra work. Tell me all about the extra work a developer has to perform, or bugs that might be created, if they are dealing with char instead of varchar. Maybe we should petition Microsoft to remove the char type if it is so useless and dangerous?

    – Aaron Bertrand
    May 2 at 15:23






  • 2





    @jpmc26 an app that is writing data doesn't have to trim anything. An app that is displaying data also doesn't have to trim anything. An app that is comparing data shouldn't have to worry about trailing spaces unless it is building literals inside of ad hoc queries. All of those cases are moot anyway if we're talking about character data that uses the entire column width. I repeated it twice already but, once more, this is not my first string pick, it is a use case that may be appropriate sometimes. Stop talking to me like I told everyone they all have to abandon varchar tomorrow.

    – Aaron Bertrand
    May 2 at 20:33













14












14








14







With 6 rows, no, there will be no observable benefit. That entire table will fit on a single page so lowering the maximum potential space you’ll use on that page while still occupying that entire page is really no different in all practical sense.



On larger tables, though, right-sizing is crucial. The reason is that memory estimates will be based on the assumption that every value will be 50% populated. So if you have varchar(128), every value will expect to occupy 64 bytes, regardless of the actual data, therefore memory grants will be 64b * number of rows. If all the values will be 32 characters or less, making it a varchar(64) or even varchar(32) is probably a better choice. If a large percentage of values are close to or at the cap, you could even argue for char to take volatility out of it.



As for benefits of having string lengths capped at powers of 2, I don’t think on today’s hardware anyone could demonstrate any obvious advantages.






share|improve this answer













With 6 rows, no, there will be no observable benefit. That entire table will fit on a single page so lowering the maximum potential space you’ll use on that page while still occupying that entire page is really no different in all practical sense.



On larger tables, though, right-sizing is crucial. The reason is that memory estimates will be based on the assumption that every value will be 50% populated. So if you have varchar(128), every value will expect to occupy 64 bytes, regardless of the actual data, therefore memory grants will be 64b * number of rows. If all the values will be 32 characters or less, making it a varchar(64) or even varchar(32) is probably a better choice. If a large percentage of values are close to or at the cap, you could even argue for char to take volatility out of it.



As for benefits of having string lengths capped at powers of 2, I don’t think on today’s hardware anyone could demonstrate any obvious advantages.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 1 at 14:24









Aaron BertrandAaron Bertrand

156k18303508




156k18303508







  • 2





    @jpmc26 -1 all you like. Sometimes char is a valid choice, and sometimes what a developer has to learn how to handle (or that you have to obscure from them) isn't top priority. Anyway if you read closely my answer wasn't "use char" it was mentioned as an aside with a very specific use case as a disclaimer.

    – Aaron Bertrand
    May 2 at 13:39







  • 4





    @jpmc26 When was the last time you had the freedom to just add RAM to a production server that was wasting memory? I would -1 anywhere where you said the best approach is always to just throw hardware at something.

    – Aaron Bertrand
    May 2 at 13:41






  • 2





    @jpmc26 Agree to disagree. Adding hardware might sound like the simpler thing in some cases, but adding memory isn't always possible. It's not always your hardware. There aren't always free RAM slots. Sometimes you're already using the highest capacity chips available. I think in theory that argument works but not always in practice. Just like fragmentation, wasting memory has downstream effects too (think readable secondaries that are less equipped). And just to re-state one more time: I am not advocating char here, just mentioning it as a possibility in very specific cases.

    – Aaron Bertrand
    May 2 at 13:55







  • 2





    @jpmc26 I also don't believe that changing a varchar column to char(32) because it always or almost always contains all 32 characters is either illogical or requires extra work. Tell me all about the extra work a developer has to perform, or bugs that might be created, if they are dealing with char instead of varchar. Maybe we should petition Microsoft to remove the char type if it is so useless and dangerous?

    – Aaron Bertrand
    May 2 at 15:23






  • 2





    @jpmc26 an app that is writing data doesn't have to trim anything. An app that is displaying data also doesn't have to trim anything. An app that is comparing data shouldn't have to worry about trailing spaces unless it is building literals inside of ad hoc queries. All of those cases are moot anyway if we're talking about character data that uses the entire column width. I repeated it twice already but, once more, this is not my first string pick, it is a use case that may be appropriate sometimes. Stop talking to me like I told everyone they all have to abandon varchar tomorrow.

    – Aaron Bertrand
    May 2 at 20:33












  • 2





    @jpmc26 -1 all you like. Sometimes char is a valid choice, and sometimes what a developer has to learn how to handle (or that you have to obscure from them) isn't top priority. Anyway if you read closely my answer wasn't "use char" it was mentioned as an aside with a very specific use case as a disclaimer.

    – Aaron Bertrand
    May 2 at 13:39







  • 4





    @jpmc26 When was the last time you had the freedom to just add RAM to a production server that was wasting memory? I would -1 anywhere where you said the best approach is always to just throw hardware at something.

    – Aaron Bertrand
    May 2 at 13:41






  • 2





    @jpmc26 Agree to disagree. Adding hardware might sound like the simpler thing in some cases, but adding memory isn't always possible. It's not always your hardware. There aren't always free RAM slots. Sometimes you're already using the highest capacity chips available. I think in theory that argument works but not always in practice. Just like fragmentation, wasting memory has downstream effects too (think readable secondaries that are less equipped). And just to re-state one more time: I am not advocating char here, just mentioning it as a possibility in very specific cases.

    – Aaron Bertrand
    May 2 at 13:55







  • 2





    @jpmc26 I also don't believe that changing a varchar column to char(32) because it always or almost always contains all 32 characters is either illogical or requires extra work. Tell me all about the extra work a developer has to perform, or bugs that might be created, if they are dealing with char instead of varchar. Maybe we should petition Microsoft to remove the char type if it is so useless and dangerous?

    – Aaron Bertrand
    May 2 at 15:23






  • 2





    @jpmc26 an app that is writing data doesn't have to trim anything. An app that is displaying data also doesn't have to trim anything. An app that is comparing data shouldn't have to worry about trailing spaces unless it is building literals inside of ad hoc queries. All of those cases are moot anyway if we're talking about character data that uses the entire column width. I repeated it twice already but, once more, this is not my first string pick, it is a use case that may be appropriate sometimes. Stop talking to me like I told everyone they all have to abandon varchar tomorrow.

    – Aaron Bertrand
    May 2 at 20:33







2




2





@jpmc26 -1 all you like. Sometimes char is a valid choice, and sometimes what a developer has to learn how to handle (or that you have to obscure from them) isn't top priority. Anyway if you read closely my answer wasn't "use char" it was mentioned as an aside with a very specific use case as a disclaimer.

– Aaron Bertrand
May 2 at 13:39






@jpmc26 -1 all you like. Sometimes char is a valid choice, and sometimes what a developer has to learn how to handle (or that you have to obscure from them) isn't top priority. Anyway if you read closely my answer wasn't "use char" it was mentioned as an aside with a very specific use case as a disclaimer.

– Aaron Bertrand
May 2 at 13:39





4




4





@jpmc26 When was the last time you had the freedom to just add RAM to a production server that was wasting memory? I would -1 anywhere where you said the best approach is always to just throw hardware at something.

– Aaron Bertrand
May 2 at 13:41





@jpmc26 When was the last time you had the freedom to just add RAM to a production server that was wasting memory? I would -1 anywhere where you said the best approach is always to just throw hardware at something.

– Aaron Bertrand
May 2 at 13:41




2




2





@jpmc26 Agree to disagree. Adding hardware might sound like the simpler thing in some cases, but adding memory isn't always possible. It's not always your hardware. There aren't always free RAM slots. Sometimes you're already using the highest capacity chips available. I think in theory that argument works but not always in practice. Just like fragmentation, wasting memory has downstream effects too (think readable secondaries that are less equipped). And just to re-state one more time: I am not advocating char here, just mentioning it as a possibility in very specific cases.

– Aaron Bertrand
May 2 at 13:55






@jpmc26 Agree to disagree. Adding hardware might sound like the simpler thing in some cases, but adding memory isn't always possible. It's not always your hardware. There aren't always free RAM slots. Sometimes you're already using the highest capacity chips available. I think in theory that argument works but not always in practice. Just like fragmentation, wasting memory has downstream effects too (think readable secondaries that are less equipped). And just to re-state one more time: I am not advocating char here, just mentioning it as a possibility in very specific cases.

– Aaron Bertrand
May 2 at 13:55





2




2





@jpmc26 I also don't believe that changing a varchar column to char(32) because it always or almost always contains all 32 characters is either illogical or requires extra work. Tell me all about the extra work a developer has to perform, or bugs that might be created, if they are dealing with char instead of varchar. Maybe we should petition Microsoft to remove the char type if it is so useless and dangerous?

– Aaron Bertrand
May 2 at 15:23





@jpmc26 I also don't believe that changing a varchar column to char(32) because it always or almost always contains all 32 characters is either illogical or requires extra work. Tell me all about the extra work a developer has to perform, or bugs that might be created, if they are dealing with char instead of varchar. Maybe we should petition Microsoft to remove the char type if it is so useless and dangerous?

– Aaron Bertrand
May 2 at 15:23




2




2





@jpmc26 an app that is writing data doesn't have to trim anything. An app that is displaying data also doesn't have to trim anything. An app that is comparing data shouldn't have to worry about trailing spaces unless it is building literals inside of ad hoc queries. All of those cases are moot anyway if we're talking about character data that uses the entire column width. I repeated it twice already but, once more, this is not my first string pick, it is a use case that may be appropriate sometimes. Stop talking to me like I told everyone they all have to abandon varchar tomorrow.

– Aaron Bertrand
May 2 at 20:33





@jpmc26 an app that is writing data doesn't have to trim anything. An app that is displaying data also doesn't have to trim anything. An app that is comparing data shouldn't have to worry about trailing spaces unless it is building literals inside of ad hoc queries. All of those cases are moot anyway if we're talking about character data that uses the entire column width. I repeated it twice already but, once more, this is not my first string pick, it is a use case that may be appropriate sometimes. Stop talking to me like I told everyone they all have to abandon varchar tomorrow.

– Aaron Bertrand
May 2 at 20:33

















draft saved

draft discarded
















































Thanks for contributing an answer to Database Administrators Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f237128%2fusing-column-size-much-larger-than-necessary%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company