When to Use CHAR vs VARCHAR in MySQL? Understanding Length, Storage, and Performance
CHAR stores fixed‑length strings padded with spaces, while VARCHAR stores variable‑length strings with a length byte, leading to different maximum sizes—255 characters for CHAR and up to 65,535 bytes for VARCHAR (about 21,589 UTF‑8 characters)—and distinct use cases such as short fixed fields versus space‑saving flexible fields.
Basic Differences
CHAR is fixed‑length; MySQL pads the value on the right with spaces to reach the defined length.
VARCHAR is variable‑length; each value occupies only the bytes needed plus a length byte (one byte if length < 255, otherwise two bytes).
Maximum Lengths
CHAR can be defined up to 255 characters (not bytes).
VARCHAR can be defined up to 65,535 bytes (not characters). With utf8 encoding (3 bytes per character), the practical maximum is about 21,589 characters.
Applicable Scenarios
Use CHAR for short, relatively fixed‑length fields such as postal codes, UUIDs, or columns that change frequently, because it avoids the overhead of length calculation.
Use VARCHAR when you want to save space for variable‑length data.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
