我寫了一個與我的Visual Basic .NET代碼完全相同的存儲過程。現在我已經對它們進行了基準測試。使用while循環和for循環。但是,兩者都給我帶來了更糟糕的結果,即使用我的Visual Basic .NET代碼。有什麼辦法改善這種代碼的性能:存儲過程提高性能
DECLARE @RelationCode nvarchar(50)
DECLARE @CompanyName nvarchar(256)
DECLARE @IncomingInvoice decimal(18, 2)
DECLARE @OutgoingInvoice decimal(18, 2)
DECLARE @Profit decimal(18, 2)
DECLARE @RelationTable as TABLE (RELATIONCODE nvarchar(10), COMPANY nvarhar(120))
INSERT INTO @RelationTable (RELATIONCODE, COMPANY) SELECT [fms].[dbo].[Relation].[RELATIONCODE], [fms].[dbo].[Relation].[COMPANYNAME] FROM [fms].[dbo].[Relation]
/* Result table */
DECLARE @RESULTTABLE TABLE (RelationCode nvarchar(50), Companyname nvarchar(256), IncomingInvoice nvarchar(50), OutgoingInvoice nvarchar(50), profit nvarchar(50))
WHILE EXISTS(SELECT * FROM @RelationTable)
BEGIN
SELECT TOP 1 @RelationCode = RELATIONCODE, @CompanyName = COMPANY FROM @RelationTable
SELECT fmsTotalAmountIncoming INTO TempIncomingAmounts FROM (
SELECT SUM(CASE WHEN fms1.currency != 'EUR'
THEN fms1.amount * fms1.rate
ELSE ISNULL(fms1.amount, 0) END) fmsTotalAmountIncoming
FROM [fms].[dbo].[file] f
INNER JOIN [fms].[dbo].[incominginvoiceline] fms1 ON
fms1.filenumber = CONVERT(NVARCHAR, f.filenumber)
WHERE f.RELATIONCODE = @RelationCode
) a
SELECT fmsTotalAmountOutgoing INTO TempOutgoingAmounts FROM (
SELECT SUM(CASE WHEN fms1.currency != 'EUR'
THEN fms1.amount * fms1.rate
ELSE ISNULL(fms1.amount, 0) END) fmsTotalAmountOutgoing
FROM [fms].[dbo].[file] f
INNER JOIN [fms].[dbo].[outgoinginvoiceline] fms1 ON
fms1.filenumber = CONVERT(NVARCHAR, f.filenumber)
WHERE f.RELATIONCODE = @RelationCode
) a
SET @IncomingInvoice = (SELECT fmsTotalAmountIncoming FROM [fms].[dbo].[TempIncomingAmounts])
SET @OutgoingInvoice = (SELECT fmsTotalAmountOutgoing FROM [fms].[dbo].[TempOutgoingAmounts])
SET @Profit = ((@OutgoingInvoice - @IncomingInvoice)/@OutgoingInvoice)
INSERT INTO @RESULTTABLE ([RELATIONCODE], [CompanyName], [IncomingInvoice], [OutgoingInvoice], [Profit])
VALUES (@RelationCode, @CompanyName, @IncomingInvoice, @OutgoingInvoice, @Profit)
DROP TABLE [fms].[dbo].[TempIncomingAmounts]
DROP TABLE [fms].[dbo].[TempOutgoingAmounts]
DELETE FROM @RelationTable WHERE RelationCode = @RelationCode
END
SELECT * FROM @RESULTTABLE
我已經基準這一點,下面的結果:
STORED PROCEDURE VB.NET
6:54 5:11
6:20 5:11
6:19 3:55
6:43 4:01
有沒有人對如何提高VB.net上述性能的任何線索性能,因爲這種方式存儲過程是沒用的。
存儲過程*不*提高錯誤查詢的性能。而不是像這樣使用遊標或循環(實際上是一種更慢的遊標形式),您應該編寫適當的查詢 –
請發佈您的表模式和數據示例。 –
使用正確的工具進行工作。 Sql服務器就是爲保存和讀取數據而設計的IO設備。計算和其他「業務」邏輯可以在你的應用程序('vb.net')中完成 - 很容易,可以更快地完成(用於加載來自不同表格的數據的'async-await'方法),可讀(非常重要)和簡單保持。 – Fabio