我有一个用SQL编写的信用卡支付流程存储过程。这是在循环使用50万条付款记录时使用SQL。这非常慢,因为我必须为每个支付记录循环另一组数据并相应地更新一堆表。这意味着while循环内部while循环在sql中。
我必须在c#中重写它以提高性能。我们正在使用LINQ&实体框架。任何人都可以建议我这是否有帮助?
由于
使用修改后的存储过程更新我的问题。我已经替换了对表的引用并删除了部分代码。但如果不清楚,请告诉我。
CREATE PROCEDURE [dbo].[USP_ProcessPayments]
(
@UserKey int
)
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #TempUserPayments
(
-- with Columns declared
)
INSERT INTO #TempUserPayments
SELECT DISTINCT up.*, ROW_NUMBER() OVER(ORDER BY up.UserKey DESC, up.DateReceived) AS RN --generate row number
FROM UserPayment up
where up.UserKey = @UserKey
CREATE INDEX UP_1 on #TempUserPayments (RN);
DECLARE @LOOPCOUNTER INT, @ROWCOUNTER INT, @REQUESTEDAMOUNT MONEY, @UPDATEDREQUESTEDAMOUNT MONEY, @AMOUNT MONEY,
@AMOUNTNOTAPPLIED MONEY, @APPLIEDAMOUNT MONEY, @SYSTEMKEY INT, @LOOPCOUNTER_PR INT, @ROWCOUNTER_PR INT, @IS_UPDATE BIT
SELECT @ROWCOUNTER = COUNT(*) from #TempUserPayments;
SET @LOOPCOUNTER = 1
WHILE (@LOOPCOUNTER <= @ROWCOUNTER)
BEGIN
SELECT *,
ROW_NUMBER() OVER (ORDER BY mp.Month DESC, mp.PayPriority) AS RN_PR
INTO #TempPR
FROM Requests pr
INNER JOIN Premium mp ON mp.PremiumKey = pr.PremiumKey
--Joing couple of other tables here
ORDER BY mp.[Month] DESC, mp.PayPriority
CREATE INDEX PR_1 on #TempPR (RN_PR);
SELECT @SYSTEMKEY = SystemKey FROM #TempUserPayments WHERE RN = @LOOPCOUNTER;
SELECT @ROWCOUNTER_PR = COUNT(*) from #TempPR;
SET @LOOPCOUNTER_PR = 1
WHILE (@LOOPCOUNTER_PR <= @ROWCOUNTER_PR)
BEGIN
SET @IS_UPDATE = 0;
IF(@APPLIEDAMOUNT IS NULL OR (@REQUESTEDAMOUNT <> @APPLIEDAMOUNT AND @APPLIEDAMOUNT < @REQUESTEDAMOUNT))
BEGIN
IF(@SYSTEMKEY = 1 OR @SYSTEMKEY = 3)
BEGIN
--Check all the conditions here
SET @IS_UPDATE = 1;
END
IF(@IS_UPDATE = 1)
BEGIN
--Update bunch of requets tables ---
INSERT INTO userpaid
(
--Column names --
)
VALUES
(
--Values --
)
END
END
-- END
SET @LOOPCOUNTER_PR = @LOOPCOUNTER_PR + 1;
END
SET @LOOPCOUNTER = @LOOPCOUNTER + 1;
DROP TABLE #TempPR;
END
DROP TABLE #TempUserPayments;
END