MySQL
VARCHAR
database
string length
SQL limits

What is the MySQL VARCHAR max size?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

MySQL's VARCHAR type is a versatile string data type that holds data of varying length. Understanding its maximum size involves both SQL syntax and some knowledge of underlying storage. This article provides a detailed insight into the maximum size of the VARCHAR type in MySQL, including its variations under different versions and its impact on storage and performance.

What is VARCHAR?

VARCHAR(n) is a variable-length string data type in MySQL where n represents the maximum number of characters that the column can store. Unlike fixed-length types such as CHAR, VARCHAR allows you to store data of varying length, potentially reducing wasted storage space.

Maximum Size of VARCHAR

Character Count

The formal specification of VARCHAR in SQL:2003 states that the maximum length can be up to 65,535 bytes. However, this limit is not just about the characters; it includes additional overhead stored by MySQL to manage the VARCHAR column.

  • Overhead: VARCHAR columns include 1 or 2 additional bytes to store the length of the data. If the length of the stored string is less or equal to 255 bytes, 1 byte is used for length. If it's more, 2 bytes are needed.
  • Character Encoding: The actual maximum number of characters that can be stored can vary based on character set. For instance, utf8mb4, a common charset that supports full Unicode, can require up to 4 bytes per character. Thus, the formula to calculate the maximum number of characters is as follows:
    maximum characters=65535length bytesmax bytes per character\text{maximum characters} = \left\lfloor \frac{65535 - \text{length bytes}}{\text{max bytes per character}} \right\rfloor#### Practical Example

Consider a scenario where you're using the utf8mb4 charset:

  • Each character may require up to 4 bytes.
    Max characters=6553524=16383\text{Max characters} = \left\lfloor \frac{65535 - 2}{4} \right\rfloor = 16383In this case, the maximum number of characters you can store in a VARCHAR using utf8mb4 is 16,383.

Influential Factors on VARCHAR Size

  1. Row Size Constraints:
    • A row in InnoDB may not extend a total of 65,535 bytes across all columns.
    • Having multiple VARCHAR or other large columns can limit the potential size of a single VARCHAR column.
  2. Other Column Types:
    • MySQL provides TEXT, MEDIUMTEXT, LONGTEXT for larger text requirements that surpass VARCHAR limits, albeit with different storage and indexing properties.
  3. Table Definition Limits:
    • The InnoDB table has its own storage constraints that can affect the effective use of VARCHAR.

Considerations for Usage

  • Performance: Using the largest possible VARCHAR size may cause performance degradation due to increased I/O if not required by the application's logic.
  • Indexes: MySQL limits the size of index keys; this affects the indexing of large VARCHAR columns.

Summarizing the Key Constraints

ConstraintImpact on VARCHAR
Maximum Storage65,535 bytes per row, including length overhead.
Length BytesAdds 1-2 bytes overhead per VARCHAR column.
Character EncodingAffects how many characters fit based on bytes/char.
Max Length (utf8mb4)16,383 characters with 4-byte max char encoding.
Row ConstraintsCombined row size across all columns is limited.

In conclusion, while VARCHAR is a highly flexible data type suitable for many use cases, it comes with considerations that must be addressed in terms of storage capacity, performance characteristics, and character set constraints. When designing a database schema, it's crucial to understand these parameters to ensure efficient and effective database utilization.


Course illustration
Course illustration