MySQL - varchar length and performanceLength of varchar column - is there an advantage to using 255?Prefix...

Was it really inappropriate to write a pull request for the company I interviewed with?

Can one live in the U.S. and not use a credit card?

ESPP--any reason not to go all in?

Movie: boy escapes the real world and goes to a fantasy world with big furry trolls

Cycles on the torus

Help! My Character is too much for her story!

Too soon for a plot twist?

Did Amazon pay $0 in taxes last year?

(Codewars) Linked Lists-Sorted Insert

Why does Central Limit Theorem break down in my simulation?

If nine coins are tossed, what is the probability that the number of heads is even?

How can I portion out frozen cookie dough?

How to increase the accuracy of a plot

What to do if my university does not offer any advanced math courses?

Can I negotiate a patent idea for a raise, under French law?

Boss Telling direct supervisor I snitched

Has a sovereign Communist government ever run, and conceded loss, on a fair election?

Locked Away- What am I?

Difference between `nmap local-IP-address` and `nmap localhost`

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

Is this Paypal Github SDK reference really a dangerous site?

How to educate team mate to take screenshots for bugs with out unwanted stuff

How do we create new idioms and use them in a novel?

Is there stress on two letters on the word стоят



MySQL - varchar length and performance


Length of varchar column - is there an advantage to using 255?Prefix index length impact for VARCHAR?Performance implications of MySQL VARCHAR sizesHow to resolve: Specified key was too long; max key length is 767 byteserror 1118: row size too large. utf8 innodbWhy isn't this index helping my InnoDB MySQL query?MariaDB 10.1.38 - Specified key was too long; max key length is 767 bytesWhat is the difference between MySQL VARCHAR and TEXT data types?Performance implications of MySQL VARCHAR sizesMysql int vs varchar as primary key (InnoDB Storage Engine?Issues on defining a PK on a nvarchar column and indexing the FK referring this PKVARCHAR, InnoDB MySQL, is inline allocated space variable or fixed?Which is better MySQL search strategy, varchar x 2 or text column?Wordpress using varchar(255) for index with InnoDB and utf8mb4_unicode_ci?MySQL Large Table Indexes - Recommended length of indexvarchar(255) or varchar(256)?What's the difference between varchar(255) and varchar(max)













15















Is declaring VARCHAR size make sense for performance? Is there any difference (in speed) between VARCHAR(50) and VARCHAR(255)? Or defining length is logic/design constraint?










share|improve this question























  • dba.stackexchange.com/questions/424/…

    – RolandoMySQLDBA
    Sep 13 '14 at 22:56
















15















Is declaring VARCHAR size make sense for performance? Is there any difference (in speed) between VARCHAR(50) and VARCHAR(255)? Or defining length is logic/design constraint?










share|improve this question























  • dba.stackexchange.com/questions/424/…

    – RolandoMySQLDBA
    Sep 13 '14 at 22:56














15












15








15


8






Is declaring VARCHAR size make sense for performance? Is there any difference (in speed) between VARCHAR(50) and VARCHAR(255)? Or defining length is logic/design constraint?










share|improve this question














Is declaring VARCHAR size make sense for performance? Is there any difference (in speed) between VARCHAR(50) and VARCHAR(255)? Or defining length is logic/design constraint?







mysql database-design






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 13 '14 at 10:33









SoniqueSonique

178114




178114













  • dba.stackexchange.com/questions/424/…

    – RolandoMySQLDBA
    Sep 13 '14 at 22:56



















  • dba.stackexchange.com/questions/424/…

    – RolandoMySQLDBA
    Sep 13 '14 at 22:56

















dba.stackexchange.com/questions/424/…

– RolandoMySQLDBA
Sep 13 '14 at 22:56





dba.stackexchange.com/questions/424/…

– RolandoMySQLDBA
Sep 13 '14 at 22:56










2 Answers
2






active

oldest

votes


















27














This is a very common "exam/interview question". I will answer as good as I can:



In the standard row formats for InnoDB and MyISAM (dynamic/compact) a VARCHAR(50) and a VARCHAR(255) will store the string text in the same way- 1 byte for the length and the actual string with between 1 and 4 bytes per character (depending on the encoding and the actual character stored).



In fact, if I remember correctly, I recall someone modifying the data dictionary with an hexadecimal editor in order to change something like a VARCHAR(50) into a VARCHAR(100), so it could be done dynamically (normally, that requires a table reconstruction). And that was possible, because the actual data was not affected by that change.



That is not true with VARCHAR(256), because then 2 bytes (at least) for the length are always required.



So, that means that we should always do VARCHAR(255), shouldn't we? No. There are several reasons.



While InnoDB may store a varchar in a dynamic way, that is not true for other engines. MyISAM has a fixed row size format, and MEMORY tables are always fixed in size. Should we care about those other engines? Yes, we should, because even if we do not use them directly, MEMORY tables are very commonly used for intermediate results (temporary tables on memory), and as the results are not known beforehand, the table has to be created with the maximum size possible -VARCHAR(255) if that is our type. If you can think about the wasted space, if we are using MySQL's 'utf8' charset encoding, MEMORY will reserve 2 bytes for the length + 3 * 255 bytes per row (for values that may only take a few bytes on InnoDB). That is almost 1GB on a 1 million table -only for the VARCHAR. Not only this causes unnecessary memory stress, it may provoke the actions to be performed on disk, potentially slowing it down thousands of times. All of that because of a poor selection of its defined data type (independently of the contents).



It has some consequences for InnoDB, too. Index size is restricted to 3072 bytes and single column indexes, to 767 bytes*. So, it is very likely that you won't be able to index fully a VARCHAR(255) field (assuming you use utf8 or any other variable length-encoding).



Additionally, the maximum inline row size for InnoDB is half a page (around 8000 bytes), and variable-lenght fields like BLOB or varchar, can be stored off-page if they do not fit on the half-page. That has some consequences in performance (sometimes good, sometimes bad, depending on the usage) that cannot be ignored. This caused some weirdness between the COMPACT and DYNAMIC formats. See, for example: error 1118: row size too large. utf8 innodb



Last but not least, as @ypercube has reminded me, more than 1 byte for the length may be required even if you are using VARCHAR(255), because the definition is in characters, while the length stores bytes. For example REPEAT('ñ', 255) has more than 2^255 bytes in utf8, so it would require more than 1 byte for storing its length:



mysql> SELECT LENGTH(REPEAT('ñ', 255));
+---------------------------+
| LENGTH(REPEAT('ñ', 255)) |
+---------------------------+
| 510 |
+---------------------------+
1 row in set (0.02 sec)

mysql> SELECT CHAR_LENGTH(REPEAT('ñ', 255));
+--------------------------------+
| CHAR_LENGTH(REPEAT('ñ', 255)) |
+--------------------------------+
| 255 |
+--------------------------------+
1 row in set (0.00 sec)


So the general piece of advice is to use the smallest type possible, because it can potentially create performance or management problems otherwise. A VARCHAR(100) is better than VARCHAR(255) (although a VARCHAR(20) would be better), even if you do not know the exact length. Try to be conservative because, unless the table is too large, you can always change the definition later.



Update: Because the exploding popularity of variable-length strings, for example, with the usage of emojis, Oracle has been pushing for improved performance for those cases. In the latest MySQL versions (5.6, 5.7), InnoDB has been set as the default engine for both intrinsic and explicit temporary tables meaning that variable-length fields are now first-class citizens. That means that there may be less reasons to have very constrained character lengths (but those still exist).



(*) Second Update: large_prefix_index is now enabled by default on the latest MySQL versions (8.0), but that is still true for older versions or if you are using lagacy innodb file/row formats (other than dynamic or compressed), but now by default, single column indexes can be up to those 3072 bytes.






share|improve this answer


























  • small update: MySQL-8.0.13+ uses TempTable by default for temp tables which has efficient storage for varchars.

    – danblack
    Jan 14 at 22:25



















0














Forget about the 1- versus 2-byte prefix on VARCHARs.




  • It impacts performance by a minuscule amount.

  • It is "2" more often than the obvious rule says.


The question about 255 has been asked and answered many times.




  • Too many long VARCHARs can lead to failure of CREATE TABLE.

  • Temp tables may turn into MEMORY tables, with VARCHARs turned into VARCHAR. This means, for example, that VARCHAR(255) CHARACTER SET utf8mb4 wants a fixed length of 1020 bytes. (This will fail, and it will degenerate to using MyISAM.)


Bottom line: Don't blindly use 255 (or 256); do what makes sense for the schema.






share|improve this answer























    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%2f76469%2fmysql-varchar-length-and-performance%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









    27














    This is a very common "exam/interview question". I will answer as good as I can:



    In the standard row formats for InnoDB and MyISAM (dynamic/compact) a VARCHAR(50) and a VARCHAR(255) will store the string text in the same way- 1 byte for the length and the actual string with between 1 and 4 bytes per character (depending on the encoding and the actual character stored).



    In fact, if I remember correctly, I recall someone modifying the data dictionary with an hexadecimal editor in order to change something like a VARCHAR(50) into a VARCHAR(100), so it could be done dynamically (normally, that requires a table reconstruction). And that was possible, because the actual data was not affected by that change.



    That is not true with VARCHAR(256), because then 2 bytes (at least) for the length are always required.



    So, that means that we should always do VARCHAR(255), shouldn't we? No. There are several reasons.



    While InnoDB may store a varchar in a dynamic way, that is not true for other engines. MyISAM has a fixed row size format, and MEMORY tables are always fixed in size. Should we care about those other engines? Yes, we should, because even if we do not use them directly, MEMORY tables are very commonly used for intermediate results (temporary tables on memory), and as the results are not known beforehand, the table has to be created with the maximum size possible -VARCHAR(255) if that is our type. If you can think about the wasted space, if we are using MySQL's 'utf8' charset encoding, MEMORY will reserve 2 bytes for the length + 3 * 255 bytes per row (for values that may only take a few bytes on InnoDB). That is almost 1GB on a 1 million table -only for the VARCHAR. Not only this causes unnecessary memory stress, it may provoke the actions to be performed on disk, potentially slowing it down thousands of times. All of that because of a poor selection of its defined data type (independently of the contents).



    It has some consequences for InnoDB, too. Index size is restricted to 3072 bytes and single column indexes, to 767 bytes*. So, it is very likely that you won't be able to index fully a VARCHAR(255) field (assuming you use utf8 or any other variable length-encoding).



    Additionally, the maximum inline row size for InnoDB is half a page (around 8000 bytes), and variable-lenght fields like BLOB or varchar, can be stored off-page if they do not fit on the half-page. That has some consequences in performance (sometimes good, sometimes bad, depending on the usage) that cannot be ignored. This caused some weirdness between the COMPACT and DYNAMIC formats. See, for example: error 1118: row size too large. utf8 innodb



    Last but not least, as @ypercube has reminded me, more than 1 byte for the length may be required even if you are using VARCHAR(255), because the definition is in characters, while the length stores bytes. For example REPEAT('ñ', 255) has more than 2^255 bytes in utf8, so it would require more than 1 byte for storing its length:



    mysql> SELECT LENGTH(REPEAT('ñ', 255));
    +---------------------------+
    | LENGTH(REPEAT('ñ', 255)) |
    +---------------------------+
    | 510 |
    +---------------------------+
    1 row in set (0.02 sec)

    mysql> SELECT CHAR_LENGTH(REPEAT('ñ', 255));
    +--------------------------------+
    | CHAR_LENGTH(REPEAT('ñ', 255)) |
    +--------------------------------+
    | 255 |
    +--------------------------------+
    1 row in set (0.00 sec)


    So the general piece of advice is to use the smallest type possible, because it can potentially create performance or management problems otherwise. A VARCHAR(100) is better than VARCHAR(255) (although a VARCHAR(20) would be better), even if you do not know the exact length. Try to be conservative because, unless the table is too large, you can always change the definition later.



    Update: Because the exploding popularity of variable-length strings, for example, with the usage of emojis, Oracle has been pushing for improved performance for those cases. In the latest MySQL versions (5.6, 5.7), InnoDB has been set as the default engine for both intrinsic and explicit temporary tables meaning that variable-length fields are now first-class citizens. That means that there may be less reasons to have very constrained character lengths (but those still exist).



    (*) Second Update: large_prefix_index is now enabled by default on the latest MySQL versions (8.0), but that is still true for older versions or if you are using lagacy innodb file/row formats (other than dynamic or compressed), but now by default, single column indexes can be up to those 3072 bytes.






    share|improve this answer


























    • small update: MySQL-8.0.13+ uses TempTable by default for temp tables which has efficient storage for varchars.

      – danblack
      Jan 14 at 22:25
















    27














    This is a very common "exam/interview question". I will answer as good as I can:



    In the standard row formats for InnoDB and MyISAM (dynamic/compact) a VARCHAR(50) and a VARCHAR(255) will store the string text in the same way- 1 byte for the length and the actual string with between 1 and 4 bytes per character (depending on the encoding and the actual character stored).



    In fact, if I remember correctly, I recall someone modifying the data dictionary with an hexadecimal editor in order to change something like a VARCHAR(50) into a VARCHAR(100), so it could be done dynamically (normally, that requires a table reconstruction). And that was possible, because the actual data was not affected by that change.



    That is not true with VARCHAR(256), because then 2 bytes (at least) for the length are always required.



    So, that means that we should always do VARCHAR(255), shouldn't we? No. There are several reasons.



    While InnoDB may store a varchar in a dynamic way, that is not true for other engines. MyISAM has a fixed row size format, and MEMORY tables are always fixed in size. Should we care about those other engines? Yes, we should, because even if we do not use them directly, MEMORY tables are very commonly used for intermediate results (temporary tables on memory), and as the results are not known beforehand, the table has to be created with the maximum size possible -VARCHAR(255) if that is our type. If you can think about the wasted space, if we are using MySQL's 'utf8' charset encoding, MEMORY will reserve 2 bytes for the length + 3 * 255 bytes per row (for values that may only take a few bytes on InnoDB). That is almost 1GB on a 1 million table -only for the VARCHAR. Not only this causes unnecessary memory stress, it may provoke the actions to be performed on disk, potentially slowing it down thousands of times. All of that because of a poor selection of its defined data type (independently of the contents).



    It has some consequences for InnoDB, too. Index size is restricted to 3072 bytes and single column indexes, to 767 bytes*. So, it is very likely that you won't be able to index fully a VARCHAR(255) field (assuming you use utf8 or any other variable length-encoding).



    Additionally, the maximum inline row size for InnoDB is half a page (around 8000 bytes), and variable-lenght fields like BLOB or varchar, can be stored off-page if they do not fit on the half-page. That has some consequences in performance (sometimes good, sometimes bad, depending on the usage) that cannot be ignored. This caused some weirdness between the COMPACT and DYNAMIC formats. See, for example: error 1118: row size too large. utf8 innodb



    Last but not least, as @ypercube has reminded me, more than 1 byte for the length may be required even if you are using VARCHAR(255), because the definition is in characters, while the length stores bytes. For example REPEAT('ñ', 255) has more than 2^255 bytes in utf8, so it would require more than 1 byte for storing its length:



    mysql> SELECT LENGTH(REPEAT('ñ', 255));
    +---------------------------+
    | LENGTH(REPEAT('ñ', 255)) |
    +---------------------------+
    | 510 |
    +---------------------------+
    1 row in set (0.02 sec)

    mysql> SELECT CHAR_LENGTH(REPEAT('ñ', 255));
    +--------------------------------+
    | CHAR_LENGTH(REPEAT('ñ', 255)) |
    +--------------------------------+
    | 255 |
    +--------------------------------+
    1 row in set (0.00 sec)


    So the general piece of advice is to use the smallest type possible, because it can potentially create performance or management problems otherwise. A VARCHAR(100) is better than VARCHAR(255) (although a VARCHAR(20) would be better), even if you do not know the exact length. Try to be conservative because, unless the table is too large, you can always change the definition later.



    Update: Because the exploding popularity of variable-length strings, for example, with the usage of emojis, Oracle has been pushing for improved performance for those cases. In the latest MySQL versions (5.6, 5.7), InnoDB has been set as the default engine for both intrinsic and explicit temporary tables meaning that variable-length fields are now first-class citizens. That means that there may be less reasons to have very constrained character lengths (but those still exist).



    (*) Second Update: large_prefix_index is now enabled by default on the latest MySQL versions (8.0), but that is still true for older versions or if you are using lagacy innodb file/row formats (other than dynamic or compressed), but now by default, single column indexes can be up to those 3072 bytes.






    share|improve this answer


























    • small update: MySQL-8.0.13+ uses TempTable by default for temp tables which has efficient storage for varchars.

      – danblack
      Jan 14 at 22:25














    27












    27








    27







    This is a very common "exam/interview question". I will answer as good as I can:



    In the standard row formats for InnoDB and MyISAM (dynamic/compact) a VARCHAR(50) and a VARCHAR(255) will store the string text in the same way- 1 byte for the length and the actual string with between 1 and 4 bytes per character (depending on the encoding and the actual character stored).



    In fact, if I remember correctly, I recall someone modifying the data dictionary with an hexadecimal editor in order to change something like a VARCHAR(50) into a VARCHAR(100), so it could be done dynamically (normally, that requires a table reconstruction). And that was possible, because the actual data was not affected by that change.



    That is not true with VARCHAR(256), because then 2 bytes (at least) for the length are always required.



    So, that means that we should always do VARCHAR(255), shouldn't we? No. There are several reasons.



    While InnoDB may store a varchar in a dynamic way, that is not true for other engines. MyISAM has a fixed row size format, and MEMORY tables are always fixed in size. Should we care about those other engines? Yes, we should, because even if we do not use them directly, MEMORY tables are very commonly used for intermediate results (temporary tables on memory), and as the results are not known beforehand, the table has to be created with the maximum size possible -VARCHAR(255) if that is our type. If you can think about the wasted space, if we are using MySQL's 'utf8' charset encoding, MEMORY will reserve 2 bytes for the length + 3 * 255 bytes per row (for values that may only take a few bytes on InnoDB). That is almost 1GB on a 1 million table -only for the VARCHAR. Not only this causes unnecessary memory stress, it may provoke the actions to be performed on disk, potentially slowing it down thousands of times. All of that because of a poor selection of its defined data type (independently of the contents).



    It has some consequences for InnoDB, too. Index size is restricted to 3072 bytes and single column indexes, to 767 bytes*. So, it is very likely that you won't be able to index fully a VARCHAR(255) field (assuming you use utf8 or any other variable length-encoding).



    Additionally, the maximum inline row size for InnoDB is half a page (around 8000 bytes), and variable-lenght fields like BLOB or varchar, can be stored off-page if they do not fit on the half-page. That has some consequences in performance (sometimes good, sometimes bad, depending on the usage) that cannot be ignored. This caused some weirdness between the COMPACT and DYNAMIC formats. See, for example: error 1118: row size too large. utf8 innodb



    Last but not least, as @ypercube has reminded me, more than 1 byte for the length may be required even if you are using VARCHAR(255), because the definition is in characters, while the length stores bytes. For example REPEAT('ñ', 255) has more than 2^255 bytes in utf8, so it would require more than 1 byte for storing its length:



    mysql> SELECT LENGTH(REPEAT('ñ', 255));
    +---------------------------+
    | LENGTH(REPEAT('ñ', 255)) |
    +---------------------------+
    | 510 |
    +---------------------------+
    1 row in set (0.02 sec)

    mysql> SELECT CHAR_LENGTH(REPEAT('ñ', 255));
    +--------------------------------+
    | CHAR_LENGTH(REPEAT('ñ', 255)) |
    +--------------------------------+
    | 255 |
    +--------------------------------+
    1 row in set (0.00 sec)


    So the general piece of advice is to use the smallest type possible, because it can potentially create performance or management problems otherwise. A VARCHAR(100) is better than VARCHAR(255) (although a VARCHAR(20) would be better), even if you do not know the exact length. Try to be conservative because, unless the table is too large, you can always change the definition later.



    Update: Because the exploding popularity of variable-length strings, for example, with the usage of emojis, Oracle has been pushing for improved performance for those cases. In the latest MySQL versions (5.6, 5.7), InnoDB has been set as the default engine for both intrinsic and explicit temporary tables meaning that variable-length fields are now first-class citizens. That means that there may be less reasons to have very constrained character lengths (but those still exist).



    (*) Second Update: large_prefix_index is now enabled by default on the latest MySQL versions (8.0), but that is still true for older versions or if you are using lagacy innodb file/row formats (other than dynamic or compressed), but now by default, single column indexes can be up to those 3072 bytes.






    share|improve this answer















    This is a very common "exam/interview question". I will answer as good as I can:



    In the standard row formats for InnoDB and MyISAM (dynamic/compact) a VARCHAR(50) and a VARCHAR(255) will store the string text in the same way- 1 byte for the length and the actual string with between 1 and 4 bytes per character (depending on the encoding and the actual character stored).



    In fact, if I remember correctly, I recall someone modifying the data dictionary with an hexadecimal editor in order to change something like a VARCHAR(50) into a VARCHAR(100), so it could be done dynamically (normally, that requires a table reconstruction). And that was possible, because the actual data was not affected by that change.



    That is not true with VARCHAR(256), because then 2 bytes (at least) for the length are always required.



    So, that means that we should always do VARCHAR(255), shouldn't we? No. There are several reasons.



    While InnoDB may store a varchar in a dynamic way, that is not true for other engines. MyISAM has a fixed row size format, and MEMORY tables are always fixed in size. Should we care about those other engines? Yes, we should, because even if we do not use them directly, MEMORY tables are very commonly used for intermediate results (temporary tables on memory), and as the results are not known beforehand, the table has to be created with the maximum size possible -VARCHAR(255) if that is our type. If you can think about the wasted space, if we are using MySQL's 'utf8' charset encoding, MEMORY will reserve 2 bytes for the length + 3 * 255 bytes per row (for values that may only take a few bytes on InnoDB). That is almost 1GB on a 1 million table -only for the VARCHAR. Not only this causes unnecessary memory stress, it may provoke the actions to be performed on disk, potentially slowing it down thousands of times. All of that because of a poor selection of its defined data type (independently of the contents).



    It has some consequences for InnoDB, too. Index size is restricted to 3072 bytes and single column indexes, to 767 bytes*. So, it is very likely that you won't be able to index fully a VARCHAR(255) field (assuming you use utf8 or any other variable length-encoding).



    Additionally, the maximum inline row size for InnoDB is half a page (around 8000 bytes), and variable-lenght fields like BLOB or varchar, can be stored off-page if they do not fit on the half-page. That has some consequences in performance (sometimes good, sometimes bad, depending on the usage) that cannot be ignored. This caused some weirdness between the COMPACT and DYNAMIC formats. See, for example: error 1118: row size too large. utf8 innodb



    Last but not least, as @ypercube has reminded me, more than 1 byte for the length may be required even if you are using VARCHAR(255), because the definition is in characters, while the length stores bytes. For example REPEAT('ñ', 255) has more than 2^255 bytes in utf8, so it would require more than 1 byte for storing its length:



    mysql> SELECT LENGTH(REPEAT('ñ', 255));
    +---------------------------+
    | LENGTH(REPEAT('ñ', 255)) |
    +---------------------------+
    | 510 |
    +---------------------------+
    1 row in set (0.02 sec)

    mysql> SELECT CHAR_LENGTH(REPEAT('ñ', 255));
    +--------------------------------+
    | CHAR_LENGTH(REPEAT('ñ', 255)) |
    +--------------------------------+
    | 255 |
    +--------------------------------+
    1 row in set (0.00 sec)


    So the general piece of advice is to use the smallest type possible, because it can potentially create performance or management problems otherwise. A VARCHAR(100) is better than VARCHAR(255) (although a VARCHAR(20) would be better), even if you do not know the exact length. Try to be conservative because, unless the table is too large, you can always change the definition later.



    Update: Because the exploding popularity of variable-length strings, for example, with the usage of emojis, Oracle has been pushing for improved performance for those cases. In the latest MySQL versions (5.6, 5.7), InnoDB has been set as the default engine for both intrinsic and explicit temporary tables meaning that variable-length fields are now first-class citizens. That means that there may be less reasons to have very constrained character lengths (but those still exist).



    (*) Second Update: large_prefix_index is now enabled by default on the latest MySQL versions (8.0), but that is still true for older versions or if you are using lagacy innodb file/row formats (other than dynamic or compressed), but now by default, single column indexes can be up to those 3072 bytes.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jun 6 '18 at 7:52

























    answered Sep 13 '14 at 13:42









    jynusjynus

    11.1k11832




    11.1k11832













    • small update: MySQL-8.0.13+ uses TempTable by default for temp tables which has efficient storage for varchars.

      – danblack
      Jan 14 at 22:25



















    • small update: MySQL-8.0.13+ uses TempTable by default for temp tables which has efficient storage for varchars.

      – danblack
      Jan 14 at 22:25

















    small update: MySQL-8.0.13+ uses TempTable by default for temp tables which has efficient storage for varchars.

    – danblack
    Jan 14 at 22:25





    small update: MySQL-8.0.13+ uses TempTable by default for temp tables which has efficient storage for varchars.

    – danblack
    Jan 14 at 22:25













    0














    Forget about the 1- versus 2-byte prefix on VARCHARs.




    • It impacts performance by a minuscule amount.

    • It is "2" more often than the obvious rule says.


    The question about 255 has been asked and answered many times.




    • Too many long VARCHARs can lead to failure of CREATE TABLE.

    • Temp tables may turn into MEMORY tables, with VARCHARs turned into VARCHAR. This means, for example, that VARCHAR(255) CHARACTER SET utf8mb4 wants a fixed length of 1020 bytes. (This will fail, and it will degenerate to using MyISAM.)


    Bottom line: Don't blindly use 255 (or 256); do what makes sense for the schema.






    share|improve this answer




























      0














      Forget about the 1- versus 2-byte prefix on VARCHARs.




      • It impacts performance by a minuscule amount.

      • It is "2" more often than the obvious rule says.


      The question about 255 has been asked and answered many times.




      • Too many long VARCHARs can lead to failure of CREATE TABLE.

      • Temp tables may turn into MEMORY tables, with VARCHARs turned into VARCHAR. This means, for example, that VARCHAR(255) CHARACTER SET utf8mb4 wants a fixed length of 1020 bytes. (This will fail, and it will degenerate to using MyISAM.)


      Bottom line: Don't blindly use 255 (or 256); do what makes sense for the schema.






      share|improve this answer


























        0












        0








        0







        Forget about the 1- versus 2-byte prefix on VARCHARs.




        • It impacts performance by a minuscule amount.

        • It is "2" more often than the obvious rule says.


        The question about 255 has been asked and answered many times.




        • Too many long VARCHARs can lead to failure of CREATE TABLE.

        • Temp tables may turn into MEMORY tables, with VARCHARs turned into VARCHAR. This means, for example, that VARCHAR(255) CHARACTER SET utf8mb4 wants a fixed length of 1020 bytes. (This will fail, and it will degenerate to using MyISAM.)


        Bottom line: Don't blindly use 255 (or 256); do what makes sense for the schema.






        share|improve this answer













        Forget about the 1- versus 2-byte prefix on VARCHARs.




        • It impacts performance by a minuscule amount.

        • It is "2" more often than the obvious rule says.


        The question about 255 has been asked and answered many times.




        • Too many long VARCHARs can lead to failure of CREATE TABLE.

        • Temp tables may turn into MEMORY tables, with VARCHARs turned into VARCHAR. This means, for example, that VARCHAR(255) CHARACTER SET utf8mb4 wants a fixed length of 1020 bytes. (This will fail, and it will degenerate to using MyISAM.)


        Bottom line: Don't blindly use 255 (or 256); do what makes sense for the schema.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 12 mins ago









        Rick JamesRick James

        43.3k22259




        43.3k22259






























            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%2f76469%2fmysql-varchar-length-and-performance%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

            Parapolítica Índice Antecedentes El escándalo Proceso judicial Consecuencias Véase...

            How to remove border from elements in the last row?Targeting flex items on the last rowHow to vertically wrap...

            Tecnologías entrañables Índice Antecedentes Desarrollo Tecnologías Entrañables en la...