学过数据库理论的读者,都应该还记得关于CHAR和VARCHAR的性能对比:CHAR比VARCHAR更快,因为CHAR是固定长度的,而VARCHAR需要增加
学过数据库理论的读者,都应该还记得关于CHAR和VARCHAR的性能对比:CHAR比VARCHAR更快,因为CHAR是固定长度的,而VARCHAR需要增加一个长度标识,处理时需要多一次运算。
针对这种情况,我做了一下基准测试,基准测试环境如下:
【硬件配置】
硬件
配置
CPU
Intel(R) Xeon(R) CPU E5620
内存
24G
硬盘
300G * 3,无
OS
RHEL5
MySQL
5.1.49/5.1.54
【MySQL配置】
配置项
配置
innodb_buffer_pool_size
18G
innodb_log_file_size
200M
innodb_log_files_in_group
3
sync_binlog
100
innodb_flush_log_at_trx_commit
2
【表配置】
VARCHAR平均长度200,CHAR长度250,其它配置如下:
配置项
配置
记录数
1000
存储引擎
Innodb
行格式
compact
性能测试结果如下:
【查询】
【插入】
【更新】
更新时VARCHAR也是随机长度
【删除】
测试结果展现了一个与理论不太相符的现象:当表大小小于Innodb buffer pool时,CHAR和VARCHAR没有差别,而在表大小大于Innodb buffer pool时,VARCHAR性能反而更高!这是为什么呢?
首先,性能是综合硬件、配置、表记录数、业务模型等多种因素综合后的结果,单一因素的差异,对整体来说可能几乎没有影响;
例如,执行一个操作需要100ms,而CHAR 比 VARCHAR性能上只快了1微秒,那么最终的性能就不会有影响。
这就是当Innodb buffer pool足够大时,CHAR 和VARCHAR没有差别的原因。
再次,理论上CHAR比VARCHAR快的根本原因是站在CPU的角度来说的,但性能是综合各种因素后的最终结果,,当Innodb buffer pool小于表大小时,"磁盘读写"成为了性能的关键因素,而VARCHAR更短,因此性能反而比CHAR高。
最后,有朋友可能会认为,VARCHAR更新时如果新的数据比旧的数据要长,可能需要移动数据,导致性能更低;从实测结果来看,这种操作对最终的性能也是没有明显影响的。可能是因为Innodb采用页管理数据,数据移动是先在内存里完成,再写到磁盘,因此数据即使移动也很快。
【应用技巧】
基于以上测试结果和分析,我个人认为一般情况下优先使用VARCHAR,特别是字符串的平均长度比最大长度要小很多的情况;
当然,如果你的字符串本来就很短,例如只有10个字符,那么就优先选CHAR了。
附:
1)有兴趣的朋友可以推断一下:为什么测试结果中10KW的表性能,VARCHAR比CHAR快大约20%?
2)测试数据只为对比用,不代表一般情况下MySQL的性能就这么高,因为为了能够对比,测试时做了很多准备工作,测试操作也是比较特殊的。

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
