MySQL common optimization summary

The common optimization summary in the author's work:

  1. Try to avoid operations on the column, which will cause the index to fail; select * from t where YEAR(d) >=2011; Optimized to: select * from t where d>="2011-01-01";
  2. When using JOIN, you should use a small result set to drive a large result set. At the same time, split a complex JOIN query into multiple queries, because JOIN multiple tables may lead to more locks and blockages. Try to avoid using like this: select * from a JOIN b ON a.id=b.id
        LEFT JOIN c ON c.time=a.date
        LEFT JOIN d ON c.pid=b.aid
        LEFT JOIN e ON e.cid=a.did;
  3. Note that when LIKE fuzzy query, avoid %%, for example: select * from t where name LIKE "%de%"; optimize to: select * from t where name >="de" AND name <="df";
  4. Only list the fields that need to be queried, which will not have a significant impact on the speed, and mainly consider saving memory;
  5. Use bulk insert statements to save interaction. insert into t(id,name) VALUES(1,"a");
        insert into t(id,name) VALUES(2,"b");
        insert into t(id,name) VALUES (3,"c");Optimized as: INSERT INTO t(id,name) VALUES(1,"a"),(2,"b"),(3,"c");
  6. Use between when the cardinality of limit is relatively large, for example: select * from article as article order by id limit 1000000,10; optimized to: select * from article where id between 1000000 and 1000010 order by id;
  7. Don't use the rand function to get multiple random records. Avoid doing this: select * from table order by rand() limit 20;
  8. avoid using NULL;
  9. Don't use count(id); instead use count(*);
  10. Don't do unnecessary sorting operations, but do the sorting in the index as much as possible.

Guess you like

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