如果您遵循 SQL 操作的数据库设计和调优技术的最佳实践,但由于磁盘 I/O 活动繁重,您的数据库仍然很慢,请考虑这些磁盘 I/O 优化。如果 Unix top 工具或 Windows 任务管理器显示您的工作负载的 CPU 使用率低于 70%,则您的工作负载可能受磁盘限制。If you follow best practices for database design and tuning
techniques for SQL operations, but your database is still slow
due to heavy disk I/O activity, consider these disk I/O
optimizations. If the Unix top tool or the
Windows Task Manager shows that the CPU usage percentage with
your workload is less than 70%, your workload is probably
disk-bound.
增加缓冲池大小Increase buffer pool size
当表数据缓存在
InnoDB缓冲池中时,可以通过查询重复访问它,而不需要任何磁盘 I/O。使用innodb_buffer_pool_size选项指定缓冲池的大小。此内存区域非常重要,因此通常建议将innodb_buffer_pool_size配置为系统内存的 50% 到 75%。有关详细信息,请参阅第 10.12.3.1 节“MySQL 如何使用内存”。When table data is cached in theInnoDBbuffer pool, it can be accessed repeatedly by queries without requiring any disk I/O. Specify the size of the buffer pool with theinnodb_buffer_pool_sizeoption. This memory area is important enough that it is typically recommended thatinnodb_buffer_pool_sizeis configured to 50 to 75 percent of system memory. For more information see, Section 10.12.3.1, “How MySQL Uses Memory”.调整冲洗方式Adjust the flush method
在某些版本的 GNU/Linux 和 Unix 中,使用 Unix
fsync()调用和类似方法将文件刷新到磁盘的速度出奇地慢。如果数据库写入性能存在问题,请使用设置为O_DSYNC的innodb_flush_method参数进行基准测试。In some versions of GNU/Linux and Unix, flushing files to disk with the Unixfsync()call and similar methods is surprisingly slow. If database write performance is an issue, conduct benchmarks with theinnodb_flush_methodparameter set toO_DSYNC.配置操作系统刷新阈值Configure a threshold for operating system flushes
默认情况下,当
InnoDB创建一个新的数据文件,例如新的日志文件或表空间文件时,文件会先完全写入操作系统缓存,然后才刷新到磁盘,这可能会导致大量磁盘写入活动同时发生。为了强制从操作系统缓存进行更小、周期性的数据刷新,您可以使用innodb_fsync_threshold变量来定义一个阈值,以字节为单位。当达到字节阈值时,操作系统缓存的内容会刷新到磁盘。默认值 0 会强制使用默认行为,即只有在文件完全写入缓存后才将数据刷新到磁盘。By default, whenInnoDBcreates a new data file, such as a new log file or tablespace file, the file is fully written to the operating system cache before it is flushed to disk, which can cause a large amount of disk write activity to occur at once. To force smaller, periodic flushes of data from the operating system cache, you can use theinnodb_fsync_thresholdvariable to define a threshold value, in bytes. When the byte threshold is reached, the contents of the operating system cache are flushed to disk. The default value of 0 forces the default behavior, which is to flush data to disk only after a file is fully written to the cache.在多个 MySQL 实例使用相同存储设备的情况下,指定阈值来强制进行较小的定期刷新可能会很有用。例如,创建新的 MySQL 实例及其关联的数据文件可能会导致磁盘写入活动大幅激增,从而影响使用相同存储设备的其他 MySQL 实例的性能。配置阈值有助于避免写入活动激增。Specifying a threshold to force smaller, periodic flushes may be beneficial in cases where multiple MySQL instances use the same storage devices. For example, creating a new MySQL instance and its associated data files could cause large surges of disk write activity, impeding the performance of other MySQL instances that use the same storage devices. Configuring a threshold helps avoid such surges in write activity.
使用 fdatasync() 而不是 fsync()Use fdatasync() instead of fsync()
在支持
fdatasync()系统调用的平台上,innodb_use_fdatasync变量允许对操作系统使用fdatasync()而不是fsync()脸红。fdatasync()系统调用不会刷新对文件元数据的更改,除非后续数据检索需要,从而提供潜在的性能优势。On platforms that supportfdatasync()system calls, theinnodb_use_fdatasyncvariable permits usingfdatasync()instead offsync()for operating system flushes. Anfdatasync()system call does not flush changes to file metadata unless required for subsequent data retrieval, providing a potential performance benefit.innodb_flush_method设置的子集,例如fsync、O_DSYNC和O_DIRECT使用fsync()系统调用。使用这些设置时,innodb_use_fdatasync变量适用。A subset ofinnodb_flush_methodsettings such asfsync,O_DSYNC, andO_DIRECTusefsync()system calls. Theinnodb_use_fdatasyncvariable is applicable when using those settings.在 Linux 上将 noop 或截止时间 I/O 调度程序与本机 AIO 结合使用Use a noop or deadline I/O scheduler with native AIO on Linux
InnoDB使用 Linux 上的异步 I/O 子系统(本机 AIO)来执行数据文件页面的预读和写入请求。此行为由innodb_use_native_aio配置选项控制,该选项默认启用。对于本机 AIO,I/O 调度程序的类型对 I/O 性能影响更大。通常,建议使用 noop 和 Deadline I/O 调度程序。进行基准测试以确定哪个 I/O 调度程序可以为您的工作负载和环境提供最佳结果。有关详细信息,请参阅第 17.8.6 节“在 Linux 上使用异步 I/O”。InnoDBuses the asynchronous I/O subsystem (native AIO) on Linux to perform read-ahead and write requests for data file pages. This behavior is controlled by theinnodb_use_native_aioconfiguration option, which is enabled by default. With native AIO, the type of I/O scheduler has greater influence on I/O performance. Generally, noop and deadline I/O schedulers are recommended. Conduct benchmarks to determine which I/O scheduler provides the best results for your workload and environment. For more information, see Section 17.8.6, “Using Asynchronous I/O on Linux”.在 Solaris 10 上针对 x86_64 架构使用直接 I/OUse direct I/O on Solaris 10 for x86_64 architecture
在 x86_64 架构 (AMD Opteron) 的 Solaris 10 上使用
InnoDB存储引擎时,对InnoDB相关文件使用直接 I/O,以避免降低InnoDB性能。要对用于存储InnoDB相关文件的整个 UFS 文件系统使用直接 I/O,请使用forcedirectio选项挂载它;请参阅mount_ufs(1M)。 (Solaris 10/x86_64 上默认不使用此选项。)要将直接 I/O 仅应用于InnoDB文件操作而不是整个文件系统,请设置innodb_flush_method = O_DIRECT。通过此设置,InnoDB调用directio()而不是fcntl()来进行数据文件的 I/O(而不是日志文件的 I/O)。When using theInnoDBstorage engine on Solaris 10 for x86_64 architecture (AMD Opteron), use direct I/O forInnoDB-related files to avoid degradation ofInnoDBperformance. To use direct I/O for an entire UFS file system used for storingInnoDB-related files, mount it with theforcedirectiooption; seemount_ufs(1M). (The default on Solaris 10/x86_64 is not to use this option.) To apply direct I/O only toInnoDBfile operations rather than the whole file system, setinnodb_flush_method = O_DIRECT. With this setting,InnoDBcallsdirectio()instead offcntl()for I/O to data files (not for I/O to log files).在 Solaris 2.6 或更高版本中使用原始存储来存储数据和日志文件Use raw storage for data and log files with Solaris 2.6 or later
在任何 Solaris 2.6 及更高版本和任何平台 (sparc/x86/x64/amd64) 上使用具有较大
innodb_buffer_pool_size值的InnoDB存储引擎时,请进行基准测试使用原始设备或单独的直接 I/O UFS 文件系统上的InnoDB数据文件和日志文件,使用前面所述的forcedirectio挂载选项。 (如果要对日志文件进行直接 I/O,则必须使用挂载选项,而不是设置innodb_flush_method。)Veritas 文件系统 VxFS 的用户应使用convosync=direct挂载选项。When using theInnoDBstorage engine with a largeinnodb_buffer_pool_sizevalue on any release of Solaris 2.6 and up and any platform (sparc/x86/x64/amd64), conduct benchmarks withInnoDBdata files and log files on raw devices or on a separate direct I/O UFS file system, using theforcedirectiomount option as described previously. (It is necessary to use the mount option rather than settinginnodb_flush_methodif you want direct I/O for the log files.) Users of the Veritas file system VxFS should use theconvosync=directmount option.不要将其他 MySQL 数据文件,例如
MyISAM表的数据文件,放在直接 I/O 文件系统上。可执行文件或库不得放置在直接 I/O 文件系统上。Do not place other MySQL data files, such as those forMyISAMtables, on a direct I/O file system. Executables or libraries must not be placed on a direct I/O file system.使用额外的存储设备Use additional storage devices
额外的存储设备可用于设置 RAID 配置。有关相关信息,请参阅第 10.12.1 节“优化磁盘 I/O”。Additional storage devices could be used to set up a RAID configuration. For related information, see Section 10.12.1, “Optimizing Disk I/O”.
或者,
InnoDB表空间数据文件和日志文件可以放置在不同的物理磁盘上。有关详细信息,请参阅以下部分:Alternatively,InnoDBtablespace data files and log files can be placed on different physical disks. For more information, refer to the following sections:考虑非旋转存储Consider non-rotational storage
非旋转存储通常为随机 I/O 操作提供更好的性能;以及用于顺序 I/O 操作的循环存储。当跨旋转和非旋转存储设备分布数据和日志文件时,请考虑主要在每个文件上执行的 I/O 操作类型。Non-rotational storage generally provides better performance for random I/O operations; and rotational storage for sequential I/O operations. When distributing data and log files across rotational and non-rotational storage devices, consider the type of I/O operations that are predominantly performed on each file.
面向随机 I/O 的文件通常包括file-per-table 和通用表空间数据文件、撤消表空间 文件和临时表空间 文件。面向顺序 I/O 的文件包括
InnoDB系统表空间 文件、双写文件和日志文件,例如 二进制日志 文件和 重做日志 文件。Random I/O-oriented files typically include file-per-table and general tablespace data files, undo tablespace files, and temporary tablespace files. Sequential I/O-oriented files includeInnoDBsystem tablespace files, doublewrite files, and log files such as binary log files and redo log files.使用非旋转存储时查看以下配置选项的设置:Review settings for the following configuration options when using non-rotational storage:
crc32选项使用更快的校验和算法,建议用于快速存储系统。Thecrc32option uses a faster checksum algorithm and is recommended for fast storage systems.优化旋转存储设备的 I/O。对于非旋转存储或旋转和非旋转存储的混合禁用它。默认情况下它是禁用的。Optimizes I/O for rotational storage devices. Disable it for non-rotational storage or a mix of rotational and non-rotational storage. It is disabled by default.
允许在空闲期间限制页面刷新,这有助于延长非旋转存储设备的使用寿命。Permits placing a limit on page flushing during idle periods, which can help extend the life of non-rotational storage devices.
innodb_io_capacityinnodb_io_capacity默认设置 10000 通常就足够了。The default setting of 10000 is generally sufficient.
innodb_io_capacity_maxinnodb_io_capacity_max默认值 (2 *
innodb_io_capacity) 适用于大多数工作负载。The default value of (2 *innodb_io_capacity) is intended for most workloads.如果重做日志位于非旋转存储上,请考虑禁用此选项以减少日志记录。请参阅禁用压缩页面日志记录。If redo logs are on non-rotational storage, consider disabling this option to reduce logging. See Disable logging of compressed pages.
innodb_log_file_size(已弃用)innodb_log_file_size(deprecated)如果重做日志位于非旋转存储上,请配置此选项以最大化缓存和写入组合。If redo logs are on non-rotational storage, configure this option to maximize caching and write combining.
如果重做日志位于非旋转存储上,请配置此选项以最大化缓存和写入组合。If redo logs are on non-rotational storage, configure this option to maximize caching and write combining.
考虑使用与磁盘内部扇区大小匹配的页面大小。早期 SSD 设备的扇区大小通常为 4KB。一些较新的设备具有 16KB 扇区大小。默认的
InnoDB页面大小为 16KB。保持页面大小接近存储设备块大小可以最大限度地减少重写到磁盘的未更改数据量。Consider using a page size that matches the internal sector size of the disk. Early-generation SSD devices often have a 4KB sector size. Some newer devices have a 16KB sector size. The defaultInnoDBpage size is 16KB. Keeping the page size close to the storage device block size minimizes the amount of unchanged data that is rewritten to disk.如果二进制日志位于非旋转存储上并且所有表都有主键,请考虑将此选项设置为
minimal以减少日志记录。If binary logs are on non-rotational storage and all tables have primary keys, consider setting this option tominimalto reduce logging.
确保您的操作系统启用了 TRIM 支持。它通常默认启用。Ensure that TRIM support is enabled for your operating system. It is typically enabled by default.
增加 I/O 容量以避免积压Increase I/O capacity to avoid backlogs
如果吞吐量由于
InnoDB检查点 操作而周期性下降,请考虑增加innodb_io_capacity配置选项的值。较高的值会导致更频繁的刷新,避免积压的工作导致吞吐量下降。If throughput drops periodically because ofInnoDBcheckpoint operations, consider increasing the value of theinnodb_io_capacityconfiguration option. Higher values cause more frequent flushing, avoiding the backlog of work that can cause dips in throughput.如果刷新不落后,则降低 I/O 容量Lower I/O capacity if flushing does not fall behind
如果系统在
InnoDB刷新 操作方面没有落后,请考虑降低innodb_io_capacity配置的值选项。通常,您可以将此选项值设置为尽可能低的值,但不要太低,以免导致吞吐量周期性下降,如前面项目符号中所述。在典型情况下,您可以降低选项值,您可能会在SHOW ENGINE INNODB STATUS的输出中看到如下组合:If the system is not falling behind withInnoDBflushing operations, consider lowering the value of theinnodb_io_capacityconfiguration option. Typically, you keep this option value as low as practical, but not so low that it causes periodic drops in throughput as mentioned in the preceding bullet. In a typical scenario where you could lower the option value, you might see a combination like this in the output fromSHOW ENGINE INNODB STATUS:历史列表长度较低,低于几千。History list length low, below a few thousand.
插入缓冲区合并到插入的行附近。Insert buffer merges close to rows inserted.
缓冲池中修改的页面始终远低于缓冲池的
innodb_max_dirty_pages_pct。 (在服务器不进行批量插入时测量;在批量插入期间,修改的页面百分比显着上升是正常的。)Modified pages in buffer pool consistently well belowinnodb_max_dirty_pages_pctof the buffer pool. (Measure at a time when the server is not doing bulk inserts; it is normal during bulk inserts for the modified pages percentage to rise significantly.)Log sequence number - Last checkpoint小于InnoDB总大小的 7/8,最好小于 6/8 日志文件。Log sequence number - Last checkpointis at less than 7/8 or ideally less than 6/8 of the total size of theInnoDBlog files.
在 Fusion-io 设备上存储系统表空间文件Store system tablespace files on Fusion-io devices
您可以通过将包含双写存储区域的文件存储在支持原子写入的 Fusion-io 设备上来利用与双写缓冲区相关的 I/O 优化。 (doublewrite 缓冲区存储区域驻留在 doublewrite 文件中。请参见第 17.6.4 节“Doublewrite 缓冲区”。)当 doublewrite 存储区域文件放置在支持原子写入的 Fusion-io 设备上时,doublewrite 缓冲区会自动禁用,并且 Fusion-io 原子写入将用于所有数据文件。此功能仅在 Fusion-io 硬件上受支持,并且仅针对 Linux 上的 Fusion-io NVMFS 启用。要充分利用此功能,建议将
innodb_flush_method设置为O_DIRECT。You can take advantage of a doublewrite buffer-related I/O optimization by storing the files that contain the doublewrite storage area on Fusion-io devices that support atomic writes. (The doublewrite buffer storage area resides in doublewrite files. See Section 17.6.4, “Doublewrite Buffer”.) When doublewrite storage area files are placed on Fusion-io devices that support atomic writes, the doublewrite buffer is automatically disabled and Fusion-io atomic writes are used for all data files. This feature is only supported on Fusion-io hardware and is only enabled for Fusion-io NVMFS on Linux. To take full advantage of this feature, aninnodb_flush_methodsetting ofO_DIRECTis recommended.Note由于双写缓冲区设置是全局的,因此对于不驻留在 Fusion-io 硬件上的数据文件,双写缓冲区也会被禁用。Because the doublewrite buffer setting is global, the doublewrite buffer is also disabled for data files that do not reside on Fusion-io hardware.
禁用压缩页面的日志记录 Disable logging of compressed pages
使用
InnoDB表压缩功能时,重新压缩页面会写入重做日志。此行为由innodb_log_compressed_pages控制,默认情况下启用该功能以防止在恢复期间使用不同版本的zlib压缩算法时可能发生的损坏。如果您确定zlib版本不会更改,请禁用innodb_log_compressed_pages以减少修改压缩数据的工作负载生成重做日志。When using theInnoDBtable compression feature, images of re-compressed pages are written to the redo log when changes are made to compressed data. This behavior is controlled byinnodb_log_compressed_pages, which is enabled by default to prevent corruption that can occur if a different version of thezlibcompression algorithm is used during recovery. If you are certain that thezlibversion is not subject to change, disableinnodb_log_compressed_pagesto reduce redo log generation for workloads that modify compressed data.