MySQL standalone optimization --- SQL optimization

SQL optimization ( changed several times as a maintenance Maintenance )

  Sql optimization divided into: DDL, DML, DQL

  A, DDL optimization

 

    1 , to provide import data by disabling the performance index. This operation is mainly targeted at database tables, additional data

 

      // remove keys

 

      alter table test3 DISABLE keys;

 

      // bulk insert data

 

      insert into test3 select * from test;

 

      // recovery key

 

      alter table test3 ENABLE keys;

 

    Index maintenance changed several times as a index maintenance

 

 

 

       2 , check off single

 

      set unique_checks = 0   Close

 

      // bulk insert data

 

      insert into test3 select * from test;

 

      set unique_checks = 1   Open

 

    The only check once changed many times the only check

 

 

 

        3 , modify transaction submission ( import )

 

      set autocommit = 0    Close # false

 

      // bulk insert

 

      set autocommit = 1    turn  true

 

    Rights to change many times once the transaction commits

 

 

  Two, DML optimization

 

    insert into test (name) values(2);

 

    insert into test values(1,3);

 

    insert into test values(1,4);

 

    // merge multiple is a  mybatis batch operation:

 

    insert into test values(1,2),(1,3),(1,4)

 

    Rights to change many times once the transaction commits

 

  Three, DQL optimization

 

    (1)   1 by the Order Optimization

 

      1 , the use of index ordering

 

      2 , the ordinary sort the results (non-indexed sort) filesort

 

      The index itself is sort of, so much the use of the index .

 

    (2)    Group by optimization

 

      Queries sum payment for a time

 

      explain

 

      select DATE_FORMAT(payment_date,'%Y-%m'),sum(amount) from payment GROUP BY DATE_FORMAT(payment_date,'%Y-%m') ;

 

      explain

 

      select DATE_FORMAT(payment_date,'%Y-%m'),sum(amount) from payment GROUP BY DATE_FORMAT(payment_date,'%Y-%m') order by null;

 

      In the group by using the order by null, cancel the default sort

 

 

 

    (. 3)  SubQuery nesting optimization

 

       Find the customer is not in the list of customers to pay list

 

       # Find customers not "pay list" in the customer list , query the customer never bought something

 

       explain

 

       select * from customer where customer_id not in (select DISTINCT customer_id from payment);

 

       explain

 

          select * from customer c left join payment p on (c.customer_id = p.customer_id) where p.customer_id is null - this is based on the "Index" outer chain

 

 

 

    (4) or optimization

 

      Using two separate indexes or better performance than

 

      1 , or both sides are to make a judgment with index fields, good performance! !

 

      2 , or on both sides, one side is not, poor performance

 

      3 , if the employee table name and email two columns is a composite index, but if it is : name = 'A' OR email = 'B' In this manner, the index will not be used!

 

 

 

    (5)   limit optimization

 

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

 

      select a.film_id,a.description from filqm a inner join (select film_id from film order by title limit 50,5)b on a.film_id=b.film_id

 

 

  30 Zhong sql statement optimization: https://www.cnblogs.com/Little-Li/p/8031295.html

 

 

Guess you like

Origin www.cnblogs.com/wanghj-15/p/11470506.html