The common optimization summary in the author's work:
- 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";
- 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; - 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";
- Only list the fields that need to be queried, which will not have a significant impact on the speed, and mainly consider saving memory;
- 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"); - 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;
- Don't use the rand function to get multiple random records. Avoid doing this: select * from table order by rand() limit 20;
- avoid using NULL;
- Don't use count(id); instead use count(*);
- Don't do unnecessary sorting operations, but do the sorting in the index as much as possible.