Table of Contents
Leftmost index matching rule
Home Database Mysql Tutorial Introducing the MySQL large table optimization solution

Introducing the MySQL large table optimization solution

Jan 28, 2021 am 09:28 AM
mysql

Introducing the MySQL large table optimization solution

Free learning recommendation: mysql database(Video )

Background

Alibaba Cloud RDS FOR MySQL (MySQL version 5.7) database business table adds more than 10 million new data every month. As the amount of data continues to increase, slow queries on large tables appear in our business. During peak business periods, slow queries on the main business table take dozens of seconds, seriously affecting the business

Program Overview

Introducing the MySQL large table optimization solution

1. Database design and index optimization

The MySQL database itself is highly flexible, resulting in insufficient performance and heavy reliance on the developer's table design capabilities and indexing Optimization capabilities, here are some optimization suggestions

  • Convert the time type to timestamp format, store it in int type, build an index to increase query efficiency
  • It is recommended that the field definition is not null, null Values ​​are difficult to query and optimize and occupy additional index space
  • Use TINYINT type instead of enumeration ENUM
  • To store precise floating point numbers, DECIMAL must be used instead of FLOAT and DOUBLE
  • The field length is serious According to business needs, do not set it too large
  • Try not to use the TEXT type. If you must use it, it is recommended to split the infrequently used large fields into other tables
  • MySQL has restrictions on the length of index fields. Yes, the length of each index column of the innodb engine is limited to 767 bytes by default, and the sum of the lengths of all index columns cannot be greater than 3072 bytes (mysql8.0 single index can create 1024 characters)
  • 大If the table has DDL requirements, please contact the DBA

Leftmost index matching rule

As the name suggests, it means leftmost priority. When creating a combined index, it should be used in the where clause according to business needs. The most frequent column is placed on the far left. A very important issue in a compound index is how to arrange the order of columns. For example, if the two fields c1 and c2 are used after where, then the order of the index is (c1, c2) or (c2, c1). The correct approach is to repeat The smaller the value, the higher it is placed. For example, if 95% of the values ​​in a column are not repeated, then this column can generally be placed at the front.

  • Compound index index(a,b,c)
  • where a=3 only uses a
  • where a=3 and b=5 uses a,b
  • where a=3 and b=5 and c=4 uses a, b,c
  • where b=3 or where c=4 No index is used
  • where a=3 and c=4 Only a
  • where a=3 and b> 10 and c=7 uses a,b
  • where a=3 and b like 'xx%' and c=7 uses a,b
  • which is actually equivalent to creating multiple indexes: key (a), key(a,b), key(a,b,c)

2. Switch the database to PloarDB read-write separation

PolarDB It is a next-generation relational cloud database self-developed by Alibaba Cloud. It is 100% compatible with MySQL. The storage capacity can reach up to 100 TB. A single database can be expanded to up to 16 nodes. It is suitable for diversified database application scenarios of enterprises. PolarDB adopts an architecture that separates storage and computing. All computing nodes share a copy of data and provides minute-level configuration upgrades and downgrades, second-level fault recovery, global data consistency, and free data backup and disaster recovery services.

  • Cluster architecture, separation of computing and storage
    PolarDB adopts a multi-node cluster architecture. There is a Writer node (master node) and multiple Reader nodes (read-only nodes) in the cluster. Each node Sharing the underlying storage (PolarStore) through the distributed file system (PolarFileSystem)
  • Read-write separation
    When the application uses the cluster address, PolarDB provides external services through the internal proxy layer (Proxy), and the application All requests go through the proxy first and then access the database node. The proxy layer can not only perform security authentication and protection, but also parse SQL, send write operations (such as transactions, UPDATE, INSERT, DELETE, DDL, etc.) to the master node, and evenly distribute read operations (such as SELECT) to multiple nodes. Read nodes realize automatic read and write separation. For applications, it's as simple as using a single point of database.

In offline mixed scenarios: different services use different connection addresses and use different data nodes to avoid mutual influence

Introducing the MySQL large table optimization solution

Sysbench performance stress test Report:

  • PloarDB 4-core 16G 2 units

Introducing the MySQL large table optimization solution
Introducing the MySQL large table optimization solution

    ##PloarDB 8-core 32G 2 units

Introducing the MySQL large table optimization solution
Introducing the MySQL large table optimization solution

##3. Migrate historical data of sub-tables to MySQL8.0 X-Engine storage engine

The split business table retains 3 months of data (this is based on the company's needs). Historical data is split into historical database X-Engine storage engine tables on a monthly basis. Why should we choose X-Engine storage engine tables? What are its advantages? ?

  1. Saving costs, the storage cost of X-Engine is about half that of InnoDB
  2. X-Engine tiered storage improves QPS, adopts a hierarchical storage structure, and combines hot data with Cold data is stored in different levels, and the level where the cold data is located is compressed by default.

X-Engine is an online transaction processing (OLTP) self-developed by Alibaba Cloud Database Product Division. Processing) database storage engine.
The X-Engine storage engine is not only seamlessly compatible with MySQL (thanks to the MySQL Pluginable Storage Engine feature), but X-Engine also uses a layered storage architecture. Because the goal is to store large-scale massive data, provide high concurrent transaction processing capabilities and reduce storage costs, in most large data volume scenarios, the opportunities for data to be accessed are uneven, and hot data that is frequently accessed actually accounts for Very rarely, X-Engine divides the data into multiple levels according to the frequency of data access. According to the access characteristics of each level of data, it designs the corresponding storage structure and writes it to the appropriate storage device

  • X-Engine uses LSM-Tree as the architectural basis for hierarchical storage and has been redesigned:
  • The hot data layer and data updates use memory storage, through in-memory database technology (Lock-Free index structure/ append only) improves transaction processing performance.
  • The pipeline transaction processing mechanism parallels several stages of transaction processing, greatly improving throughput.
  • Data with low access frequency is gradually eliminated or merged into the persistent storage layer, and combined with multi-layer storage devices (NVM/SSD/HDD) for storage.
  • A lot of optimizations have been made to the Compaction process that has a large impact on performance:
  • Split the data storage granularity, use the characteristics of relatively concentrated data update hotspots, and reuse data as much as possible in the merge process .
  • Finely control the shape of LSM, reduce I/O and computational costs, and effectively alleviate the space increase during the merger process.
  • Also use more fine-grained access control and caching mechanisms to optimize read performance.

Introducing the MySQL large table optimization solution

4. Parallel query of Alibaba Cloud PloarDB MySQL8.0 version

After splitting the tables, our data volume is still very large Large, it does not completely solve our slow query problem, but only reduces the size of our business tables. For these slow queries, we need to use PolarDB’s parallel query optimization

PolarDB MySQL 8.0 launches the parallel query framework , when the amount of your query data reaches a certain threshold, the parallel query framework will be automatically started, thereby exponentially reducing the query time.
Split the data into different threads at the storage layer, and multiple threads will perform parallel calculations. The results of the pipeline are summarized into the main thread, and finally the main thread does a simple merge and returns it to the user to improve query efficiency.
Parallel Query utilizes the parallel processing capabilities of multi-core CPUs. Taking the 8-core 32 GB configuration as an example, the schematic diagram is as follows.

Introducing the MySQL large table optimization solution

Parallel queries are suitable for most SELECT statements, such as large table queries, multi-table join queries, and queries with large calculation loads. For very short queries, the effect is less noticeable.

Parallel query usage, you can use Hint syntax to control a single statement. For example, when the system turns off parallel queries by default, but you need to speed up a high-frequency slow SQL query, you can use Hint to Specific SQL is accelerated.

SELECT / PARALLEL(x)/ … FROM …; – x >0

SELECT /* SET_VAR(max_parallel_degree=n) */ * FROM … // n > 0

Query test: The database is configured with 16 cores and 32G. The data volume of a single table exceeds 30 million

It was 4326ms before parallel query was added, and it was 525ms after adding it, and the performance was improved by 8.24 times.

Introducing the MySQL large table optimization solution

Introducing the MySQL large table optimization solution

##5. Interactive analysis Hologre

Although we use parallelism for slow queries on large tables Query optimization has improved efficiency, but we still cannot achieve some specific requirements for real-time reports and real-time large screens, and can only rely on big data for processing.

Here we recommend Alibaba Cloud’s interactive analysis Hologre (
https://help.aliyun.com/product/113622.html)

Introducing the MySQL large table optimization solution

6. Postscript

Optimization of tens of millions of large tables is based on business scenarios and at the cost of cost. It is not possible to horizontally split and expand the database right from the start, which will bring problems to operation and maintenance and business. A huge challenge. In many cases, the results may not be good. Whether our database design, index optimization, and table partitioning strategies are in place, we should choose the appropriate technology to implement them based on business needs.

More related free learning recommendations: mysql tutorial(Video)

The above is the detailed content of Introducing the MySQL large table optimization solution. For more information, please follow other related articles on the PHP Chinese website!

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)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 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.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles