Access中not in优化方式
Access中使用not in效率低下,可以使用外连接语句代替:
代码
--改造前写法
SELECT TOP 50 StockholderID,HaveRate,HaveNum
FROM (
SELECT StockholderID,HaveRate,HaveNum
FROM StockInfo
WHERE
CapitalStockID = 104
AND StockholderID NOT IN (
SELECT StockholderID FROM StockInfo WHERE
CapitalStockID = 105)
ORDER BY HaveRate DESC
) AS A
--改造后写法
SELECT TOP 50 StockholderID,HaveRate,HaveNum
FROM (
SELECT a.StockholderID,a.HaveRate,a.HaveNum FROM StockInfo a left outer join (
SELECT c.StockholderID,c.HaveRate,c.HaveNum FROM StockInfo c WHERE c.CapitalStockID = 105
) as b
on a.StockholderID = b.StockholderID
WHERE CapitalStockID = 104 and b.StockholderID is null
ORDER BY a.HaveNum DESC
) as t
--例子1
select TOP 50 a.Name,a.StockholderID,-5 from CollectNew a left outer join (
SELECT TOP 50 c.Name,c.StockholderID FROM CollectOld c ORDER BY OldNum DESC
) as b
on a.StockholderID=b.StockholderID
where b.StockholderID is null
ORDER BY a.NewNum DESC
--排序后会按照a表获取结果,如果想要差异性内容,不能加排序,或者在外层嵌套一层查询Select * from()ORDER ....
--例子二
select a.* from generator a left outer join (select c.busno,c.id from generator c,ta_generator d where c.busno=d.busno) as b
on a.busno=b.busno
where b.busno is null
--改造前写法
SELECT TOP 50 StockholderID,HaveRate,HaveNum
FROM (
SELECT StockholderID,HaveRate,HaveNum
FROM StockInfo
WHERE
CapitalStockID = 104
AND StockholderID NOT IN (
SELECT StockholderID FROM StockInfo WHERE
CapitalStockID = 105)
ORDER BY HaveRate DESC
) AS A
--改造后写法
SELECT TOP 50 StockholderID,HaveRate,HaveNum
FROM (
SELECT a.StockholderID,a.HaveRate,a.HaveNum FROM StockInfo a left outer join (
SELECT c.StockholderID,c.HaveRate,c.HaveNum FROM StockInfo c WHERE c.CapitalStockID = 105
) as b
on a.StockholderID = b.StockholderID
WHERE CapitalStockID = 104 and b.StockholderID is null
ORDER BY a.HaveNum DESC
) as t
--例子1
select TOP 50 a.Name,a.StockholderID,-5 from CollectNew a left outer join (
SELECT TOP 50 c.Name,c.StockholderID FROM CollectOld c ORDER BY OldNum DESC
) as b
on a.StockholderID=b.StockholderID
where b.StockholderID is null
ORDER BY a.NewNum DESC
--排序后会按照a表获取结果,如果想要差异性内容,不能加排序,或者在外层嵌套一层查询Select * from()ORDER ....
--例子二
select a.* from generator a left outer join (select c.busno,c.id from generator c,ta_generator d where c.busno=d.busno) as b
on a.busno=b.busno
where b.busno is null