枚举 Linq Call 耗时 5 分钟
Enumerate Linq Call takes 5 minutes
我遇到此代码的性能问题,
我正在从数据库读取数据,然后将数据表转换为 CampaignRecipientLib 列表,调用此方法至少需要 5 分钟:
private static List<CampaignRecipientLib> GetGroupRcipientsToSchedule(int GroupId, int CustomerId, bool Delete = false, bool NewlyAddedCampaignGroupRecipient = false)
{
List<SqlParameter> param = new List<SqlParameter>();
param.Add(new SqlParameter("@GroupID", GroupId));
param.Add(new SqlParameter("@CustomerId", CustomerId));
param.Add(new SqlParameter("@Delete", Delete));
param.Add(new SqlParameter("@NewlyAddedCampaignGroupRecipient", NewlyAddedCampaignGroupRecipient));
Stopwatch st = new Stopwatch();
st.Start();
var rec_names = SqlHelper.GetDataTable("[dbo].[p_GetRecipientListByGroupCode]", param, CommandType.StoredProcedure);
st.Stop();
return rec_names.AsEnumerable().Select(row =>
new CampaignRecipientLib
{
ID = row.Field<int>("cgr_RecipientId"),
CMPRID = 0,//row.Field<int>("cr_CampaignRecipientId"),
CMPRCode = codePrefix + Guid.NewGuid().ToString("N"),//row.Field<string>("cr_CampaignRecipientCode"),
CampaignId = 0,
SentCampaigns = string.IsNullOrEmpty(row.Field<string>("GroupScenarioList")) ? new List<int>() : row.Field<string>("GroupScenarioList").Split(',').Select(int.Parse).ToList(),
EmailTo = row.Field<string>("r_Email"),
Email = row.Field<string>("r_Email"),
FirstName = row.Field<string>("r_FirstName"),
LastName = row.Field<string>("r_LastName"),
Language = string.IsNullOrEmpty(row.Field<string>("r_LangCode")) ? null : row.Field<string>("r_LangCode"),
Scheduled = DateTime.Now,//row.Field<DateTime?>("cr_Scheduled").HasValue ? row.Field<DateTime?>("cr_Scheduled") : DateTime.Now,
IsdoubleBarrle = false,//row.Field<bool>("cr_IsDoubleBarrel"),
Offset = string.IsNullOrEmpty(row.Field<string>("r_Offset")) ? null : row.Field<string>("r_Offset"),
Toffset = row.Field<string>("r_Offset").StartsWith("-") ? -TimeSpan.Parse(row.Field<string>("r_Offset").Remove(0, 1)) : TimeSpan.Parse(row.Field<string>("r_Offset")),
ReadyTobeSent = false,//row.Field<bool>("cr_ReadyTobeSent"),
PickupReady = false,//row.Field<bool>("cr_PickupReady"),
DefaultLanguage = string.Empty//string.IsNullOrEmpty(row.Field<string>("cmp_LangCode")) ? null : row.Field<string>("cmp_LangCode")
}
).ToList();
}
同一个调用,在不同的项目中超级快,不到一秒!
我的意思是不同的项目 is:windows 服务,当前需要较长时间的项目是:Asp Web Form application
有什么线索或建议吗?
注意:对数据库的调用需要 1.5 秒才能return 一个具有 4000 行的数据表,但枚举部分是花费大部分时间的部分
return rec_names.AsEnumerable().Select(row =>
new CampaignRecipientLib
{ .... }).ToList();
============================
更新:
这是系统 A 运行缓慢的屏幕截图:
有没有可能是标记线的原因?因为我将该列的内容转换为逗号分隔的 int 值
这里是系统B的截图,速度超快,但我不需要在这个调用中绑定GroupScenarioList列
找到原因了
codePrefix 是一个 属性,它每次为 dataTable 中的每一行调用一个存储过程,
return rec_names.AsEnumerable().Select(row =>
new CampaignRecipientLib
{
ID = row.Field<int>("cgr_RecipientId"),
CMPRID = 0,//row.Field<int>("cr_CampaignRecipientId"),
CMPRCode = codePrefix + Guid.NewGuid().ToString("N"),//row.Field<string>("cr_CampaignRecipientCode"),
CampaignId = 0,
... ....
我只是将其更改为调用一次,方法是将 "codePrefix" 属性 返回的唯一值存储在局部变量中。
我遇到此代码的性能问题, 我正在从数据库读取数据,然后将数据表转换为 CampaignRecipientLib 列表,调用此方法至少需要 5 分钟:
private static List<CampaignRecipientLib> GetGroupRcipientsToSchedule(int GroupId, int CustomerId, bool Delete = false, bool NewlyAddedCampaignGroupRecipient = false)
{
List<SqlParameter> param = new List<SqlParameter>();
param.Add(new SqlParameter("@GroupID", GroupId));
param.Add(new SqlParameter("@CustomerId", CustomerId));
param.Add(new SqlParameter("@Delete", Delete));
param.Add(new SqlParameter("@NewlyAddedCampaignGroupRecipient", NewlyAddedCampaignGroupRecipient));
Stopwatch st = new Stopwatch();
st.Start();
var rec_names = SqlHelper.GetDataTable("[dbo].[p_GetRecipientListByGroupCode]", param, CommandType.StoredProcedure);
st.Stop();
return rec_names.AsEnumerable().Select(row =>
new CampaignRecipientLib
{
ID = row.Field<int>("cgr_RecipientId"),
CMPRID = 0,//row.Field<int>("cr_CampaignRecipientId"),
CMPRCode = codePrefix + Guid.NewGuid().ToString("N"),//row.Field<string>("cr_CampaignRecipientCode"),
CampaignId = 0,
SentCampaigns = string.IsNullOrEmpty(row.Field<string>("GroupScenarioList")) ? new List<int>() : row.Field<string>("GroupScenarioList").Split(',').Select(int.Parse).ToList(),
EmailTo = row.Field<string>("r_Email"),
Email = row.Field<string>("r_Email"),
FirstName = row.Field<string>("r_FirstName"),
LastName = row.Field<string>("r_LastName"),
Language = string.IsNullOrEmpty(row.Field<string>("r_LangCode")) ? null : row.Field<string>("r_LangCode"),
Scheduled = DateTime.Now,//row.Field<DateTime?>("cr_Scheduled").HasValue ? row.Field<DateTime?>("cr_Scheduled") : DateTime.Now,
IsdoubleBarrle = false,//row.Field<bool>("cr_IsDoubleBarrel"),
Offset = string.IsNullOrEmpty(row.Field<string>("r_Offset")) ? null : row.Field<string>("r_Offset"),
Toffset = row.Field<string>("r_Offset").StartsWith("-") ? -TimeSpan.Parse(row.Field<string>("r_Offset").Remove(0, 1)) : TimeSpan.Parse(row.Field<string>("r_Offset")),
ReadyTobeSent = false,//row.Field<bool>("cr_ReadyTobeSent"),
PickupReady = false,//row.Field<bool>("cr_PickupReady"),
DefaultLanguage = string.Empty//string.IsNullOrEmpty(row.Field<string>("cmp_LangCode")) ? null : row.Field<string>("cmp_LangCode")
}
).ToList();
}
同一个调用,在不同的项目中超级快,不到一秒! 我的意思是不同的项目 is:windows 服务,当前需要较长时间的项目是:Asp Web Form application
有什么线索或建议吗?
注意:对数据库的调用需要 1.5 秒才能return 一个具有 4000 行的数据表,但枚举部分是花费大部分时间的部分
return rec_names.AsEnumerable().Select(row =>
new CampaignRecipientLib
{ .... }).ToList();
============================
更新:
这是系统 A 运行缓慢的屏幕截图:
这里是系统B的截图,速度超快,但我不需要在这个调用中绑定GroupScenarioList列
找到原因了
codePrefix 是一个 属性,它每次为 dataTable 中的每一行调用一个存储过程,
return rec_names.AsEnumerable().Select(row =>
new CampaignRecipientLib
{
ID = row.Field<int>("cgr_RecipientId"),
CMPRID = 0,//row.Field<int>("cr_CampaignRecipientId"),
CMPRCode = codePrefix + Guid.NewGuid().ToString("N"),//row.Field<string>("cr_CampaignRecipientCode"),
CampaignId = 0,
... ....
我只是将其更改为调用一次,方法是将 "codePrefix" 属性 返回的唯一值存储在局部变量中。