Overview of MySQL optimization strategy, one of PHP database programming

This article briefly describes the MySQL optimization strategy of PHP database programming. Share with you for your reference, as follows:

I saw an article a few days ago that the bottleneck of PHP is not in PHP itself in many cases, but in the database. We all know that in the development of PHP, the addition, deletion, modification, and checking of data is the core. In order to improve the operating efficiency of PHP, programmers not only need to write logically clear and efficient code, but also be able to optimize query statements. Although we are powerless in the speed of reading and writing the database, with the help of some database extensions like memcache, mongodb, redis and other data storage servers, PHP can also achieve faster access speeds, so understand and learn These extensions are also very necessary. This article first talks about MySQL's common optimization strategies.

Click here to join my penguin group

A few MySQL tips

1. The keywords in SQL statements are best written in uppercase. First, it is easy to distinguish between keywords and operation objects. Second, when SQL statements are executed, MySQL will convert them to uppercase. Manual uppercase can increase query efficiency ( Although small).

2. If we add or delete data rows in the database, the data ID will be too large. Use ALTER TABLE tablename AUTO_INCREMENT=Nit to make the self-increment ID count from N.

3, to add type int ZEROFILLattribute data can be automatically fill 0

4. When importing large amounts of data, it is best to delete the index first, insert the data, and then add the index, otherwise, mysql will spend a lot of time updating the index.

5. When creating a database to write sql statements, we can create a file with a suffix of .sql in the IDE. The IDE will recognize the sql syntax, making it easier to write. More importantly, if your database is lost, you can still find this file and use /path/mysql -uusername -ppassword databasename < filename.sqlit in the current directory to execute the sql statement of the entire file (note that -u and -p are followed by the username and password, no spaces).

Optimization in database design

1. The database design conforms to the third paradigm, and there is a certain amount of data redundancy for query convenience.

2. Select the data type priority int> date, time> enum, char> varchar> blob. When selecting the data type, consider replacing. For example, the ip address can be converted to unsign int type with the ip2long() function for storage.

3. For the char(n) type, the value of n should be as small as possible when the data is complete.

4. Using the partition command to partition a single table when building a table can greatly improve query efficiency. MySQL supports RANGE, LIST, HASH, and KEY partition types. Among them, RANGE is the most commonly used. The partitioning method is:

CREATE TABLE tablename{
    
    
}ENGINE innodb/myisam CHARSET utf8 //选择数据库引擎和编码
PARTITION BY RANGE/LIST(column),//按范围和预定义列表进行分区
PARTITION partname VALUES LESS THAN /IN(n),//命名分区并详细限定分区的范围

5. Pay attention to the difference between innodb and myisam when choosing a database engine.

Storage structure : MyISAM is stored as three files on the disk. All InnoDB tables are stored in the same data file, generally 2GB

Transaction support : MyISAM does not provide transaction support. InnoDB provides transaction support transactions.

Table lock difference : MyISAM only supports table-level locks. InnoDB supports transactions and row-level locks.

Full-text indexing : MyISAM supports full-text indexing of FULLTEXT type (not applicable to Chinese, so use sphinx full-text indexing engine). InnoDB does not support.

The specific number of rows in the table : MyISAM saves the total number of rows in the table, and the query count(*) is fast. InnoDB does not save the total number of rows in the table and needs to be recalculated.

Foreign key : MyISAM does not support. InnoDB support

Index optimization

1. Innodb is a clustered index. When storing the index, there must be a primary key. If not specified, the engine will automatically generate a hidden primary key and generate a primary index. The physical address of the primary key is stored in the index. The data is stored by the primary key. When using the index, first find the main index, and then find the data under the main index.

The advantage is that the search through the primary key is very fast, but the disadvantage is that the secondary index will be slower, because you need to find the primary index through the secondary index (the position of the primary index in the secondary index.), and then find the data through the primary index. And if the primary key is irregular, you need to move more data blocks when inserting a new value, which will affect efficiency, so try to use a regularly increasing int type as the primary key. Also, because the data is placed immediately after the primary key, if there are columns (text/blob) with a particularly large amount of data in the data, innodb queries will skip many data blocks, which will also cause slowness.

2. The indexes of myisam are all the same and uniformly point to the address of each row on the disk, and they are all lightweight pointer data. The disadvantage is that the establishment of each index is not through the primary key, and the query is not as fast as the clustered index to find the primary key. But because it stores the address, the comparison changes when inserting a new value.

3. When performing multi-condition queries, when creating indexes for multiple conditions separately, when executing SQL queries, MySQL will only select the closest index to use, so if you need multi-condition queries, create a joint index, even if it will cause data redundancy More.

BTREE establishment method of joint index : index the first condition, index the second condition in the BTREE area of ​​the first index, and so on, so when using the index, do not use the first condition and use the second This condition will not use the joint index. When using the index, the conditions must be ordered and used in sequence.

4. The length of the index also has a great influence on the query. We should try to create a short index length. We can use the query column
SELECT COUNT(DISTINCT LEFT(column)) / COUNT(*) FROM tablename to test the selection when indexing the column column Different lengths, how big the coverage of the index is, we choose n lengths close to saturation to create an index
ALTER TABLE tablename ADD INDEX (column(n)); to index the first n characters of a column. If the first n characters are the same, we can even reverse the storage of the string, and then create an index.

5. The maintenance method for index fragmentation caused by frequent modification: ALTER TABLE tablename ENGINE oldengine; that is, apply the table storage engine again to automatically maintain it; you can also use the OPTIMIZE tablename command for maintenance.

Data query optimization

Try to minimize database operations, and try not to perform data operations at the database level when there are queries, but return to the PHP script to manipulate data to reduce database pressure.

Once a database performance problem is discovered, it must be resolved in time. Generally, slow query logs are used to record queries that are "slow", EXPLAIN is used to analyze query and index usage, and PROFILE is used to analyze specific resource consumption during statement execution.

Slow query log:

1. Add under [mysqld] in my.ini or my.cnf

  • slow_query_log_file=/path //Set the log storage path

  • long_query_time=n //Set if the statement execution time reaches n seconds, it will be recorded

2. Then set SET slow_query_log='ON' in MySQL to enable slow query.

3. After recording the log, we use the mysqldumpslow filename in the /bin/ directory to view the log. The common parameters are as follows:

  • -g pattern uses regular expressions

  • -tn returns the first n data

  • -sc/t/l/r Sort by record times/time/query time/returned record number

EXPLAIN statement

How to use it, add EXPLAIN before the query statement to be executed

EXPLAIN SELECT * FROM user;

The result is as follows:

Insert picture description here

The following is an explanation of each item:

id The id of the query statement, simple query is meaningless, and the order of query execution can be seen when multiple queries

The type of query statement executed by select-type corresponds to multiple queries, such as simple/primary/union, etc.

Tabel query statement query data table

type The type of data obtained, the common type efficiency from high to low is null>const>eq_ref>ref>range>index>all

possible-keys : possible indexes

Index used by key

key_len index length

Which column ref uses together with the index to select from the table.

Rows finds the approximate number of rows of data to be scanned, you can see the pros and cons of the index

Extra common ones are

Use filesort to sort the files after querying the data, which is slower and needs to optimize the index

using where reads the entire row of data and then judges and filters whether it meets the where conditions

Using index index coverage, that is, the target data is already stored in the traction, and the index is directly read, very quickly.

PROFILE

Use SELECT @@frofiling to view the on state of PROFILE.

If it is not turned on, use SET profiling=1 to turn it on.

After opening, and then execute the query statement, MySQL will automatically record the profile information.

Use show profiles to view all sql information, the result is Query_ID Duration Query three columns of results, which are query ID, time spent and sql statement used.

We can use

SHOW PFROFILE [type[,type]][FOR QUREY Query_ID][Limit rwo_count [OFFSET offset]]

Common types include ALL (all), BLOCK IO (display IO related overhead), CPU (CPU overhead), MEMORY (memory overhead), etc.

Optimized for large storage

Database master-slave replication and read-write separation

1. The master records the changes in the binary log, and the slave copies the binary of the master to its relay log, and then returns the data to its own data to achieve the purpose of copying the data of the master server.

Master-slave replication can be used for: database load balancing, database backup, read-write separation and other functions.

2. Configure the master server master

修改my.ini/my.conf

[mysqld]

log-bin=mysql-bin //启用二进制日志

server-id=102 //服务器唯一ID

3. Configure the slave

log-bin=mysql-bin //启用二进制日志

server-id=226 //服务器唯一ID

4. Authorize the slave server on the master server

GRANT REPLICATION SLAVE ON *.* to 'slavename'@'IP' identified by 'root'

5. Use on the slave server

change master to 
master_host="masterip",
master_user="masteruser",
master_password="masterpasswd";

6. Then use the start slave command to start master-slave replication.

Don't forget to restart the server after each configuration modification, and then you can use show master/slave status to view the master/slave status on the master-slave server.

The realization of database read-write separation depends on MySQL middleware, such as mysql_proxy, atlas, etc. By configuring these middlewares to separate the master and slave servers for reading and writing, the slave server assumes the responsibility of being read, thereby reducing the burden on the master server.

Database sharding

When the amount of data in the data table in the database is very large, whether it is the pressure of indexing or caching, etc., the database is sharded to be stored in multiple database servers or multiple tables to reduce the query pressure.

There are vertical segmentation, horizontal segmentation and combined segmentation .

Vertical segmentation : When there are a lot of data tables, split the tables in the database that are closely related (such as the same module, often connected and query) and put them on different master and slave servers.

Horizontal segmentation : When there are not many tables and the amount of data in the table is very large, in order to speed up the query, algorithms such as hash can be used to divide a data table into several and put them on different servers to speed up the query . The difference between horizontal partitioning and data table partitioning lies in its storage media.

Joint segmentation : In most cases, the amount of data in the data table and the table is very large, and then joint segmentation is required, that is, vertical and horizontal table divisions are performed at the same time, and the database is divided into a distributed matrix for storage.

Each of these database optimization methods can be used to write an article, which can be described as extensive and profound. After understanding and remembering these methods, you can select and optimize purposefully when necessary to achieve high database efficiency.

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/109265778