Optimization of Mysql database

One: Mysql parameter optimization

1. View mysql parameter maximum connection

Increase the mysql parameter connection (maximum can be set to 12384)

2. View all processes and kill useless ones

3. View currently used connections

show global status like 'max_connections';

Two: Use the query cache to optimize queries

1. Most mysql servers have query caching enabled, which is one of the effective ways to improve performance, and is processed by the mysql engine. When there are many identical queries that are executed multiple times, the query results will be placed. into a cache, so that subsequent identical queries can directly access the cached results without operations.

Therefore, SQL functions like NOW() and RAND() or other such functions will not enable query caching, because the return of these functions will be volatile and volatile. So, all you need is to replace the MySQL function with a variable to enable caching .

For example, select username from table where date='2018-04-25' is better than select username from table where date =CURDATE();

2. Use the explain keyword to detect queries

Using the EXPLAIN keyword can let us know how MySQL processes SQL statements, which can help us analyze the performance bottlenecks of our query statements or table structures; the query results of EXPLAIN will also tell us how the index primary key is used, the data How the table is searched or sorted....etc. The syntax format is: EXPLAIN +SELECT statement;

Use the explain statement

3. Use LIMIT 1 when only one row of data is required
Plus LIMIT 1 can increase performance. The MySQL database engine will stop searching after finding a piece of data, instead of continuing to query the next eligible data record.

 

4. Index the search field
An index is not necessarily a primary key or a unique field. If there is a field in a table that is often used for searching, it needs to be indexed.
When creating an index, you can specify whether the index can contain duplicate values. If not included, the index should be created as a PRIMARY KEY or UNIQUE index. For single-column unique indexes, this guarantees that a single column does not contain duplicate values. For multi-column unique indexes, the combination of multiple values ​​is guaranteed not to be repeated.

 Create normal index/drop index

Create a unique index / delete a unique index (note: to delete a unique index is to delete the field where the index is located)

Create primary key index/delete primary key index

mysql> alter table t_table add primary key(id);

mysql> alter table t_table drop primary key;

The second statement is only used when dropping a PRIMARY KEY index, because a table can only have one PRIMARY KEY index, so there is no need to specify an index name. If no PRIMARY KEY index is created, but the table has one or more UNIQUE indexes, MySQL will drop the first UNIQUE index.
Indexes are affected if a column is removed from the table. For multi-column indexes, if you delete one of the columns, the column will also be deleted from the index. If you drop all the columns that make up the index, the entire index will be dropped.

CREATE INDEX can add a common index or a UNIQUE index to the table.

CREATE INDEX index_name ON table_name (column_list)
CREATE UNIQUE INDEX index_name ON table_name (column_list)

View the index (you can also use show keys from table name);

Non_unique: 0 if the index cannot include duplicate words. 1 if it can.

Seq_in_index: The sequence number in the index, starting from 1.

collation: how the column is stored in the index. In MySQL, there are values ​​'A' (ascending) or NULL (no sort).

 Cardinality: An estimate of the number of unique values ​​in the index. Updates can be made by running ANALYZE TABLE or myisamchk -a. Cardinality is counted against statistics stored as integers, so even for small tables the value does not have to be exact. The larger the cardinality, the greater the chance that MySQL will use that index when doing a union.

sub_part: If the column is only partially indexed, the number of characters indexed. NULL if the entire column is indexed.

NUll: If the column contains NULL, it contains YES. If not, the column contains NO.

Index_type: The index method used (BTREE, FULLTEXT, HASH, RTREE).

Full-text indexing: only applicable to fields of type VARCHAR and Text.
Note: There is a big difference between a full-text index and an ordinary index. If an ordinary index is established, the like is generally used to perform a fuzzy query, which is only valid for the first part of the query content, that is, it is only valid for the query that does not use wildcards. If With wildcards before and after, normal indexing will not work. For full-text indexing, it has its own unique matching method when querying. For example
ALTER TABLE article ADD FULLTEXT ('title', 'content'); When searching, you need to use the following syntax to search:
SELECT * FROM article WHERE MATCH('title', 'content') AGAINST ('query string');
The full-text index that comes with MySql can only be used for data tables whose database engine is MYISAM. If it is another data engine, the full-text index will not take effect. In addition, the full-text index that comes with MySql can only perform full-text retrieval in English, and currently cannot perform full-text retrieval in Chinese. If you need to perform full-text search on text data including Chinese, we need to use Sphinx/Coreseek technology to process Chinese. In addition, when using the full-text index that comes with MySql, if the length of the query string is too short, the expected search results will not be obtained. The default minimum length of words that can be found by the MySql full-text index is 4 characters. Also, if the query string contains stop words, the stop words will be ignored.

 Combined index: Combined index, also known as multi-column index, is to specify multiple field attributes when creating an index. It is somewhat similar to a dictionary directory. For example, when querying the pinyin word 'guo', first look for the letter g, then look up the list of the second letter u in the search range of g, and finally look up the last letter in the range of u. word for o. For example, the combined index (a, b, c), abc are all sorted, b under any segment a is sorted, and c under any segment b is sorted

The effective principle of the combined index is to use it in sequence from front to back. If an index in the middle is not used, the index part before the breakpoint will work, and the index after the breakpoint will not work;
The reason for the breakpoint:
Any one of the preceding indexes does not participate in the query, and all the latter indexes do not take effect.
Any of the preceding index fields are involved in range queries, and the latter will not take effect.
The breakpoint has nothing to do with the position of the index word field in the SQL statement, only whether it exists or not
First create a joint index:

There is no breakpoint in the middle of the three index sequences in the figure below, and they all play a role. Note: Even if the order is disrupted, it does not affect, because the main function of the joint index is whether it is used all, not the order.

In this case, the id plays a role and the status does not play a role. At this time, the name is the breakpoint.

In this case, the id is the breakpoint, and neither the name nor the status has any effect.

In addition: For ordinary indexes, when using like for wildcard fuzzy query, if wildcards are used between the beginning and the end, the index is invalid.

Suppose the keyword of the query content is 'abc'
SELECT * FROM tab_name WHERE index_column LIKE 'abc%'; #Index is valid
SELECT * FROM tab_name WHERE index_column LIKE '%abc'; #Index is invalid
SELECT * FROM tab_name WHERE index_column LIKE '%cba'; #Index is valid
SELECT * FROM tab_name WHERE index_column LIKE '%abc%'; #Index is invalid
When the content of the retrieved field is relatively large and the front and rear parts of the retrieved content are uncertain, it can be changed to full-text index and use a specific retrieval method.
5. When joining a table, use a column of the same type and index it, but make sure that the field type of the join is the same. If it is a string type, it must have the same character set.
6. Do not use order by rand();
7. Set the primary key for each table,
8. Use enum instead of varchar for limited and fixed fields

9. Try not to assign values ​​to null,

10. Set up a fixed-length table, because a fixed-length table is easy to calculate an offset, and a fixed-length field will waste some space, because whether we use a fixed-length field or not, he has to allocate so much space. In addition, use trim to remove spaces when retrieving values.

11. The smaller the column type is set, the faster it is, which reduces the access to the hard disk. If a table has only a few columns (such as dictionary table, configuration table), then we have no reason to use INT as the primary key , it is more economical to use MEDIUMINT, SMALLINT or smaller TINYINT. If we don't need to keep track of time, using DATE is much better than DATETIME. 

12. Choose the right storage engine

 

 

The above is the commonly used Mysql optimization. If you have other optimization methods, please leave a message!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324857244&siteId=291194637