Table of Contents
Optimize the table field structure" >Optimize the table field structure
Home Database Mysql Tutorial Summarize the most basic operations of MySQL optimization

Summarize the most basic operations of MySQL optimization

Nov 15, 2021 pm 03:47 PM
mysql optimization

Optimization ideas

The detailed MySQL optimization steps are as follows:

  • Check the data table structure and improve the imperfect design
  • Run the main business and collect commonly used database query SQL
  • Analyze the query SQL, split it appropriately, add indexes, etc. to optimize the query
  • While optimizing SQL, optimize the code logic
  • Add local cache and redis cache

Try not to use NULL values

Because when creating a table, if you do not set a default value for the created value, MySQL Will be set to NULL by default. So why is it not good to use NULL?

  • NULL makes index maintenance more complicated. It is strongly recommended to set NOT NULL
  • NOT IN on the index column. , != and other negative conditional queries will always return empty results when there is a NULL value, and the query is prone to errors in the
  • NULL column An extra byte is needed as a flag to determine whether it is NULL
  • When using NULL, it may not be of the same type as other values ​​in the column, causing problems. (It behaves differently in different languages)
  • MySQL has difficulty optimizing queries for columns that can be NULL

So for those fields that were lazy before, Manually set a default value, empty string, and add 0.

Although this method does not improve the performance of MySQL much, it is a good habit, and seeing the big from the small, don't ignore these details.

Add index

For frequently queried fields, please add indexes. The query speed with and without indexes differs by ten times or more.

  • Generally speaking, each table needs to have a primary key idField
  • Fields commonly used for queries should be indexed
  • # For fields of type ##varchar, when creating an index, it is best to specify the length
  • When the query has multiple conditions, the conditions with indexes are preferred
  • Like
  • LIKE Conditional fuzzy search is invalid for field indexes, and additional keyword indexes need to be established to solve the problem
  • Please try not to constrain the relationship between tables at the database level, The dependencies between these tables should be resolved at the code level
When there are constraints between tables, although the SQL statements for additions, deletions and queries become simpler, the negative effects are insertions, etc. Constraints will be checked when operating the database (although constraints can be ignored manually), which is equivalent to writing some business logic to the database layer, which is inconvenient to maintain.

Do not use the string type for data that can be represented by integers in the database. Should you use

varchar or charDepends on the possible values ​​of the field.

This kind of optimization is often not feasible when there is a large amount of data in the database. It is best to design it before the database design.

    For those columns with very limited possible values, use
  • tinyint instead of VARCHAR,
      For example, to record the mobile device platform, there are only two Value: android, ios, then you can use 0 to represent android and 1 to represent ios. This column must be commented
    • Why not use
    • ENUM? ENUM Difficulty in expansion. For example, the mobile platform later added an ipad, which would be confusing, but tinyint just adds a 2, and ENUM is very strange to handle in the code. Whether it is treated as an integer or a string varies from language to language.
    • In this method, the meaning of each value must be stated in the database comment or code
  • For those fixed-length strings, you can use
  • char, such as zip code, is always 5 digits
  • For those strings with unknown length, use
  • varchar
  • and do not abuse
  • bigint, such as records For the id field of the table with the number of articles, just use int. The upper limit of 2.1 billion articles is enough.
  • Properly break the database paradigm and add redundant fields to avoid queries. When querying table connection
, the

int type must be faster than varchar, because the comparison of integers can be achieved by directly calling the underlying operator, while the string type The comparison is done character by character.

Fixed-length data query is faster than variable-length data query, because the offset between fixed-length data and data is fixed, and it is easy to calculate the offset of the next data. For variable-length data, one more step is needed to query the offset of the next data. but. Fixed-length data may waste more storage space.

Large table splitting

For those tables whose data volume may exceed 5 million in the near future or grow rapidly, be sure to split tables vertically or horizontally in advance. When the data After the volume exceeds one million, the query speed will drop significantly.

Try to finalize the plan for sub-database and sub-table in the early stage of database design, otherwise it will greatly increase the code complexity and be difficult to change later.

Vertical table partitioning is based on external variables such as dates, and horizontal table partitioning is based on certain field relationships in the table, using hash mapping to divide the table into equal parts.

The prerequisite for sub-database and table sub-database is that before executing the query statement, you already know which sub-database and which sub-table the data to be queried may fall into.

Optimizing query statements

This is the initiator of many system database bottlenecks.

  • Please try to use simple queries and avoid using table links
  • Please try to avoid full table scans. Statements that will cause full table scans include but are not limited to:
    • where clause condition is always true or empty
    • Use LIKE
    • Use inequality operator (<>, !=)
    • Query the column containing is null
    • When using or
  • on non-index columns to query with multiple conditions, please put Put simple query conditions or index column queries in front
  • Please try to specify the columns you need to query, don't be lazy to use select *
    • If you don't specify, on the one hand, redundant data will be returned, occupying bandwidth, etc.
    • On the other hand, when MySQL executes a query, if there are no fields, it will first query the fields in the table structure.
  • Uppercase query keywords are a little faster than lowercase ones
  • Using subqueries will create temporary tables, which will be slightly slower than links (JOIN) and unions (UNION)
  • Try not to use database functions when querying on index fields, which is not convenient for caching query results
  • When there is only one row of data, please use LIMIT 1. If there is too much data, please set LIMIT appropriately and perform paging query
  • Never use ORDER BY RAND(), the performance is extremely low

Add cache

Using caches such as redis and local file cache can greatly reduce the number of database queries. When it comes to caching, you must analyze the data characteristics of your own system and make appropriate choices.

  • For some commonly used data, such as configuration information, etc., it can be placed in the cache.
  • The table structure of the database can be cached locally.
  • The cached data must be Pay attention to timely updates, and set the validity period
  • Increasing cache will definitely increase system complexity, so be sure to pay attention to the trade-off

Check the data table structure

Recommended learning: "mysql video tutorial"

The above is the detailed content of Summarize the most basic operations of MySQL optimization. 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.

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

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.

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 vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles