索引优化

--查询语句例子
select b.material_id,a.lot_no_status,sum(a.qty) as qty
from tb_lot_no b with(nolock)
inner join tb_lot_no_wip a with(nolock) on b.lot_no_status=11 and a.lot_no_uid=b.lot_no_uid 
inner join plan_wo c with(nolock) on a.wo_code = c.wo_code
where c.create_time >= '2021-05-01 00:00:00' and c.create_time < '2021-05-31 23:59:59' 
and a.lot_no_status in(11,12,16,17,20,23)
group by b.material_id,a.lot_no_status

上述语句查询需要0.7秒左右,略微缓慢,考虑到后续数据量变大会更慢,有优化的必要。SQLServer显示估计的执行计划并没有分析出来缺少的索引,但是发现tb_lot_no_wip走的主键索引,开销95%,这就需要人为去分析索引了。要优化索引,首先要了解索引作用于何处。join语句的关联条件,where条件,order by的字段,至于group by的字段,having后面的条件还不太确定是否用到索引。分析上述语句,得知需要给wo_code,lot_no_status,lot_no_uid等字段建索引,包含的字段为qty。索引建好后,上述语句查询时间为0.2秒左右。

Copyright © TouchNet 2015 all right reserved,powered by Gitbook最后修订时间: 2022-06-21 09:21:02

results matching ""

    No results matching ""