Introduction to MySQL database optimization (pictures and text)
The content of this article is an introduction (pictures and texts) about MySQL database optimization. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
On the one hand, database optimization is to identify the bottlenecks of the system and improve the overall performance of the MySQL database. On the other hand, it requires reasonable structural design and parameter adjustment to improve the user's response speed, and at the same time, as much as possible Save system resources so that the system can provide greater load. (Related recommendations: MySQL Tutorial)
1. Optimization Overview
2. Optimization
The author divides optimization into two categories, soft optimization and hard optimization. Soft optimization generally involves operating the database, while hard optimization involves operating the server hardware and parameter settings.
2.1 Soft optimization
2.1.1 Query statement optimization
1. First, we can use the EXPLAIN or DESCRIBE (abbreviation: DESC) command to analyze the execution information of a query statement.
2. Example:
DESC SELECT * FROM `user`
Display:
In which information such as the index and the number of query data read data will be displayed.
2.1.2 Optimizing subqueries
In MySQL, try to use JOIN instead of subqueries. Because subqueries require nested queries, a temporary table will be created when nesting queries. The creation of the temporary table and Deletion will have a large system overhead, and join queries will not create temporary tables, so the efficiency is higher than nested subqueries.
2.1.3 Using indexes
Indexes improve database query speed One of the most important methods. Regarding indexes, you can refer to the author's article "MySQL Database Index". The introduction is more detailed. Three major precautions for using indexes are recorded here:
- LIKE keyword matching Strings starting with '%' will not use indexes.
- Both fields of the OR keyword must be indexed before the query will use indexes.
- Use multi-column indexes Must satisfy the leftmost matching.
2.1.4 Decompose table
For tables with many fields, if some fields are used less frequently, they should be separated at this time Thus forming a new table,
2.1.5 Intermediate table
For tables that require a large number of connection queries, an intermediate table can be created, thereby reducing the time-consuming connection caused during the query.
2.1.6 Add redundant fields
Similar to creating an intermediate table, adding redundancy is also to reduce connection queries.
2.1.7 Analyze tables, check tables, optimize tables
Analyzing the table mainly analyzes the distribution of keywords in the table, checking the table mainly checks whether there are errors in the table, optimizing the table mainly eliminates the waste of table space caused by deletion or update.
Analyzing the table: Use ANALYZE Key Words, such as ANALYZE TABLE user;Msg_type: Information type, including status, info, note, warning, error.
Msg_text: Display information.
Check table: Use the CHECK keyword, such as CHECK TABLE user [option]
option is only valid for MyISAM, with a total of five parameter values:
QUICK: Do not scan rows , does not check wrong connections.
FAST: Only checks tables that have not been closed correctly.
CHANGED: Only checks tables that have been changed since the last check and tables that have not been closed correctly.
MEDIUM: Scan the line to verify that the deleted connection is valid, and also calculate the keyword checksum of each line.
EXTENDED: The most comprehensive check, comprehensive keywords for each line Search.
Optimize the table: Use the OPTIMIZE keyword, such as OPTIMIZE [LOCAL|NO_WRITE_TO_BINLOG] TABLE user;
LOCAL|NO_WRITE_TO_BINLOG all means not to write to the log. Optimizing the table is only for VARCHAR, BLOB and TEXT are valid. File fragmentation can be eliminated through the OPTIMIZE TABLE statement, and read-only locks will be added during execution.
2.2 Hard optimization
2.2.1 Hardware three-piece set
1. Configure a multi-core and high-frequency CPU. Multi-core can execute multiple threads.
2. Configure large memory and increase the memory to increase the cache capacity, thus reducing disk I/O O time, thereby improving the response speed.
3. Configure high-speed disks or reasonably distributed disks: high-speed disks improve I/O, and distributed disks can improve the ability of parallel operations.
2.2.2 Optimization Database parameters
Optimizing database parameters can improve resource utilization and thereby improve MySQL server performance. The configuration parameters of the MySQL service are all in my.cnf or my.ini. The parameters that have a greater impact on performance are listed below.
key_buffer_size: index buffer size
table_cache: the number of tables that can be opened simultaneously
query_cache_size and query_cache_type: The former is the query buffer size, the latter is the switch of the previous parameter, 0 means not to use the buffer, 1 means to use the buffer, but you can use SQL_NO_CACHE in the query to mean not to use the buffer, 2 means The buffer must be used only when it is clearly stated in the query, that is, SQL_CACHE.
sort_buffer_size: sorting buffer
Portal:More parameters
2.2.3 Sub-database and sub-table
Because the database pressure is too high, the first problem is that the system performance may be reduced during peak periods, because excessive database load will have an impact on performance. Another one, what should you do if your database crashes due to excessive pressure? So at this time, you must divide the system into databases and tables, and separate reading and writing, that is, splitting one database into multiple databases and deploying them on multiple database services. At this time, the database will serve as the main database to handle write requests. Then each master library mounts at least one slave library, and the slave library handles read requests.
2.2.4 Cache Cluster
If the number of users is getting larger and larger, you can keep adding machines at this time, for example, at the system level. By adding more machines, you can handle higher concurrent requests. Then if the write concurrency at the database level becomes higher and higher, the database server will be expanded and the machine will be expanded through sub-database and table sharding. If the read concurrency at the database level becomes higher and higher, the capacity will be expanded and more slave databases will be added. But there is a big problem here: the database itself is not actually used to carry high concurrent requests, so generally speaking, the concurrency carried by a single database machine per second is in the order of thousands, and the machines used by the database are relatively high-configuration , relatively expensive machines, the cost is very high. If you simply keep adding machines, it is actually wrong. Therefore, cache is usually included in high-concurrency architectures. The cache system is designed to carry high concurrency. Therefore, the amount of concurrency carried by a single machine is tens of thousands, or even hundreds of thousands per second, and the carrying capacity of high concurrency is one to two orders of magnitude higher than that of a database system. Therefore, you can completely introduce a cache cluster according to the business characteristics of the system for requests that require less writing and more reading. Specifically, when writing to the database, a copy of the data is written to the cache cluster at the same time, and then the cache cluster is used to carry most of the read requests. In this case, through cache clustering, fewer machine resources can be used to host higher concurrency.
Conclusion
A complete and complex high-concurrency system architecture will definitely include: various complex self-developed infrastructure systems. All kinds of exquisite architectural designs. Therefore, a small article can at most have the effect of inspiring others, but that's about it for database optimization ideas.
The above is the detailed content of Introduction to MySQL database optimization (pictures and text). 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

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

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



PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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, 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.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

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.
