search
HomeDatabaseMysql TutorialMySQL数据库优化实践

MySQL数据库优化实践

Jun 07, 2016 pm 04:33 PM
mysqloptimizationpracticeusdatabasetidy

最近一段时间,我们整理了一些关于Percona,Linux,Flashcache,硬件设备的优化经验,分享给大家: 硬件 1.开启BBWC RAID卡都有写cache(Battery Backed Write Cache),写cache对IO性能的提升非常明显,因为掉电会丢失数据,所以必须由电池提供支持。电池会定

最近一段时间,我们整理了一些关于Percona,Linux,Flashcache,硬件设备的优化经验,分享给大家:

硬件

1.开启BBWC

RAID卡都有写cache(Battery Backed Write Cache),写cache对IO性能的提升非常明显,因为掉电会丢失数据,所以必须由电池提供支持。电池会定期充放电,一般为90天左右,当发现电量低于某个阀值时,会将写cache策略从writeback置为writethrough,相当于写cache会失效,这时如果系统有大量的IO操作,可能会明显感觉到IO响应速度变慢。目前,新的RAID卡内置了flash存储,掉电后会将写cache的数据写入flash中,这样就可以保证数据永不丢失,但依然需要电池的支持。

解决方案有两种:1.人工触发充放电,可以选择在业务低谷时做,降低对应用的影响;2.设置写cache策略为force write back,即使电池失效,也保持写cache策略为writeback,这样存在掉电后丢失数据的风险。

目前,有一些硬件厂家提供了电容供电的RAID卡,没有电池充放电的问题,可以联系自己的硬件厂商。

2.RAID卡配置

关闭读cache:RAID卡上的cache容量有限,我们选择direct方式读取数据,从而忽略读cache。

关闭预读:RAID卡的预读功能对于随机IO几乎没有任何提升,所以将预读功能关闭。

关闭磁盘cache:一般情况下,如果使用RAID,系统会默认关闭磁盘的cache,也可以用命令强制关闭。

以上设置都可以通过RAID卡的命令行来完成,比如LSI芯片的RAID卡使用megacli命令。

3.开启Fastpath功能

Fastpath是LSI的新特性,在RAID控制器为SSD做了了优化,使用fastpath特性可以最大程度发挥出SSD的能力。如果使用SSD做RAID的方式,可以开启fastpath功能。关于fastpath特性,可以从LSI官网下载资料,并咨询自己的硬件提供商。

4.Fusionio参数调整

基本上,Fusionio无需做任何调整,下列三个参数可能会提升性能:

options iomemory-vsl use_workqueue=0

对于fusionio设备,忽略Linux IO调度,相当于使用NOOP。

options iomemory-vsl disable-msi=0

开启MSI中断,如果设备支持,则打开。

options iomemory-vsl use_large_pcie_rx_buffer=1

打开Large PCIE buffer,可能会提升性能。

操作系统

1.IO调度算法

Linux有四种IO调度算法:CFQ,Deadline,Anticipatory和NOOP,CFQ是默认的IO调度算法。完全随机的访问环境下,CFQ与Deadline,NOOP性能差异很小,但是一旦有大的连续IO,CFQ可能会造成小IO的响应延时增加,所以数据库环境建议修改为deadline算法,表现更稳定。我们的环境统一使用deadline算法。

IO调度算法都是基于磁盘设计,所以减少磁头移动是最重要的考虑因素之一,但是使用Flash存储设备之后,不再需要考虑磁头移动的问题,可以使用NOOP算法。NOOP的含义就是NonOperation,意味着不会做任何的IO优化,完全按照请求来FIFO的方式来处理IO。

减少预读:/sys/block/sdb/queue/read_ahead_kb,默认128,调整为16

增大队列:/sys/block/sdb/queue/nr_requests,默认128,调整为512

2.NUMA设置

单机单实例,建议关闭NUMA,关闭的方法有三种:1.硬件层,在BIOS中设置关闭;2.OS内核,启动时设置numa=off;3.可以用numactl命令将内存分配策略修改为interleave(交叉),有些硬件可以在BIOS中设置。

单机多实例,请参考:http://www.hellodb.net/2011/06/mysql_multi_instance.html

3.文件系统设置

我们使用XFS文件系统,XFS有两个设置:su(stripe size)和sw(stirpe width),要根据硬件层RAID来设置这两个参数,比如10块盘做RAID10,条带大小为64K,XFS设置为su=64K,sw=10。

xfs?mount参数:defaults,rw,noatime,nodiratime,noikeep,nobarrier,allocsize=8M,attr2,largeio,inode64,swalloc

数据库

1.Flashcache参数

创建flashcache:flashcache_create -b 4k cachedev /dev/sdc /dev/sdb

指定flashcache的block大小与Percona的page大小相同。

Flashcache参数设置:

flashcache.fast_remove = 1:打开fast remove特性,关闭机器时,无需将cache中的脏块写入磁盘。

flashcache.reclaim_policy = 1:脏块刷出策略,0:FIFO,1:LRU。

flashcache.dirty_thresh_pct = 90:flashcache上每个hash set上的脏块阀值。

flashcache.cache_all = 1:cache所有内容,可以用黑名单过滤。

flashecache.write_merge = 1:打开写入合并,提升写磁盘的性能。

2.Percona参数

innodb_page_size:如果使用fusionio,4K的性能最好;使用SAS磁盘,设置为8K。如果全表扫描很多,可以设置为16K。比较小的page size,可以提升cache的命中率。

innodb_adaptive_checkpoint:如果使用fusionio,设置为3,提高刷新频率到0.1秒;使用SAS磁盘,设置为2,采用estimate方式刷新脏页。

innodb_io_capacity:根据IOPS能力设置,使用fuionio可以设置10000以上。

innodb_flush_neighbor_pages = 0:针对fusionio或者SSD,因为随机IO足够好,所以关闭此功能。

innodb_flush_method=ALL_O_DIRECT:公版的MySQL只能将数据库文件读写设置为DirectIO,对于Percona可以将log和数据文件设置为direct方式读写。但是我不确定这个参数对于innodb_flush_log_at_trx_commit的影响,

innodb_read_io_threads = 1:设置预读线程设置为1,因为线性预读的效果并不明显,所以无需设置更大。

innodb_write_io_threads = 16:设置写线程数量为16,提升写的能力。

innodb_fast_checksum = 1:开启Fast checksum特性。

监控

1.fusionio监控:fio-status命令

Media status: Healthy; Reserves: 100.00%, warn at 10.00%

Thresholds: write-reduced: 96.00%, read-only: 94.00%

Lifetime data volumes:

Logical bytes written : 2,664,888,862,208

Logical bytes read??? : 171,877,629,608,448

Physical bytes written: 27,665,550,363,560

Physical bytes read?? : 223,382,659,085,448

2.flashcache监控:dmsetup status

read hit percent(99)

write hit percent(51)

dirty write hit percent(44)

–EOF–

MySQL数据库优化实践 最近一段时间,我们整理了一些关于Percona,Linux,Flashcache,硬件设备的优化经验,分享给大家: 硬件 1.开启BBWC RAID卡都有写cache(Battery Backed Write Cache),写cache对IO性能的提升非常明显,因为掉电会丢失数据,所以必须由电池提供支持。电池会定期充放电,一般为90天左右,当发现电量低于某个阀值时,会将写cache策略从writeback置为writethrough,相当于写cache会失效,这时如果系统有大量的IO操作,可能会明显感觉到IO响应速度变慢。目前,新的RAID卡内置了flash存储,掉电后会将写cache的数据写入flash中,这样就可以保证数据永不丢失,但依然需要电池的支持。 解决方案有两种:1.人工触发充放电,可以选择在业务低谷时做,降低对应用的影响;2.设置写cache策略为force write back,即使电池失效,也保持写cache策略为writeback,这样存在掉电后丢失数据的风险。 目前,有一些硬件厂家提供了电容供电的RAID卡,没有电池充放电的问题,可以联系自己的硬件厂商。 2.RAID卡配置 关闭读cache:RAID卡上的cache容量有限,我们选择direct方式读取数据,从而忽略读cache。 关闭预读:RAID卡的预读功能对于随机IO几乎没有任何提升,所以将预读功能关闭。 关闭磁盘cache:一般情况下,如果使用RAID,系统会默认关闭磁盘的cache,也可以用命令强制关闭。 以上设置都可以通过RAID卡的命令行来完成,比如LSI芯片的RAID卡使用megacli命令。 3.开启Fastpath功能 Fastpath是LSI的新特性,在RAID控制器为SSD做了了优化,使用fastpath特性可以最大程度发挥出SSD的能力。如果使用SSD做RAID的方式,可以开启fastpath功能。关于fastpath特性,可以从LSI官网下载资料,并咨询自己的硬件提供商。 4.Fusionio参数调整 基本上,Fusionio无需做任何调整,下列三个参数可能会提升性能: options iomemory-vsl use_workqueue=0 对于fusionio设备,忽略Linux IO调度,相当于使用NOOP。 options iomemory-vsl disable-msi=0 开启MSI中断,如果设备支持,则打开。 options iomemory-vsl use_large_pcie_rx_buffer=1 打开Large PCIE buffer,可能会提升性能。 操作系统 1.IO调度算法 Linux有四种IO调度算法:CFQ,Deadline,Anticipatory和NOOP,CFQ是默认的IO调度算法。完全随机的访问环境下,CFQ与Deadline,NOOP性能差异很小,但是一旦有大的连续IO,CFQ可能会造成小IO的响应延时增加,所以数据库环境建议修改为deadline算法,表现更稳定。我们的环境统一使用deadline算法。 IO调度算法都是基于磁盘设计,所以减少磁头移动是最重要的考虑因素之一,但是使用Flash存储设备之后,不再需要考虑磁头移动的问题,可以使用NOOP算法。NOOP的含义就是NonOperation,意味着不会做任何的IO优化,完全按照请求来FIFO的方式来处理IO。 减少预读:/sys/block/sdb/queue/read_ahead_kb,默认128,调整为16 增大队列:/sys/block/sdb/queue/nr_requests,默认128,调整为512 2.NUMA设置 单机单实例,建议关闭NUMA,关闭的方法有三种:1.硬件层,在BIOS中设置关闭;2.OS内核,启动时设置numa=off;3.可以用numactl命令将内存分配策略修改为interleave(交叉),有些硬件可以在BIOS中设置。 单机多实例,请参考:http://www.hellodb.net/2011/06/mysql_multi_instance.html 3.文件系统设置 我们使用XFS文件系统,XFS有两个设置:su(stripe size)和sw(stirpe width),要根据硬件层RAID来设置这两个参数,比如10块盘做RAID10,条带大小为64K,XFS设置为su=64K,sw=10。 xfs?mount参数:defaults,rw,noatime,nodiratime,noikeep,nobarrier,allocsize=8M,attr2,largeio,inode64,swalloc 数据库 1.Flashcache参数 创建flashcache:flashcache_create -b 4k cachedev /dev/sdc /dev/sdb 指定flashcache的block大小与Percona的page大小相同。 Flashcache参数设置: flashcache.fast_remove = 1:打开fast remove特性,关闭机器时,无需将cache中的脏块写入磁盘。 flashcache.reclaim_policy = 1:脏块刷出策略,0:FIFO,1:LRU。 flashcache.dirty_thresh_pct = 90:flashcache上每个hash set上的脏块阀值。 flashcache.cache_all = 1:cache所有内容,可以用黑名单过滤。 flashecache.write_merge = 1:打开写入合并,提升写磁盘的性能。 2.Percona参数 innodb_page_size:如果使用fusionio,4K的性能最好;使用SAS磁盘,设置为8K。如果全表扫描很多,可以设置为16K。比较小的page size,可以提升cache的命中率。 innodb_adaptive_checkpoint:如果使用fusionio,设置为3,提高刷新频率到0.1秒;使用SAS磁盘,设置为2,采用estimate方式刷新脏页。 innodb_io_capacity:根据IOPS能力设置,使用fuionio可以设置10000以上。 innodb_flush_neighbor_pages = 0:针对fusionio或者SSD,因为随机IO足够好,所以关闭此功能。 innodb_flush_method=ALL_O_DIRECT:公版的MySQL只能将数据库文件读写设置为DirectIO,对于Percona可以将log和数据文件设置为direct方式读写。但是我不确定这个参数对于innodb_flush_log_at_trx_commit的影响, innodb_read_io_threads = 1:设置预读线程设置为1,因为线性预读的效果并不明显,所以无需设置更大。 innodb_write_io_threads = 16:设置写线程数量为16,提升写的能力。 innodb_fast_checksum = 1:开启Fast checksum特性。 监控 1.fusionio监控:fio-status命令 Media status: Healthy; Reserves: 100.00%, warn at 10.00% Thresholds: write-reduced: 96.00%, read-only: 94.00% Lifetime [...]MySQL数据库优化实践
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor