修改InnoDB数据页大小的背景与原理
通常认为InnoDB的数据页固定为16K,且被视为硬性限制,系统层面无法直接更改,期望未来MySQL能像Oracle那样支持多种数据页尺寸。然而在实际应用中,16K有时显得过大,特别是当业务从Oracle或SQL Server迁移到MySQL后,若发现IO增长显著,便会考虑调整数据页大小。实际上,InnoDB的数据页大小是可以更改的,但这需要在源码层面进行修改,随后重新构建MySQL。具体更改步骤如下:以MySQL-5.1.38源码为例,需定位至storage/innobase/include/univ.i文件,在该文件中搜索UNIV_PAGE_SIZE。
/*
DATABASE VERSION CONTROL
========================
*/
/* The universal page size of the database */
#define UNIV_PAGE_SIZE (2 * 8192) /* NOTE! Currently, this has to be a
power of 2 */
/* The 2-logarithm of UNIV_PAGE_SIZE: */
#define UNIV_PAGE_SIZE_SHIFT 14
/* Maximum number of parallel threads in a parallelized operation */
#define UNIV_MAX_PARALLELISM 32
UNIV_PAGE_SIZE代表数据页大小,默认值为16K。其备注说明该值必须设置为2的次方。可设置的数值包括4K、8K、16K、32K和64K,超过64K则无实际意义。在更改UNIV_PAGE_SIZE的同时,还需修改UNIV_PAGE_SIZE_SHIFT,该值表示2的多少次方等于UNIV_PAGE_SIZE。因此,不同数据页大小对应的设置情况如下:
#define UNIV_PAGE_SIZE_SHIFT 12 if UNIV_PAGE_SIZ=4K
#define UNIV_PAGE_SIZE_SHIFT 13 if UNIV_PAGE_SIZ=8K
#define UNIV_PAGE_SIZE_SHIFT 15 if UNIV_PAGE_SIZ=32K
例如,若要将InnoDB的数据页更改为8K,相应的修改应为:
/*
DATABASE VERSION CONTROL
========================
*/
/* The universal page size of the database */
#define UNIV_PAGE_SIZE 8192 /* NOTE! Currently, this has to be a
power of 2 */
/* The 2-logarithm of UNIV_PAGE_SIZE: */
#define UNIV_PAGE_SIZE_SHIFT 13
/* Maximum number of parallel threads in a parallelized operation */
#define UNIV_MAX_PARALLELISM 32
完成修改后,重新编译代码,并进行充分的测试验证。祝好运!