search
HomeDatabaseMysql TutorialHow to optimize performance of MySQL? Optimization Tips Sharing

How to optimize performance of MySQL? Optimization Tips Sharing

When it comes to database performance optimization, the most important thing is to choose the right one. You should decide whether your application requires a relational or non-relational database. Even within one genre, you'll have multiple options to choose from. As well as relational databases, you may find Oracle, MySQL, SQL Server, and PostgreSQL. On the other hand, non-relational databases introduced MongoDB, Cassandra, and CouchDB.

You might want me to suggest using a non-relational database for faster read/write performance. However, with some improvements and tweaks, you can push a relational database beyond its known limits. So, in this article, I will introduce you some tips to make your MySQL database faster.

If you are specifically wondering why you should use MySQL, the answer is simple because it is free, open source, and very popular in the PHP community, while Oracle is not widely used due to its high price. Other options are less popular than MySQL.

- MySQL Server Configuration: \

Well, first you should know the location of the configuration file, depending on your operating system. On Linux systems, it is located in "/etc/mysql/my.cnf". \
Now it's time to choose your engine InnoDB and MyISAM. To make the choice easier, you should know that InnoDB became the default engine in MySQL 5.5 because it supports "row-level locking, foreign keys and transactions", while MyISAM does not support any of the mentioned features, which makes it rarely useful in modern applications program. \
After selecting the correct engine, it's time to set some configuration variables in the my.cnf file.

max_connection variable: \

The max_connection variable represents the number of connections allowed by the application. The default value is 151 connections, however, if you get the error message "MySQL error, too many connections..." you can easily increase this number\

最大连接数 = 170

innodb_buffer_pool_size variable: \

To speed things up, MySQL will cache the data in your server's memory. This variable tells MySQL how many GigaBytes it can use. This variable is useful if you save large blobs in the database. You can set this to 80–90% of the server's memory. So if your server has 16GB of memory, you can set it to 14GB.

innodb_buffer_pool_size = 14GB

innodb_io_capacity variable: \

This variable tells MySQL how many input/output operations it can use, and it depends on your disk. For example, a single 7200 RPM drive is limited to 200 I/O, while an enterprise SSD disk is limited to 50,000 I/O. You can easily find the input/output values ​​from the command line on your operating system and set the variables to 90% of the available I/O so MySQL never uses too many I/O operations.

innodb_io_capacity = 21000

query_cache_limit and query_cache_size variables:\

MySQL also supports caching data in memory, but we cannot rely on it as a caching system, because every time your program requests When data is written to a database table, MySQL will rebuild the entire table's query cache. So if your program has a high load, the MySQL cache will be completely useless. In this case, it is better to set both variables to 0 to save the overhead of MySQL cache. Instead, you can use something like Redis to manage the cache.

query_cache_limit = 0

query_cache_size = 0

Slow query log:\

The slow query log will show you which of your queries exceed the threshold you define, eliminating the need to guess which query is slower. \
First, you must enable slow_query_log in your configuration file. In the Linux server, open "/etc/mysql/my.cnf" or the equivalent file on your system. \
And add:

slow_query_log = 1

long_query_time = 1

Then these two options will enable slow query logging and log any query that takes more than one second. If you prefer to view the logs in a table instead of a file, you can add:

log_output = 'TABLE'

Then you can find your logs in the "slow_log" table. There you can see information about all slow queries that performed for more than one second. This information includes the exact execution time and number of rows affected by the query, as well as which user executed it.

Query Optimization\

After you get all the slow queries, you need a way to optimize them to make them faster. Therefore, you can add the "explain" keyword in front of the query statement to obtain detailed information about the relevant query, for example: explain select * from users where active=1;

「Explanation ” keywords help you define which indexes your query hits and the number of rows you query to get the data. This information can tell you whether you need to create more indexes or restructure the database tables.

Denormalization and constraints: \

Denormalization is the process of improving read performance by adding redundant data or grouping them. For example, if you have a "Products" table and a "Category" table, and every time you query the "Products" table, you also need to get the "Category Name" of each product. In this case, you can use "join" to retrieve "category_name". However, this means that every time the user opens the product page, a complex join query is executed. Therefore, you may consider adding a "Category Name" to the "Products" table. Despite the redundant data, the increase in read performance is worth it.

The denormalization method may cause the "Category Name" in the "Product" table to be outdated. So you need to define a "foreign key" constraint, but you need to be aware that a "foreign key" will make write performance slightly slower because MySQL needs to check the constraint before writing the data. So it is always your task to make the best choice.

English original address: https://codeburst.io/database-performance-optimization-8d8407808b5b

Translation address: https://learnku.com/mysql/t/71571

[Related recommendations: mysql video tutorial]

The above is the detailed content of How to optimize performance of MySQL? Optimization Tips Sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete
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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version