项目中MySQL查询优化
项目中数据量大或者多表多添件查询时,查询时间长,排查及解决方案
关键字explain
- 查看当前SQL中,各表及表中字段是否命中索引
explain select count(base.code) from order_base_info as base inner join order_logistics_info as logisticsInfo on logisticsInfo.order_code = base.code left join order_logistics_cost_info as costInfo on costInfo.order_code = base.code;
- 查看表中的所有索引
show index from order_logistics_info;
- 如果没有需要的索引,就创建索引
create index order_code_index on order_logistics_info(order_code);
- 查看索引有没有生效,再执行一下SQL,看一下,时间有没有变化,如果还不能达到我们要求的时间,那么需要在增加其他需要的索引,以达到我们需要的效能
MySQL操作卡慢问题排查
show processlist
该操作可以查看数据库当前有没有进程还在操作表