22,210
社区成员
发帖
与我相关
我的任务
分享
ALTER PROCEDURE SelUserRecordGameScoreInfoByKindID
@varTime1 DATETIME,
@varTime2 DATETIME,
@varKindID INT,
@vartype INT
AS
DECLARE
@TIME1 DATETIME,
@TIME2 DATETIME,
@KindID INT,
@type INT
SET @Time1=@varTime1
SET @Time2=@varTime2
SET @KindID=@varKindID
SET @type=@vartype
改成这样后,速度跟sql一样快了。 谢谢
DECLARE
@Time1 DATETIME,
@Time2 DATETIME,
@KindID INT,
@type INT
SET @Time1='2014-8-10' SET @Time2='2014-8-18' SET @KindID=1000 SET @type=2
--0.2元
CREATE TABLE #m(userid INT,money numeric(18,1) DEFAULT ((0)),cgl INT DEFAULT ((0)),sumscore BIGINT DEFAULT ((0)),sumdiamond BIGINT DEFAULT ((0)))
INSERT #m (userid)
SELECT DISTINCT userid FROM [QPTreasureDB].[dbo].RecordUserScorePerGame (NOLOCK) a
WHERE RecordTime > @Time1 AND RecordTime < @Time2 AND userid>20000 AND KindID=@KindID
AND NOT EXISTS(SELECT userid FROM QPGameUserDB.dbo.PlayTest b WHERE a.userid=b.userid)
IF @type=1
BEGIN
IF @KindID=3001 OR @KindID=1002
BEGIN
UPDATE #m SET money=score
FROM (SELECT UserID,(SUM(GoodsCount)*0.2) AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (14,18) and RecordDate>@Time1
and RecordDate<@Time2 and GoodsID =96 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--1元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount) AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =31 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--5元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*5 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =67 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--10元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*10 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =37 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--15元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*15 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse=1 and GoodsID =69 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--20元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*20 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =77 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--30元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*30 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =91 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--50元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*50 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =78 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--80元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*80 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =71 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--100元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*100 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse =1 and GoodsID =89 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--200元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*200 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse =1 and GoodsID =90 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
UPDATE #m SET cgl=cc
FROM (SELECT UserID, SUM(GoodsCount) AS cc
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (14,18) and RecordDate>@Time1
and RecordDate<@Time2 and GoodsID =13 AND KindID=@KindID
GROUP BY UserID
)AS A WHERE A.UserID=#m.UserID
END
UPDATE #m SET sumscore=ss,sumdiamond=sd
FROM (SELECT UserID, SUM(Score) AS ss,SUM(Diamond) AS sd
FROM [QPTreasureDB].[dbo].RecordUserScorePerGame a(NOLOCK)
where RecordTime>@Time1 AND KindID=@KindID AND Reason IN(1,6,24,21,22,27)
and RecordTime<@Time2
GROUP BY UserID
)AS A WHERE A.UserID=#m.UserID
SELECT TOP 50 @kindID AS kindid,KindName,Accounts,#m.userid,money,cgl,sumscore,sumdiamond,((money+cgl)*10000)+sumscore+(sumdiamond*10) AS Score FROM #m,QPGameUserDB.dbo.AccountsInfo b,QPServerInfoDB.dbo.GameKindItem c
WHERE #m.userid=b.UserID AND c.KindID=@KindID AND ((money+cgl)*10000)+sumscore+(sumdiamond*10) <>0
AND b.userid>20000
GROUP BY KindName,Accounts,#m.userID,money,cgl,sumscore,sumdiamond
ORDER BY (money+cgl)*10000+sumscore+(sumdiamond*10) DESC
END
ELSE
BEGIN
IF @KindID=3001 OR @KindID=1002
BEGIN
UPDATE #m SET money=score
FROM (SELECT UserID,(SUM(GoodsCount)*0.2) AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (14,18) and RecordDate>@Time1
and RecordDate<@Time2 and GoodsID =96 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--1元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount) AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =31 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--5元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*5 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =67 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--10元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*10 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =37 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--15元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*15 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse=1 and GoodsID =69 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--20元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*20 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =77 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--30元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*30 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =91 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--50元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*50 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =78 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--80元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*80 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (1,14,18) and GoodsID =71 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--100元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*100 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse =1 and GoodsID =89 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
--200元
UPDATE #m SET money= money+score
FROM (SELECT UserID,SUM(GoodsCount)*200 AS score
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse =1 and GoodsID =90 AND KindID<>0 and RecordDate>@Time1
and RecordDate<@Time2 AND KindID=@KindID
GROUP BY UserID
) AS A WHERE A.UserID=#m.UserID
UPDATE #m SET cgl=cc
FROM (SELECT UserID, SUM(GoodsCount) AS cc
FROM [QPTreasureDB].[dbo].[RecordUserGoods] a
where GetOrUse in (14,18) and RecordDate>@Time1
and RecordDate<@Time2 and GoodsID =13 AND KindID=@KindID
GROUP BY UserID
)AS A WHERE A.UserID=#m.UserID
END
--这里就是统计大量数据表的
UPDATE #m SET sumscore=ss,sumdiamond=sd
FROM (SELECT UserID, SUM(Score) AS ss,SUM(Diamond) AS sd
FROM [QPTreasureDB].[dbo].RecordUserScorePerGame a(NOLOCK)
where RecordTime>@Time1 AND KindID=@KindID AND Reason IN(1,6,24,21,22,27)
and RecordTime<@Time2
GROUP BY UserID
)AS A WHERE A.UserID=#m.UserID
SELECT TOP 50 @kindID AS kindid,KindName,Accounts,#m.userid,money,cgl,sumscore,sumdiamond,((money+cgl)*10000)+sumscore+(sumdiamond*10) AS Score FROM #m,QPGameUserDB.dbo.AccountsInfo b,QPServerInfoDB.dbo.GameKindItem c
WHERE #m.userid=b.UserID AND c.KindID=@KindID AND ((money+cgl)*10000)+sumscore+(sumdiamond*10) <>0
AND b.userid>20000
GROUP BY KindName,Accounts,#m.userID,money,cgl,sumscore,sumdiamond
ORDER BY (money+cgl)*10000+sumscore+(sumdiamond*10) ASC
END
DROp TABLE #m
GO
--这是存储过程了,虽然#m临时表多次查询,但是不太影响效率(这要统计每个道具对应的价值。。。)
--直接执行很快,改成存储过程就很慢