MySQL Performance (a) basic optimization

A, SQL statement optimization

Use mysql slow query log to SQL monitor the efficiency problem:

// slow query log to see whether to open the
show variables like 'slow_query_log';

// Check slow query log storage location
show variables like 'slow_query_log_file';

// turn on slow query log
set global slow_query_log = on;

// Specify slow query log storage location
set global show_query_log_file = '/ var / lib / mysql / homestead-slow.log';

// record does not use the index SQL
SET Global log_queries_not_using_indexes = ON;

// query record of more than 1s SQL
the SET, Ltd. Free Join long_query_time = 1;
slow query log contains:

# User @ Host: root [root ] @localhost [] // host information sql execution of
#Query_time: 0.0000024 Lock_time: 0.00 Rows_sent: 0 Rows_esamined: 0 // sql execution information
SET timestamp = 1402389324 // sql execution time
select * from store; // sql content of

 

The official MySQL slow query log analysis tool of mysqldumpslow:

usage:

// Check the list of parameters
mysqldumpslow -h

// slow query log analysis in the first three relatively slow SQL
mysqldumpslow -t 3 /var/lib/mysql/homestead-slow.log | More

// output style effects
the Count:. 1 Time: 0.00s 0.00s = the Rows = 10.0 Lock
the root [RPPT] @localhost
SELECT * from Store

 

MySQL's slow query log analysis tools pt-Query-Digest (the result of more detailed and comprehensive than mysqldumpslow):

// output to a file
pt-query-digest slow-log > slow_log.report

// to the data table
Pt-Query-Digest slow.log -review \
H = 127.0.0.1, D = Test, P = the root, P = 3306, U = the root, T = query_review \
--create-reviewtable \
- -review-history t = hostname_slow

usage:

// Check the list of parameters
pt-query-digest --help

// slow query log analysis in the first three relatively slow SQL
pt-Query-Digest /var/lib/mysql/homestead-slow.log | More

// output three parts
1. In addition to the display time range of the log, and the total number and the number of different sql sql
2.Response Time: Response time accounting Calls: sql execution frequency
3.sql specific log

 

By slow query log found problematic SQL, the problem is as follows:

1. queries more often and each time SQL queries long occupied
the first several queries are usually analyzed as pt-query-digest

2.IO large SQL (database major bottleneck in IO level)
Note Rows examine item pt-query-digest analysis

3. misses index SQL
Note Rows examine comparative analysis and Rows Send a pt-query-digest.

 

By explain SQL query and analysis implementation plan
explain select customer_id ,, first_name, last_name from customer;

 

 

max () and count () optimization:

// query the last pay period - to optimize max () function

explain select max(payment_date) from payment;

create index idx_paydate on payment (payment_data) ; // index to payment_date (covering indexes)

// find out at the same time in 2006 and 2007 in the film in a number of SQL - Optimize Count () function

select count (release_year = '2006' or null) as '2006, the number of cinema', count (release_year = '2007' or null) as '2007 annual number of films' from film;

count () Description:

 

 

 

 

 

 

 

Sub-query optimization:

Under normal circumstances, we need to handle the query optimizer to join queries, but when optimizing to pay attention to whether there are associated with key-many relationship, pay attention to duplicate data. (Distinct de-emphasis)

// query all movie starring sandra

explain select title,release_year,LENGTH from film

where film_id in (

select film_id from film_actor where actor_id in (

select actor_id from actor where first_name='sandra'));

group by optimization

// change before the temporary table

explain select actor.first_name,actor_last_name,count(*) from sakila.film_actor

inner join sakila.actor USING(actor_id)

group by film_actor.actor_id;

// change the combination of sub-index query

explain select actor.first_name,actor.last_name,c.cnt from sakila.film_actor

inner join (

select actor_id,count(*) as cnt from sakila.film_actor group by actor_id) as c USING(actor_id);

 

limit Optimization

limit commonly used in the paging process, often accompanied by order by clause to use, so most of the time will be used Filesorts this will cause a lot of IO issues.

// Sort files, IO big

explain select film_id,description from sakila.film order by title limit 50,5;

1. Optimization: indexed using the primary key column or order by operation (order by film_id)

2. The return of the last primary key record with a primary key filter at the next query, to avoid excessive scan large volumes of data records

select film_id,description from sakila.film where film_if>55 and film_id<=60 order by film_id limit 1,5; 

The greater the number of pages, the greater the IO

// query the last pay period - to optimize max () function EXPLAIN the SELECT max (PAYMENT_DATE) from Payment; the Create index idx_paydate ON Payment (payment_data); // index (covering indexes) to payment_date // find out at the same time in a SQL the number of films in 2006 and 2007 - optimize Count () function of the SELECT COUNT (RELEASE_YEAR = '2006' or null ) AS '2006, the number of cinema' , COUNT (RELEASE_YEAR = '2007' or null ) AS '2007, the number of movie ' from Film; // relevant count () function https://blog.csdn.net/wendychiang1991/article/details/70909958/

Guess you like

Origin www.cnblogs.com/lovechengyu/p/11490809.html