1. 问题描述分析
erp web 超时日志,从阿里云日志里捞出,排列第2多,每天出现
子查询用到了not in 测试改为 not exists 效果依然不好,因为RB_WareHouse 表查询只用到了一个 id 条件,
测试对比:NOT IN, not exists 查询效果是一样的,都是用的索引扫描,查询时间都是55秒
2.解决方案
测试对比:NOT IN, not exists 查询效果是一样的,都是用的索引扫描,查询时间都是55秒,所以反向条件查询 + 1个id 条件,没有优化的空间
贴在此处,为以后的not in 查询 提供借鉴参考
3. 超时SQL
drop table #temp01
create table #temp01(
id int primary key
)
insert into #temp01
SELECT Max(ID)
FROM rb_warehouse_Product (nolock)
WHERE CustomerID = 1
AND WareHouseID IN(SELECT id
FROM dbo.RB_WareHouse (nolock)
WHERE AutonomousWareHouseID = 5327
AND WareHouseType = 3
AND CustomerID = 1)
GROUP BY OrderSourceSKU,
ASIN,
FNSKU,
WareHouseID,
SKU
select id from rb_warehouse_Product a where not exists(select id from #temp01 b where b.id =a.ID)
select id from rb_warehouse_Product a where ID NOT IN(select id from #temp01)
AND CustomerID = 1
AND WareHouseID IN(SELECT id
FROM dbo.RB_WareHouse (nolock)
WHERE AutonomousWareHouseID = 5327
AND WareHouseType = 3
AND CustomerID = 1)
--4274334 ,优化改造后的sql查询 55秒
--4275435 rb_warehouse_Product 数据行数
--4274334 原sql查询行数 用时 88秒
--not in 1101 行数