https://apistore.top 开源电商支付系统,欢迎加入转发。
https://github.com/XiaoMi/soar
参见启发式建议
避免采用子查询,关联查询效率更高
SELECT t1., t4. from table1 t1
left join table2 t4
on t1.activityId = t4.toolActivityId
and t4.attrKey in (‘BUSINESS_SOURCE’)
left join table2 t5
on t1.activityId = t5.toolActivityId
and t5.attrKey in (‘BUSINESS_SOURCE’)
and t5.attrValue = ‘ZB_BLZX’
where t5.id is not null
删除时也应该避免子查询,改为关联查询再删除
delete t1 from table1 t1, (
select t2.id from table2 t2
inner join table3 t3
on t2.id = t3.tid
) t4
where t1.id = t4.id
避免写select count(1) from (select ? from ?) t1,这种在子查询无分页、无条件的情况下耗费很多资源。
Order by的字段需要用高效率的索引,不然扫描行数也会很多。
在子查询内部尽量将数据量降到最少,并且作为一张关联表和其它表做JOIN查询,避免子查询。
IN内的数量过多会造成全表扫描。
LIKE不要使用全模式匹配“%你%”,这种无法使用索引。