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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

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 is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).
