我保存了一组大数据,每个记录都有一个时间标记。我想用UTC时间标记保存每个项目,所以我使用ToUniversalTime()(VB.NET)转换原始时间。使用这种方法似乎很慢。您是否有任何提示如何加快这一过程?
For Each i In CollectionOutput.Select(Function(f) f)
Dim utcTimeMark = i.GetTIMEMARK.ToUniversalTime()
contextEmanagement.pr_Collection_Store(i.GetID, i.GetTIMEMARK, utcTimeMark, i.GetVALUE)
Next
其中.pr_Collection_Store调用SQL Server存储过程。
答案 0 :(得分:1)
目前您的存储过程大概是这样的(我绝对猜测数据类型,我假设您使用的是SQL Server 2008或更好 - 请在提问时指定您的版本!):
CREATE PROCEDURE dbo.pr_Collection_Store
@GetID INT,
@GetTimeMark DATETIME,
@UTCTimeMark DATETIME,
@GetValue INT
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.SomeTable(ID, TimeMark, UTCTimeMark, Value)
SELECT @GetID, @GetTimeMark, @UTCTimeMark, @GetValue;
END
GO
你可以这样做:
CREATE TYPE dbo.Collection AS TABLE
(
GetID INT,
GetTimeMark DATETIME,
UTCTimeMark DATETIME,
GetValue INT
);
GO
CREATE PROCEDURE dbo.pr_Collection_Store2
@Collection AS dbo.Collection READONLY
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.SomeTable(ID, TimeMark, UTCTimeMark, Value)
SELECT GetID, GetTimeMark, UTCTimeMark, GetValue
FROM @Collection AS c
WHERE NOT EXISTS (SELECT 1 FROM dbo.SomeTable WHERE ID = c.GetID);
END
GO
然后从应用程序。对不起,我知道C#,希望你能把它翻译成VB.NET。
DataTable tvp = new DataTable();
tvp.Columns.Add(new DataColumn("i"));
foreach(var i in CollectionOutput)
{
tvp.Rows.Add(i.GetID, i.GetTIMEMARK, i.GetTIMEMARK.ToUniversalTime(), i.GetValue);
}
using (connection)
{
SqlCommand cmd = new SqlCommand("dbo.pr_Collection_Store2", connection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvparam = cmd.Parameters.AddWithValue("@Collection", tvp);
tvparam.SqlDbType = SqlDbType.Structured;
connection.Open();
cmd.ExecuteNonQuery();
}