An article tells you common database optimization

First, the design:

Field avoid null values appear, and query optimization null value is difficult to take up extra space index, suggesting default number 0 instead of null.
Try to use INT instead of BIGINT, if the non-negative plus UNSIGNED (the value of such capacity will be doubled), of course, can use TINYINT, SMALLINT, MEDIUM_INT better.
Instead of using a string or an integer enumeration type
used TINYINT instead ENUM: ENUM add a new value to DDL operations
make use instead TIMESTAMP DATETIME: both occupying byte range indicated different
single table do not have too many fields, it is recommended within 20
with integral memory for IP
use varchar (20) stores phone number, do not use the whole: varchar Fuzzy queries may be, for example, like '138%'
use may be able to save the data of the minimum data type integer <date, time <char, varchar <blob
minimize the use of text, non-preferably with a sub-table is not
two, the index

The index is not possible, to create targeted based on the query, consider the query frequently listed in the where, group by, order by, on columns that appear in indexing clause, it can be used to check whether according to EXPLAIN full scan table index or
a small length of column, index fields as small as possible, because the database is stored in units of pages, one page of data will accommodate better
dispersion degree (multiple different values) are shown, the discharge in the joint index front. View dispersion, is achieved by different statistical column values, count, the higher the degree of dispersion:
Should be avoided fields NULL value judgment in the WHERE clause, it will cause the engine to give up using the index and full table scan
values distribution is sparse field is not suitable for building an index, such as "sex" this is only the value of field twenty-three
character field to build only the prefix index
character key field is best not to call the shots
without foreign key constraint by the program to ensure
as far as possible not UNIQUE, by the program ensure restraint
idea to maintain order and query when using a multi-column index is consistent, delete unnecessary single-column index
of the most left-prefix understand the principle of the composite index, avoid duplication index, if the establishment of (a, b, c), equivalent to the establishment of a (a), (a, b ), (a, b, c)

Third, the need to pay attention to optimize the preparation of sql

Use the limit of record results were limited
to avoid select *, the fields will need to find a list to
use the connection (join) to replace the sub-query
splitting large delete or insert statements
may be slower SQL find out by opening the slow query log
do not do arithmetic column: SELECT id WHERE age + 1 = 10, any operation on the columns will cause a table scan, which includes a database tutorial function, evaluate expressions, etc., when a query to the right of the equal sign moves the operation to the extent possible
sql statement as simple as possible: only a sql a cpu operation; small split large statement statement, reducing lock time; sql a large entire library can be blocked
OR rewrite the iN: OR efficiency level is n, iN is the efficiency of log ( n) level, in the number of recommended control in less than 200
do not function and triggers in applications implement
avoid% xxx type queries
less JOIN: If you want to JOIN query, the field must be the same type of JOIN, and indexed using the comparing the same type, such as a '123' and '123' ratio, 123 and 123 than
to avoid the use in the WHERE clause! = or <> operator, otherwise the engine to give up using the index and a full table Scanning
For continuous values, do not use BETWEEN IN: SELECT id FROM t WHERE num BETWEEN 1 AND 5
list data do not take full table, to use LIMIT to pagination, page numbers are not too big
to be updated ...

Author: Wang Wei
Source: CSDN
Original: https://blog.csdn.net/qq_38234594/article/details/91451622
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/PyhtonChen/article/details/94618165