我写了一个与我的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