Readers who have studied database theory should still remember the Performance Comparison Between CHAR and VARCHAR: CHAR is faster than VARCHAR because CHAR is of Fixed Length and VARCHAR needs to be increased.
Readers who have studied database theory should still remember the Performance Comparison Between CHAR and VARCHAR: CHAR is faster than VARCHAR because CHAR is of Fixed Length and VARCHAR needs to be increased.
Readers who have studied database theory should remember the performance comparison of CHAR and VARCHAR:CHAR is faster than VARCHARBecause CHAR is of a fixed length, and VARCHAR needs to add a length identifier, it needs to be operated once more.
In this case, I conducted a benchmark test. The benchmark test environment is as follows:
[Hardware configuration]
Hardware
Configuration
CPU
Intel (R) Xeon (R) CPU E5620
Memory
24G
Hard Disk
300G * 3, none
OS
RHEL5
MySQL
5.1.49/5.1.54
[MySQL configuration]
Configuration item
Configuration
Innodb_buffer_pool_size
18G
Innodb_log_file_size
200 M
Innodb_log_files_in_group
3
Sync_binlog
100
Innodb_flush_log_at_trx_commit
2
[Table configuration]
The average VARCHAR length is 200, and the CHAR length is 250. Other configurations are as follows:
Configuration item
Configuration
Number of records
1000
Storage Engine
Innodb
Row format
Compact
The performance test results are as follows:
[Query]
[Insert]
[Update]
The VARCHAR is also a random length during the update.
[Delete]
The test results show a phenomenon that is not in line with the theory: when the table size is smaller than the Innodb buffer pool, there is no difference between CHAR and VARCHAR, while when the table size is greater than the Innodb buffer pool, VARCHAR delivers higher performance! Why?
First, the performance is the result of combining multiple factors, such as hardware, configuration, number of table records, and business model. The single factor difference may have almost no impact on the whole;
For example, it takes 100 ms to execute an operation, and CHAR is only 1 microsecond faster than VARCHAR, so the final performance will not be affected.
This is why there is no difference between CHAR and VARCHAR when the Innodb buffer pool is large enough.
Again, in theory, CHAR is faster than VARCHAR because From the CPU perspectiveHowever, the performance is the final result of combining various factors. When the Innodb buffer pool is smaller than the table size, "Disk read/write"As a key factor in performance, VARCHAR is shorter, so the performance is higher than CHAR.
Finally, some may think that if the new data is longer than the old data when VARCHAR is updated, the data may need to be moved, resulting in lower performance, this operation has no significant impact on the final performance. It may be because Innodb uses pages to manage data. Data movement is completed in the memory first and then written to the disk. Therefore, even data is moved quickly.
[Application tips]
Based on the above test results and analysis, I personally think that VARCHAR is preferred in general, especially when the average length of a string is much smaller than the maximum length;
Of course, if your string is really short, for example, it only contains 10 characters, then CHAR is preferred.
Appendix:
1) if you are interested, I can infer: Why is VARCHAR about 20% faster than CHAR for 10 kW table performance in the test results?
2) the test data is only used for comparison. It does not mean that the performance of MySQL is so high in general. In order to be able to compare, a lot of preparation work has been done during the test, the test operation is also special.