Mysql performance optimization -10. A typical SQL

1.DDL (Data Definition Language) execution

On the online server, execute DDL, update the structure of the table, this operation requires careful handling.
DDL, structural changes will result in a full table is exclusively locked. If it is still better innodb; At this time table for the maintenance state, the inoperable state. (After 5.6 support Online DDL effect significantly improved, greatly reducing the lock time).
DDL maintain table structure used, is copy strategy:
Task: Add a column, add an index:
idea:
create a new table, meet new structure.
Introducing the old table to the new table data (read operation), introduced one by one. Guarantee less disposable locked content, and can perform other tasks on the table.
During the import process, the new updated operation record table. Complete the record in the form of logs.
After the import is complete. The update log, redo it again in the new table.
The new standard replaces the old song to the table. The application is completed, or rename the database, the view is completed.

2. Data import statement

When restoring data, it requires a lot of data import. It requires some skill to ensure rapid import:

When importing, disable the indexes and constraints after the import is complete, and then turn indexes and constraints, to create a one-time index.

alter table table-name disable keys;
alter table table-name enable keys;

For innodb, transactional storage engine, can be a number of SQL, in a transaction completed:

begin;
多条导入;
commit;

innodb ordering is the primary key, the input data itself is introduced according to the primary sort key, the rate of introduction of relatively fast. Perpare may be used, pre-compiled programs import operation is performed to reduce the number of SQL compiler same structure.

3. Large page number, limit offset, limit

Try to ensure that no large offset, when using the limit.
limit 100000, 10;
idea is to make use of filtering conditions, filtering data is completed, instead of skipping to the query data has been offset.

4.select * less

Try to choose the fields you need.
The problem is not very serious.
ORM framework of our model, are used in select *.

5.order by rand (), do not use

Logic: random order

mysql> select * from t_student order by rand() limit 5;
+--------+------------+-----------+--------+-------+--------------------------------------+----------+
| id     | first_name | last_name | gender | user  | password                             | class_id |
+--------+------------+-----------+--------+-------+--------------------------------------+----------+
| 643727 | 2bc9f      | e6155     |      1 | 602-f | a4787d8d-cce8-4b1a-813e-6a67670b5eef |      353 |
| 583044 | 0077a      | 0b151     |      2 | 5b8-4 | b6a1f25e-962b-47ef-8d02-1017cc4f7433 |      240 |
| 648485 | 62b4d      | 82efc     |      2 | 68c-5 | ca06c084-2f77-4966-a315-b26853efea29 |      191 |
|  85567 | a2911      | 7ffe6     |      2 | a09-b | 71da7a3f-75f6-4c74-b2ac-7c1bff65d4e5 |      114 |
| 360811 | edd74      | 5e815     |      2 | 30b-d | e05b0fae-2e1f-4a7b-8141-a0774644e11b |      508 |
+--------+------------+-----------+--------+-------+--------------------------------------+----------+
5 rows in set (2.98 sec)

The problem is that a large amount of generated random numbers, each record needs to be generated, requires only five records. Unnecessary randomly generated.

Common approach: the application, the primary key generator randomly good use of the database to retrieve the primary key.
mt_rand (min, max)

6. The single-table multi-table queries

Single table: each query table relates only to a
multi-table: subqueries, join queries done by multiple query tables;
to make use of a single table query instead of multi-table queries. Cause of performance problems, reflected in the following aspects of
multi-table query execution: join, subquery, is also a table to do it. Executed, the result of the merger to go.
Calculates the distribution, single-table queries, calculate the pressure application side. Multi-table queries, calculate the pressure on the database.
Multi-table query, the table will increase the lock time. Reduce program concurrent performance.
Single-table queries, increases the complexity of the program. Using an ORM model, the model has a lot of work to complete the single-table process.

7.count (*) problem

Myisam corresponding to no problem, myisam total amount of data will be automatically stored in a table.
For innodb, not the internal counter. If statistics are a constant work. We need to own to complete the statistics:

The number of records in the table, additional record:

id table (unique) count
1 t_student 700000
2 t_calss 1000

count (id), statistical id field is not null number of
count (1), similar to the count (*). Count the number of records.

8.limit 1

If determined retrieve only a recommended. 1 plus limit
limit on a Query. 1 single frame operation, will automatically increase;

Guess you like

Origin blog.csdn.net/weixin_34364071/article/details/90888495