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)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

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: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

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.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

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.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

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

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.

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.

See all articles