Home Database Mysql Tutorial Some experience in MySQL performance optimization

Some experience in MySQL performance optimization

Apr 18, 2019 am 09:40 AM
mysql performance optimization

Optimize your queries for queries

Most MySQL servers have query caching enabled. This is one of the most effective ways to improve performance, and it's handled by the MySQL database engine. When many of the same queries are executed multiple times, these query results will be placed in a cache, so that subsequent identical queries do not need to operate the table but directly access the cached results.

The main problem here is that for programmers, this matter is easily overlooked. Because some of our query statements will cause MySQL not to use the cache. Please look at the following example:

// 查询缓存不开启
$r = mysql_query("SELECT username FROM user WHERE     signup_date >= CURDATE()");
// 开启查询缓存
$today = date("Y-m-d");
$r = mysql_query("SELECT username FROM user WHERE signup_date >= '$today'");
Copy after login

The difference between the above two SQL statements is CURDATE(). MySQL's query cache does not work on this function. Therefore, SQL functions like NOW() and RAND() or other similar functions will not enable query caching, because the returns of these functions are volatile. So, all you need is to replace the MySQL function with a variable to enable caching.

Learn to use EXPLAIN

Using the EXPLAIN keyword can let you know how MySQL processes your SQL statement.

select id, title, cate from news where cate = 1
Copy after login

If you find that the query is slow, then add an index on the cate field, it will speed up the query

Use LIMIT 1 when there is only one row of data

Sometimes you only need one piece of data when querying the table, so please use limit 1.

Use indexes correctly

Indexes are not necessarily for primary keys or unique fields. If there is a field in your table that you will always use for searches, photos, and conditions, then please create an index for it.

Don’t ORDER BY RAND()

A very inefficient random query.

Avoid SELECT *

The more data is read from the database, the slower the query will become. Moreover, if your database server and WEB server are two independent servers, this will also increase the load of network transmission. You must develop a good habit of taking whatever you need.

Use ENUM instead of VARCHAR

The ENUM type is very fast and compact. In fact, it holds a TINYINT, but it appears as a string. In this way, it becomes quite perfect to use this field to make some choice lists.

If you have a field, such as "gender", "country", "ethnicity", "status" or "department", and you know that the values ​​of these fields are limited and fixed, then you should Use ENUM instead of VARCHAR.

Using NOT NULL

Unless you have a very specific reason to use NULL values, you should always keep your fields NOT NULL. This may seem a bit controversial, please read on.

First of all, ask yourself what is the difference between "Empty" and "NULL" (if it is INT, that is 0 and NULL)? If you feel there is no difference between them, then you should not use NULL. (Did you know? In Oracle, NULL and Empty strings are the same!)

Don’t think that NULL does not require space, it requires additional space, and when you compare, your The procedure will be more complex. Of course, this does not mean that you cannot use NULL. The reality is very complicated, and there will still be situations where you need to use NULL values.

The following is excerpted from MySQL’s own documentation

“NULL columns require additional space in the row to record whether their values ​​are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.”

IP address is stored as UNSIGNED INT

Many programmers will create a VARCHAR(15) field to store the IP in the form of a string instead of an integer IP. If you use an integer to store it, it only takes 4 bytes, and you can have fixed-length fields. Moreover, this will bring you advantages in querying, especially when you need to use WHERE conditions like this: IP between ip1 and ip2.

We must use UNSIGNED INT, because the IP address will use the entire 32-bit unsigned integer

A fixed-length table will be faster

If all fields in a table are "fixed-length", the entire table is considered "static" or "fixed-length". For example, there are no fields of the following types in the table: VARCHAR, TEXT, BLOB. As long as you include one of these fields, the table is no longer a "fixed-length static table" and the MySQL engine will process it in another way.

Fixed length tables will improve performance because MySQL will search faster. Because these fixed lengths make it easy to calculate the offset of the next data, reading will naturally be faster. . And if the field is not of fixed length, then every time you want to find the next one, the program needs to find the primary key.

Also, fixed-length tables are easier to cache and rebuild. However, the only side effect is that fixed-length fields waste some space, because fixed-length fields require so much space regardless of whether you use them or not.

vertical split

"Vertical splitting" is a method of turning the tables in the database into several tables by columns, which can reduce the complexity of the table and the number of fields, thereby achieving optimization purposes. It should be noted that you will not join the tables formed by these separated fields frequently. Otherwise, the performance will be worse than when not divided, and it will be an extreme drop. .

Split a large DELETE or INSERT statement

If you execute a large DELETE or INSERT query on an online website, you need to be very careful to avoid The operation causes your entire website to stop responding. Because these two operations will lock the table, once the table is locked, no other operations can enter.

Apache will have many child processes or threads. Therefore, it works quite efficiently, and our server does not want to have too many child processes, threads and database links. This takes up a lot of server resources, especially memory.

If you lock your table for a period of time, such as 30 seconds, then for a site with a high number of visits, the access processes/threads accumulated in these 30 seconds, database links, and open The number of files may not only cause your WEB service to crash, but may also cause your entire server to hang up immediately.

Smaller columns will be faster

For most database engines, hard disk operations may be the most significant bottleneck. So, making your data compact can be very helpful in this situation because it reduces access to the hard drive.

Choose the right storage engine

There are two storage engines in MySQL, MyISAM and InnoDB. Each engine has advantages and disadvantages.

MyISAM is suitable for applications that require a large number of queries, but it is not very good for a large number of write operations. Even if you just need to update a field, the entire table will be locked, and other processes, even the reading process, cannot operate until the reading operation is completed. In addition, MyISAM is extremely fast for calculations such as SELECT COUNT(*).

The trend of InnoDB will be a very complex storage engine, and for some small applications, it will be slower than MyISAM. The other reason is that it supports "row locking", so it will be better when there are more write operations. Moreover, it also supports more advanced applications, such as transactions.

The above is the detailed content of Some experience in MySQL performance 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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 optimize MySQL connection speed? How to optimize MySQL connection speed? Jun 29, 2023 pm 02:10 PM

How to optimize MySQL connection speed? Overview: MySQL is a widely used relational database management system that is commonly used for data storage and management in a variety of applications. During development, optimization of MySQL connection speed is critical to improving application performance. This article will introduce some common methods and techniques for optimizing MySQL connection speed. Table of Contents: Use connection pools to adjust connection parameters and optimize network settings. Use indexing and caching to avoid long idle connections. Configure appropriate hardware resources. Summary: Use connection pools.

Analysis of project experience on MySQL database backup and recovery performance optimization Analysis of project experience on MySQL database backup and recovery performance optimization Nov 02, 2023 am 08:53 AM

In the current Internet era, the importance of data is self-evident. As one of the core components of Internet applications, database backup and recovery work is particularly important. However, as the amount of data continues to increase and business requirements become increasingly complex, traditional database backup and recovery solutions can no longer meet the high availability and high performance requirements of modern applications. Therefore, optimizing the backup and recovery performance of MySQL database has become an urgent problem that needs to be solved. In practice, we have adopted a series of project experiences to effectively improve MySQL data

How to use PHP's PDO class to optimize MySQL performance How to use PHP's PDO class to optimize MySQL performance May 10, 2023 pm 11:51 PM

With the rapid development of the Internet, MySQL database has become the core data storage technology for many websites, applications and even enterprises. However, with the continuous growth of data volume and the sharp increase in concurrent access, MySQL's performance problems have become increasingly prominent. PHP's PDO class is also widely used in the development and operation of MySQL because of its efficient and stable performance. In this article, we will introduce how to use the PDO class to optimize MySQL performance and improve the database's response speed and concurrent access capabilities. 1. Introduction to PDO class

MySQL performance optimization practical guide: in-depth understanding of B+ tree indexes MySQL performance optimization practical guide: in-depth understanding of B+ tree indexes Jul 25, 2023 pm 08:02 PM

MySQL Performance Optimization Practical Guide: In-depth Understanding of B+ Tree Indexes Introduction: As an open source relational database management system, MySQL is widely used in various fields. However, as the amount of data continues to increase and query requirements become more complex, MySQL's performance problems are becoming more and more prominent. Among them, the design and use of indexes are one of the key factors affecting MySQL performance. This article will introduce the principle of B+ tree index and show how to optimize the performance of MySQL with actual code examples. 1. Principle of B+ tree index B+ tree is a

How to improve MySQL performance by using dynamic SQL statements How to improve MySQL performance by using dynamic SQL statements May 11, 2023 am 09:28 AM

In modern applications, the MySQL database is a common choice. However, as data volumes grow and business needs continue to change, MySQL performance may suffer. In order to maintain the high performance of MySQL database, dynamic SQL statements have become an important technical means to improve the performance of MySQL. What is a dynamic SQL statement? A dynamic SQL statement refers to the technology of generating SQL statements by a program in an application. In layman's terms, it means treating SQL statements as strings. For large applications,

MySQL performance optimization: Master the features and advantages of TokuDB engine MySQL performance optimization: Master the features and advantages of TokuDB engine Jul 25, 2023 pm 07:22 PM

MySQL performance optimization: Master the characteristics and advantages of the TokuDB engine Introduction: In large-scale data processing applications, performance optimization of the MySQL database is a crucial task. MySQL provides a variety of engines, each with different features and advantages. This article will introduce the features and advantages of the TokuDB engine and provide some code examples to help readers better understand and apply the TokuDB engine. 1. Characteristics of TokuDB engine TokuDB is a high-performance, high-compression rate storage engine.

How to improve MySQL performance by vertically partitioning tables How to improve MySQL performance by vertically partitioning tables May 10, 2023 pm 09:31 PM

With the rapid development of the Internet, the scale of data continues to expand, and the demand for database storage and query efficiency is also increasing. As the most commonly used open source database, MySQL's performance optimization has always been the focus of developers. This article will introduce an effective MySQL performance optimization technology - vertical partition table, and explain in detail how to implement and apply it. 1. What is a vertical partition table? Vertically partitioned tables refer to dividing a table according to column characteristics and storing different columns on different physical storage devices to improve query efficiency.

Data table size management skills in MySQL Data table size management skills in MySQL Jun 15, 2023 am 09:28 AM

As a lightweight relational database management system, MySQL database is widely used in Internet applications and enterprise-level systems. In enterprise-level applications, as the amount of data increases, the size of data tables also continues to increase. Therefore, effective management of data table sizes is crucial to ensuring the performance and reliability of the database. This article will introduce data table size management techniques in MySQL. 1. Data table division As the amount of data continues to increase, the size of the data table also continues to increase, which will cause database performance to decrease and query operations to slow down.

See all articles