Background
The MySQL documentation states the following:
In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.
To put this to the test myself, I created two tables:
CREATE TABLE `varchar_length_test_255` (
`characters` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `varchar_length_test_256` (
`characters` varchar(256) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I then inserted 10,000 rows into each table, each row with values having the maximum length for the characters column.
Since I am using a character set that has a maximum byte length of one byte per character (latin1), I expected to see a difference of 20,000 bytes in storage size between the two tables, derived from the following:
- Each row in the
varchar_length_test_256table contains an additional character than the rows in thevarchar_length_test_255table. With thelatin1character set, that adds up to10,000bytes, since there are 10,000 rows in each table. - Based on the MySQL documentation,
VARCHARvalues exceeding255bytes require an additional "length" byte. Since each row in thevarchar_length_test_256table contains a value in thecharacterscolumn that has a length of256, which equates to256bytes for each value since thelatin1character set is used, that adds up to another10,000bytes utilized.
Problem
When issuing a query to retrieve the size of each table, it appears that the tables are the same size! I used the following query (based on off of this SO post) to determine the size of each table:
SELECT
table_name AS `Table`,
(data_length + index_length) `Size in Bytes`
FROM
information_schema.TABLES
WHERE
table_schema = "test";
which yielded this output:
+-------------------------+---------------+
| Table | Size in Bytes |
+-------------------------+---------------+
| varchar_length_test_255 | 4734976 |
| varchar_length_test_256 | 4734976 |
+-------------------------+---------------+
2 rows in set (0.00 sec)
What am I missing here?
- Am I correctly understanding the MySQL documentation?
- Is there something wrong with my test that is preventing the expected outcome?
- Is the query I am using to calculate the size of the tables correct?
- How could I correctly observe the information communicated in the MySQL documentation?