premise
During the tuning process, use
explain
commands to view the execution process, including execution time, scanning mode, whether indexes are used, etc., EXPLAIN usage analysis
开启查询sql执行时间:
\timing on
关闭查询sql执行时间:
\timing off
1. Problem description
A query interface is called frequently, and the query process is slow
2. Optimization ideas
- First consider optimizing SQL statements
- Secondly consider optimizing business code
- Finally, consider whether you need to add a caching mechanism
3. Optimization process
3.1 Optimize SQL
Original SQL, group query data with the largest seq_id in each group, use in + group by to achieve
select name, version from yc_test where seq_id in(
select max(seq_id) from yc_test group by name
);
3.1.1 SQL optimization ideas
- Consider adding indexes to speed up the query
in
Will be convertedor
and convertedunion all
, the efficiency is lower. Consider usingexists
orjoin
replacing. why?
3.1.2 Optimized SQL
select a.name, a.version from yc_test a inner join (
select max(seq_id) as seq_id from yc_test group by name
) b on a.seq_id=b.seq_id;
3.1.3 Comparison before and after SQL optimization
explain:
sql | advantage | Disadvantage |
---|---|---|
in implementation | sql simple | in will be converted to or and then to union all, which is less efficient, as shown in the figure for an extra layer of loop |
inner join implementation | Reduce one layer of circulation and improve efficiency | In contrast, SQL is more complicated |
3.2 Optimize business code
Combining functional business scenarios (which can be understood as historical records), read the code, and found that not all data needs to be persisted.
Optimization solution:
定时器
Regularly clean up expired data to reduce data redundancy业务逻辑
Do control, judge whether there is extra data when saving data, and delete the extra data if there is.
3.3 Add caching mechanism
热点数据
, You can consider caching in memory (variable cache), or storage in the database (Redis/Memcached).
Since the amount of data has been controlled and combined with later planning, no caching mechanism has been added.