MySQL optimization (7): Other Considerations

DDL execution

Online server to perform DDL, update table structure, need to be careful, structural changes will lead to a full table is an exclusive lock (new version has improved)

Avoid this, use the COPY strategy, rather than by directly executing the ALTER TABLE statement

Idea: create a new table, to meet the new requirements, the old table data one by importing new tables, and can perform other tasks on the table, the import process other tasks are recorded in the logs, the import is complete according to the log update the new table, and finally a new table replace the old table (Java code, etc. to achieve)

 

Data import statement

When restoring data, it requires a lot of data import

Ideas: When importing, disable indexes and constraints; After the import is complete, and then turn indexes and constraints, to create a one-time index

It is possible to use the statement: Disable Indexing and open index

ALTER TABLE [table-name] DISABLE KEYS;
ALTER TABLE [table-name] ENABLE KEYS;

For InnoDB storage engine, multiple SQL can be placed in a transaction completed 

SQL compiler can use the number of pre-compiled programs prepare import operation is performed to reduce the same structure

 

Mass Page LIMIT

Try not to use a large OFFSET, such as LIMIT 10000,10

Idea: to make use of filtering conditions, the completion of the screening data, instead of skipping data have been queried by OFFSET

 

SELECT *

Do not use SELECT *, but the use of a specific field; this performance impact is not great, but this is the norm

 

ORDER BY RAND () do not use

This sentence is in random order, if traffic is to be implemented as far as possible in the randomly selected server code, to retrieve the primary key randomly generated

 

Single-table and multi-table queries

Try using a single-table query instead of multi-table queries, the following reasons:

(1) single table java code calculated pressure, multi-table calculated pressure in the database

(2) a multi-table queries a table in order to perform, and finally merge result

(3) multi-table queries will increase the lock time, reduce program concurrency

 

COUNT(*)

MYISAM automatically stored value COUNT (*) is, INNODB no internal counter, performing the phrase relatively slow

Solution: Extra create a table, the value COUNT (*) are recorded

In addition:

COUNT (ID) is represented by: Statistical ID field is not the number of NULL

COUNT (1) is represented by: count the number of records, and COUNT (*) is almost the same

 

LIMIT 1

If you are sure of only one result found, then adding LIMIT 1 will improve performance

 

Slow query log

Used to record the execution time exceeds a certain critical value of SQL logs, slow query the user to quickly locate

Open the log:

Query whether to open

SHOW VARIABLES LIKE 'slow_query_log';

Open

SET GLOBAL slow_query_log=On;

Slow.log file will be created in the data directory after opening

View critical time, the default is ten seconds long

SHOW VARIABLES LIKE 'long_query_time';

Setting critical time

SET long_query_time = 0.5;

Once than 0.5 seconds, will be automatically recorded

 

PROFILE statistical information

A detailed record of the SQL statement execution time tool

Open PROFILE:

SET profiling=On;

Use the command to view the results

SHOW PROFILES;

Specific details of a bar

SHOW PROFILE FOR QUERY [query_id]

 

A typical server configuration

(1) MAX_CONNECTIONS: MySQL current maximum number of connections allowed 151

(2) table_open_cache: table handle caching 2000

(3) KEY_BUFFER_SIZE: 8388608 index buffer size

(4) innodb_buffer_pool_size: INNODB storage engine cache pool size, more important, as far as possible to set a larger

(5) innodb_file_per_table: INNODB separate table file table, ON represents a table corresponds to a table file, otherwise, all files stored tables together INNODB

 Above all depends on the actual configuration of the operating environment

 

mysqlslap stress testing tool

This is a tool that comes with mysql, an EXE, run directly, specifically Baidu, which is the DBA's job, not the programmer's work

 

 

Finally, I recommend an article written by very good

https://www.cnblogs.com/jajian/p/9758192.html

Guess you like

Origin www.cnblogs.com/miaoweiye/p/12519663.html