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

Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020