search
HomeDatabaseMysql TutorialMySQL performance optimization to make the database run faster



In the database optimization work, make the data as small as possible so that the table occupies 10% of the space on the hard disk. Keeping the space as small as possible is one of the most commonly used and most effective methods. Because the data is reduced, the read and write speed of the hard disk can be relatively improved, and the content processing of the small table during the query process takes up less system resources. In the same way, if an index is set on a smaller column, the index will occupy less resources. So how can database administrators lose weight for their own data? The author has the following suggestions for this.

Suggestion 1: Null values ​​do not necessarily take up space

Here I will give you some literacy first. Some database administrators believe that null values ​​will not occupy system resources. In fact, this is a wrong understanding. When designing the database, they don't like to set the field's properties to NOT NULL. Let users enter data according to their needs. The author believes that this approach is detrimental to the performance of the database

The author's opinion is that if possible, try to set the column to NOT NULL, that is, no null values ​​are allowed. Doing this can speed up subsequent processing, and at the same time save one bit per column from the perspective of data storage, thereby achieving the purpose of data weight loss. In actual work, if there are situations where users are not required to input data, default fields can also be used to achieve non-empty purposes. For example, in the payroll system, the user's working years can be set to 0 by default instead of blank. Of course, if you really need NULL, there is no way. But as a database engineer, you should try to avoid using NULL values.

Suggestion 2: Use as small a data type as possible

The size of the data type will also affect the size of the underlying table. For example, the two data types MEDIUMINT and INT can be used to save integer data, but the precision they can save is different. But from the perspective of storing data, the former requires about 25% less storage space than the latter. For this reason, do not use INT if MEDIUMINT can be used.

In addition, when defining the data length, it should be as short as possible while meeting the needs. For example, there is a field for employee coding in the salary assessment system. If the enterprise employee code has been determined, it consists of five characters. Then when defining the field, you only need to define the length of 5 characters. This can not only reduce the storage space, but also play a certain data proofreading function. When the code length entered by the user exceeds 5 digits, the data cannot be saved.

Although there are many data types to choose from when saving certain data, you can also define a relatively large number of characters. However, choosing the smallest data type as possible can help reduce data storage space and achieve the purpose of data weight loss. Thereby further improving the performance of the database.

Suggestion 3: The relationship between index and data table size

The author mentioned at the beginning of the article that if an index is set for a relatively small column, the index will also occupy relatively few resources. It can be seen that the index and the size of the data table are also closely related. Setting the right index at the right place and at the right time can also achieve the purpose of data weight loss.

Normally, each data table may have multiple indexes, but there is often only one primary index. For this reason, the primary index of each table should be kept as short and concise as possible. This can help the database identify it faster.

Another example is to index the prefix as much as possible. For example, if you have a table now, you need to set an index on a certain column. And this column has a characteristic, that is, it has a unique prefix on the first few characters. If this is the case, it would be better to index this prefix tightly, rather than all of them. In the MySQL database, it is supported to create an index on the leftmost part of a character column. This means that the database will split a field into two parts according to certain rules. If the data in the front part can remain unique after splitting, then you only need to set an index on the front part, and there is no need to set an index on the data in the entire field. This can undoubtedly reduce the resources occupied by the index and achieve the purpose of weight loss. Shorter indexes provide faster query speeds. Because they take up less hard drive space, and they will save more accesses in the index cache. This reduces the number of hard disk searches and improves query efficiency.

The last thing to note is that indexes cannot be abused. Using indexes can indeed improve data processing capabilities, but indexes also bring additional overhead. Only when the benefits are greater than the overhead, using indexes can improve database performance. Otherwise, it will have the opposite effect. For example, if a table needs to be stored quickly, if too many indexes are set on the table, the indexes will have side effects. In this regard, the author suggests that if a table is accessed mainly through a combination of search columns, it is best to set only one index for them. Of course, this index part should be the most commonly used column in daily work. As a last resort, if you need to use multiple indexes, it is best to use columns with more copies to get better index compression. This reduces the increased resource consumption caused by using multiple indexes.

Suggestion 4: You still can’t save where you need to be “full”

A woman should be thinner where she should be thin, and plump where she should be plump. In fact, the same is true for databases. Wherever you can save hard drive space, save it. And what cannot be saved cannot be streamlined in order to lose weight. Sometimes this can backfire.

The author takes Varchar as an example. As in MyISAM, if you don't have any variable-length columns, it's better to use fixed-size data types. Although fixed-length data types are used, a certain amount of storage space is often wasted. Because if the data entered by the user is insufficient and a fixed length is used, the data will still be stored at this fixed length. But in this case, if you can use a fixed length, you still have to use a fixed length. Because in this case, although a certain amount of hard disk space will be wasted, it can improve the data query speed.

It can be seen that weight loss of data cannot improve database performance under any circumstances. This is like saving money to increase revenue, and this saving should be saved on the cutting edge. Otherwise, not only will you not be able to save money, but you will also shoot yourself in the foot. In layman's terms, you should be thinner where you should be thin, and be plump where you should be plump. Just remember this sentence.

Suggestion 5: Split the table to achieve weight loss

When ants are moving food, if a piece of food is too large to move, the ants may divide the piece of food until it can be moved. This is the principle of dividing the cake. In fact, this phenomenon is often common in daily work. For example, if we have a database table, if there are a lot of records in it, the table's allowed speed will be very slow. In this case, the table can be divided into multiple workbooks based on certain rules. For example, there is now a copy of the attendance information of corporate employees. When querying, sorting, and counting this table, the waiting time is very long. At this time, you can divide it into different workbooks according to departments, and then perform relevant data analysis on them. Although the workload will be larger at this time, the processing speed will be much faster

According to this principle, when optimizing the database, a large table that is often scanned can be divided into 2 or more The representation is very helpful. For example, in my daily work, I now have a dynamic format data table, and when this data is using a scan table, I will use this to find the relevant rows in a relatively small static format table.

Through the splitting of this table, a large cake can be divided into several smaller cakes to facilitate subsequent data statistics and analysis. Of course, the quality of this effect is directly related to the rules of this split. Regarding how to split the table to achieve the desired effect, this is another relatively big topic. Due to the limited space here, the author will not give too much explanation. Perhaps in subsequent articles, the author will expand on this proposition and give you a detailed explanation.

The above is the content of MySQL performance optimization to make the database run faster. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
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
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.