标题:存储过程影响服务器cpu,该优化哪里
只看楼主
kobe2408
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-9-6
结帖率:0
已结贴  问题点数:20 回复次数:1 
存储过程影响服务器cpu,该优化哪里
----2009-03-25 针对开开,增加查看远程商店的记录
CREATE PROCEDURE PRO_ReadGatheringGuideLine(  
@vDate VARCHAR(10),  
@vChlCode VARCHAR(20),
@vUserCode VARCHAR(20)   
)  
AS  
  
DECLARE @iShopSum INT, @iHasTradeSum INT, @iNoTradeSum INT,  
@iSaleQuantity INT, @iUntreadQuantity INT, @fSaleMoney FLOAT,  
@vBestShop VARCHAR(100), @fBestMoney FLOAT, @fBestRate FLOAT,  
@iBillSum INT, @iCancelSum INT, @iPutupSum INT, @fChange FLOAT,  
@fCashMoney FLOAT, @fBankMoney FLOAT, @fOtherMoney FLOAT,
@iKHRight INT
declare
 @vKHRight VARCHAR(5000)

----2090806添加客户权限的控制

select @iKHRight=isnull(bAllCustomers,0) from UserParams where vUserCode = @vUserCode

if isnull(@iKHRight, 0) = 1
begin
SET @iShopSum=ISNULL((SELECT COUNT(*) FROM KEHU WHERE QDDM=@vChlCode AND XZDM='2' AND ISNULL(BYZD4, '0')='2'), 0)
SELECT DISTINCT vShop INTO #TEMP FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
SET @iHasTradeSum=(SELECT COUNT(vShop) FROM #TEMP) DROP TABLE #TEMP
SET @iNoTradeSum=ISNULL(@iShopSum, 0)-ISNULL(@iHasTradeSum, 0)

SET @iSaleQuantity=ISNULL((SELECT SUM(CASE WHEN fQuantity>=0 THEN fQuantity ELSE 0 END) FROM SG_Gatherings WHERE vMBillID IN (
SELECT vMBillID FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0)), 0)
SET @iUntreadQuantity=ISNULL((SELECT SUM(CASE WHEN fQuantity<0 THEN ABS(fQuantity) ELSE 0 END) FROM SG_Gatherings WHERE vMBillID IN (
SELECT vMBillID FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0)), 0)
SET @fSaleMoney=ISNULL((SELECT SUM(fRealMoney) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0), 0)

SELECT vShop, SUM(fRealMoney) AS fRealMoney INTO #TABLE FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0 GROUP BY vShop
IF @@ROWCOUNT>0 SET @fBestMoney=(SELECT TOP 1 fRealMoney FROM #TABLE ORDER BY fRealMoney DESC) ELSE SET @fBestMoney=0
IF @fBestMoney=0 SET @fBestRate=0 ELSE SET @fBestRate=@fBestMoney/@fSaleMoney
SET @vBestShop=ISNULL((SELECT TOP 1 vShop FROM #TABLE WHERE fRealMoney=@fBestMoney), '无')
SET @vBestShop=@vBestShop + ' ' + (SELECT TOP 1 KHMC FROM KEHU WHERE KHDM=@vBestShop) DROP TABLE #TABLE

SET @iBillSum=(SELECT COUNT(vMBillID) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0)
SET @iCancelSum=(SELECT COUNT(vMBillID) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=1)
SET @iPutupSum=(SELECT COUNT(vMBillID) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=1)

SET @fChange=ISNULL((SELECT SUM(fChange) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0), 0)
SET @fCashMoney=ISNULL((SELECT SUM(A.fMoney) FROM SG_PayMethod A, SG_Gathering B WHERE A.vMBillID=B.vMBillID AND
A.vShop=B.vShop AND A.vPFCode=B.vPFCode AND CONVERT(VARCHAR(10), B.dtDate, 20)=@vDate AND B.bCancel=0 AND B.bPutup=0 AND A.vBalCode='000'), 0)
SET @fCashMoney=@fCashMoney-@fChange
SET @fBankMoney=ISNULL((SELECT SUM(A.fMoney) FROM SG_PayMethod A, SG_Gathering B WHERE A.vMBillID=B.vMBillID AND
A.vShop=B.vShop AND A.vPFCode=B.vPFCode AND CONVERT(VARCHAR(10), B.dtDate, 20)=@vDate AND B.bCancel=0 AND B.bPutup=0 AND
A.vBalCode IN (SELECT JSDM FROM POSJS WHERE XZDM='2')), 0)
SET @fOtherMoney=@fSaleMoney-@fCashMoney-@fBankMoney
end
else
begin
SET @iShopSum=ISNULL((SELECT COUNT(*) FROM KEHU WHERE QDDM=@vChlCode AND XZDM='2' AND ((ISNULL(BYZD4, '0')='2') OR
(ISNULL(BYZD4,'0')='1' AND ISNULL(BYZD7,'0')='1'))
AND KEHU.KHDM IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode)), 0)

SELECT DISTINCT vShop INTO #TEMP1 FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode)
SET @iHasTradeSum=(SELECT COUNT(vShop) FROM #TEMP1) DROP TABLE #TEMP1  
SET @iNoTradeSum=ISNULL(@iShopSum, 0)-ISNULL(@iHasTradeSum, 0)
   
SET @iSaleQuantity=ISNULL((SELECT SUM(CASE WHEN fQuantity>=0 THEN fQuantity ELSE 0 END) FROM SG_Gatherings  WHERE vMBillID IN (  
SELECT vMBillID FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode))), 0)  
SET @iUntreadQuantity=ISNULL((SELECT SUM(CASE WHEN fQuantity<0 THEN ABS(fQuantity) ELSE 0 END) FROM SG_Gatherings WHERE vMBillID IN (  
SELECT vMBillID FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode))), 0)  
SET @fSaleMoney=ISNULL((SELECT SUM(fRealMoney) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode)), 0)  
  
SELECT vShop, SUM(fRealMoney) AS fRealMoney INTO #TABLE1 FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode)
GROUP BY vShop  
IF @@ROWCOUNT>0 SET @fBestMoney=(SELECT TOP 1 fRealMoney FROM #TABLE1 ORDER BY fRealMoney DESC) ELSE SET @fBestMoney=0  
IF @fBestMoney=0 SET @fBestRate=0 ELSE SET @fBestRate=@fBestMoney/@fSaleMoney   
SET @vBestShop=ISNULL((SELECT TOP 1 vShop FROM #TABLE1 WHERE fRealMoney=@fBestMoney), '无')   
SET @vBestShop=@vBestShop + ' ' + (SELECT TOP 1 KHMC FROM KEHU WHERE KHDM=@vBestShop
AND KEHU.KHDM IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode)) DROP TABLE #TABLE1  
  
SET @iBillSum=(SELECT COUNT(vMBillID) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode))  
SET @iCancelSum=(SELECT COUNT(vMBillID) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=1
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode))  
SET @iPutupSum=(SELECT COUNT(vMBillID) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=1
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode))  
  

SET @fChange=ISNULL((SELECT SUM(fChange) FROM SG_Gathering WHERE CONVERT(VARCHAR(10), dtDate, 20)=@vDate AND bCancel=0 AND bPutup=0
AND SG_Gathering.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode)), 0)  
SET @fCashMoney=ISNULL((SELECT SUM(A.fMoney) FROM SG_PayMethod A, SG_Gathering B WHERE A.vMBillID=B.vMBillID AND  
A.vShop=B.vShop AND A.vPFCode=B.vPFCode AND CONVERT(VARCHAR(10), B.dtDate, 20)=@vDate AND B.bCancel=0 AND B.bPutup=0 AND A.vBalCode='000'
AND B.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode)), 0)  
SET @fCashMoney=@fCashMoney-@fChange  
SET @fBankMoney=ISNULL((SELECT SUM(A.fMoney) FROM SG_PayMethod A, SG_Gathering B WHERE A.vMBillID=B.vMBillID AND  
A.vShop=B.vShop AND A.vPFCode=B.vPFCode AND CONVERT(VARCHAR(10), B.dtDate, 20)=@vDate AND B.bCancel=0 AND B.bPutup=0  AND
 B.vShop IN (select  UserCustomers.KHDM from UserCustomers, KEHU where UserCustomers.KHDM =  KEHU.KHDM AND KEHU.XZDM='2' and vUserCode=@vUserCode) AND
A.vBalCode IN (SELECT JSDM FROM POSJS WHERE XZDM='2')), 0)  
SET @fOtherMoney=@fSaleMoney-@fCashMoney-@fBankMoney  
end
SELECT @iShopSum AS iShopSum, @iHasTradeSum AS iHasTradeSum, @iNoTradeSum AS iNoTradeSum,   
@iSaleQuantity AS iSaleQuantity, @iUntreadQuantity AS iUntreadQuantity, @fSaleMoney AS fSaleMoney,  
@vBestShop AS vBestShop, @fBestMoney AS fBestMoney, @fBestRate AS fBestRate,  
@iBillSum AS iBillSum, @iCancelSum AS iCancelSum, @iPutupSum AS iPutupSum,  
@fCashMoney AS fCashMoney, @fBankMoney AS fBankMoney, @fOtherMoney AS fOtherMoney  



GO
搜索更多相关主题的帖子: 服务器 影响 记录 开开 
2015-09-06 10:34
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
得分:20 
先看看
2015-09-09 06:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-457035-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.113528 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved