Table of Contents
Choose the appropriate storage engine: InnoDB" >Choose the appropriate storage engine: InnoDB
How to convert an existing MyISAM database to InnoDB:
Create an InnoDB FILE for each table:
Ensure that data is read from the memory and the data is saved in the memory " > Ensure that data is read from the memory and the data is saved in the memory
A sufficiently large innodb_buffer_pool_size
How to determine innodb_buffer_pool_size is large enough so that data is read from memory instead of hard disk?
Whether there is enough memory on the server for planning
Data warm-up
Do not allow data to be saved in SWAP
Regular optimization and reconstruction of the database" >Regular optimization and reconstruction of the database
充分使用索引" >充分使用索引
查看现有表结构和索引
比如,优化用户验证表:
使用自动加索引的框架或者自动拆分表结构的框架
分析查询日志和慢查询日志" >分析查询日志和慢查询日志
Home headlines How to optimize performance of mysql? A practical method to achieve performance optimization (for beginners' reference)

How to optimize performance of mysql? A practical method to achieve performance optimization (for beginners' reference)

Jul 20, 2017 am 11:33 AM
mysql accomplish performance

MYSQL should be the most popular WEB back-end database. WEB development languages ​​have been developing rapidly recently. PHP, Ruby, Python, and Java each have their own characteristics. Although NOSQL has been mentioned more and more recently, I believe that most architects will still choose MYSQL for data storage. So how does mysql achieve performance optimization? The following article will introduce some useful methods for MySQL performance optimization. I hope it will be helpful to everyone. .

How to optimize performance of mysql? A practical method to achieve performance optimization (for beginners' reference)

Refer to php Chinese website mysql tutorial: "Six days to take you through MySQL video tutorial"

Mysql practical method to achieve performance optimization:

Improve disk read and write speed

RAID0 especially when using EC2 When using this kind of virtual disk (EBS), it is very important to use soft RAID0.

Using MYSQL the NOSQL way

B-TREE is still one of the most efficient indexes, and all MYSQL is still future-proof.

Use HandlerSocket to skip the SQL parsing layer of MYSQL, and MYSQL will truly become NOSQL.

Reduce disk write operations

1 Use a large enough write cache innodb_log_file_size

But you need to pay attention to if you use 1G innodb_log_file_size, if the server machine and takes 10 minutes to recover.

It is recommended that innodb_log_file_size be set to 0.25 * innodb_buffer_pool_size

2 innodb_flush_log_at_trx_commit

This option is closely related to the write disk operation:

innodb_flush_log_at_trx_commit = 1, each modification is written Into disk
innodb_flush_log_at_trx_commit = 0/2 Write to disk per second

If your application does not involve high security (financial system), or the infrastructure is secure enough, or the transactions are small, all You can use 0 or 2 to slow down disk operations.

3 Avoid double write buffering

1

innodb_flush_method=O_DIRECT

Copy after login

Choose the appropriate storage engine: InnoDB

Unless your data table uses For read-only or full-text search (I believe no one will use MYSQL when it comes to full-text search now), you should choose InnoDB by default.

You may find that MyISAM is faster than InnoDB during your own testing. This is because: MyISAM only caches indexes, while InnoDB caches data and indexes. MyISAM does not support transactions. But if you use innodb_flush_log_at_trx_commit = 2 you can get close read performance (a hundred times difference).

How to convert an existing MyISAM database to InnoDB:

1

2

3

mysql -u [USER_NAME] -p -e "SHOW TABLES IN [DATABASE_NAME];" | tail -n +2 | xargs -I '{}' echo "ALTER TABLE {} ENGINE=InnoDB;" > alter_table.sql

perl -p -i -e 's/(search_[a-z_]+ ENGINE=)InnoDB//1MyISAM/g' alter_table.sql

mysql -u [USER_NAME] -p [DATABASE_NAME] < alter_table.sql

Copy after login

Create an InnoDB FILE for each table:

1

innodb_file_per_table=1

Copy after login

This ensures that the ibdata1 file will not be too large. out of control. Especially when executing mysqlcheck -o –all-databases.

Ensure that data is read from the memory and the data is saved in the memory

A sufficiently large innodb_buffer_pool_size

is recommended The data is completely stored in innodb_buffer_pool_size, that is, the capacity of innodb_buffer_pool_size is planned according to the storage amount. This way you can read data entirely from memory, minimizing disk operations.

How to determine innodb_buffer_pool_size is large enough so that data is read from memory instead of hard disk?

Method 1

1

2

3

4

5

6

7

8

9

10

11

12

mysql> SHOW GLOBAL STATUS LIKE &#39;innodb_buffer_pool_pages_%&#39;;

+----------------------------------+--------+

| Variable_name                    | Value  |

+----------------------------------+--------+

| Innodb_buffer_pool_pages_data    | 129037 |

| Innodb_buffer_pool_pages_dirty   | 362    |

| Innodb_buffer_pool_pages_flushed | 9998   |

| Innodb_buffer_pool_pages_free    | 0      |  !!!!!!!!

| Innodb_buffer_pool_pages_misc    | 2035   |

| Innodb_buffer_pool_pages_total   | 131072 |

+----------------------------------+--------+

6 rows in set (0.00 sec)

Copy after login

If you find that Innodb_buffer_pool_pages_free is 0, it means that the buffer pool has been used up and you need to increase innodb_buffer_pool_size

Several other parameters of InnoDB:

1

2

innodb_additional_mem_pool_size = 1/200 of buffer_pool

innodb_max_dirty_pages_pct 80%

Copy after login

Method 2

Or use the iostat -d -x -k 1 command to check the operation of the hard disk.

Whether there is enough memory on the server for planning

Execute echo 1 > /proc/sys/vm/drop_caches to clear the operating system's file cache and you can see the real memory usage.

Data warm-up

By default, a piece of data will be cached in innodb_buffer_pool only if it is read once. Therefore, the database has just started and needs to warm up the data and cache all the data on the disk into the memory. Data warm-up can increase read speed.

For InnoDB database, you can use the following method to warm up the data:

1. Save the following script as MakeSelectQueriesToLoad.sql

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

SELECT DISTINCT

    CONCAT(&#39;SELECT &#39;,ndxcollist,&#39; FROM &#39;,db,&#39;.&#39;,tb,

    &#39; ORDER BY &#39;,ndxcollist,&#39;;&#39;) SelectQueryToLoadCache

    FROM

    (

        SELECT

            engine,table_schema db,table_name tb,

            index_name,GROUP_CONCAT(column_name ORDER BY seq_in_index) ndxcollist

        FROM

        (

            SELECT

                B.engine,A.table_schema,A.table_name,

                A.index_name,A.column_name,A.seq_in_index

            FROM

                information_schema.statistics A INNER JOIN

                (

                    SELECT engine,table_schema,table_name

                    FROM information_schema.tables WHERE

                    engine=&#39;InnoDB&#39;

                ) B USING (table_schema,table_name)

            WHERE B.table_schema NOT IN (&#39;information_schema&#39;,&#39;mysql&#39;)

            ORDER BY table_schema,table_name,index_name,seq_in_index

        ) A

        GROUP BY table_schema,table_name,index_name

    ) AA

ORDER BY db,tb

;

Copy after login

2. Execute

1

mysql -uroot -AN < /root/MakeSelectQueriesToLoad.sql > /root/SelectQueriesToLoad.sql

Copy after login

3. Execute every time you restart the database, or when you need to warm up before backing up the entire database:

1

mysql -uroot < /root/SelectQueriesToLoad.sql > /dev/null 2>&1

Copy after login

Do not allow data to be saved in SWAP

If it is a dedicated MYSQL server, you can disable it SWAP, if it is a shared server, make sure innodb_buffer_pool_size is large enough. Or use a fixed memory space for caching and use the memlock instruction.

Regular optimization and reconstruction of the database

mysqlcheck -o –all-databases will make ibdata1 continue to grow. The real optimization can only rebuild the data table structure :

1

2

3

4

5

CREATE TABLE mydb.mytablenew LIKE mydb.mytable;

INSERT INTO mydb.mytablenew SELECT * FROM mydb.mytable;

ALTER TABLE mydb.mytable RENAME mydb.mytablezap;

ALTER TABLE mydb.mytablenew RENAME mydb.mytable;

DROP TABLE mydb.mytablezap;

Copy after login

充分使用索引

查看现有表结构和索引

1

SHOW CREATE TABLE db1.tb1/G

Copy after login

添加必要的索引

索引是提高查询速度的唯一方法,比如搜索引擎用的倒排索引是一样的原理。

索引的添加需要根据查询来确定,比如通过慢查询日志或者查询日志,或者通过 EXPLAIN 命令分析查询。

1

2

ADD UNIQUE INDEX

ADD INDEX

Copy after login
比如,优化用户验证表:

添加索引

1

2

ALTER TABLE users ADD UNIQUE INDEX username_ndx (username);

ALTER TABLE users ADD UNIQUE INDEX username_password_ndx (username,password);

Copy after login

每次重启服务器进行数据预热

1

echo “select username,password from users;” > /var/lib/mysql/upcache.sql

Copy after login

添加启动脚本到 my.cnf

1

2

[mysqld]

init-file=/var/lib/mysql/upcache.sql

Copy after login
使用自动加索引的框架或者自动拆分表结构的框架

比如,Rails 这样的框架,会自动添加索引,Drupal 这样的框架会自动拆分表结构。会在你开发的初期指明正确的方向。所以,经验不太丰富的人一开始就追求从 0 开始构建,实际是不好的做法。

分析查询日志和慢查询日志

记录所有查询,这在用 ORM 系统或者生成查询语句的系统很有用。

1

log=/var/log/mysql.log

Copy after login

注意不要在生产环境用,否则会占满你的磁盘空间。

记录执行时间超过 1 秒的查询:

1

2

long_query_time=1

log-slow-queries=/var/log/mysql/log-slow-queries.log

Copy after login

相关推荐:

1. MySQL最新手册教程

2. MySQL 5.1参考手册

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

phpmyadmin connection mysql phpmyadmin connection mysql Apr 10, 2025 pm 10:57 PM

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.