简单的参数化 select 查询需要时间在 C# 中执行
Simple parameterized select query takes time to execute in C#
我有一个 table 结构,如下所示:
CREATE TABLE [dbo].[MainTbl](
[RowNum] [int] IDENTITY(1,1) NOT NULL,
[TxtID] [varchar](20) NOT NULL,
[TxtKey] [varchar](20) NOT NULL,
[TrnDate] [datetime] NOT NULL,
[SrcID] [varchar](20) NOT NULL DEFAULT (''),
[ElemName] [varchar](20) NOT NULL,
[TblXml] [varchar](max) NOT NULL,
[ActiveStatus] [varchar](20) NOT NULL DEFAULT (''),
[DevLvl] [int] NOT NULL DEFAULT ((0)),
[Archive] [bit] NULL CONSTRAINT [DF_MainTbl_Archive] DEFAULT ((0)),
CONSTRAINT [pkMainTbl] PRIMARY KEY CLUSTERED
([RowNum] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
并执行以下查询以获取 xml :
select TblXml from MainTbl with (nolock)
where TrnDate < @TrnDate and TxtID = @TxtID and TxtKey = @TxtKey
and ActiveStatus != 'NonActive' and ElemName = 'xyz' order by TrnDate desc
此查询在 SSMS 中的执行时间为 5 秒,但通过 C# 代码执行时,
大约需要 5 分钟以上。
我的 C# 代码接受查询案例号。在 "BuildQuery" 中准备上述语句和
return一个sqlcommand,我传参执行,但是"sqlCommand.ExecuteReader();"耗时多
private bool MainFunction(parameters)
{
CheckXMLDate(4,'9/6/2016 1:00:00 PM','ABC','123' );
}
________________________________
public bool CheckXMLDate(bldqry,TrnDate,TxtID,TxtKey )
{
SqlCommand sqlCommand = BuildQuery(bldqry);
sqlCommand.Parameters.AddWithValue("@TrnDate", TrnDate);
sqlCommand.Parameters.AddWithValue("@TxtID", TxtID);
sqlCommand.Parameters.AddWithValue("@TxtKey", TxtKey);
SqlDataReader dataReader = sqlCommand.ExecuteReader();
//Some statements like below...
if (check == success)
{ dataReader.Close();
return true;}
dataReader.Close();
return false;
}
_______________________________________
public SqlCommand BuildQuery(int caseNum)
{
string QryString = string.Empty;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = CommandType.Text;
sqlCommand.Connection = con;
switch (caseNum)
{
case 4:
{
QryString = "select TblXml from MainTbl with (nolock) "+
"where TrnDate < @TrnDate "+
"and TxtID = @TxtID "+
"and TxtKey = @TxtKey " +
"and ActiveStatus != 'NonActive' and ElemName = 'xyz' order by TrnDate desc";
break;
}
default:
{ break; }
}
sqlCommand.CommandText = QryString;
sqlCommand.CommandTimeout = 800;
return sqlCommand;
}
此处参数均为字符串类型
您似乎没有任何索引来优化查询。在 (ElemName, TxtID, TxtKey, ActiveStatus, TrnDate)
上创建多字段索引
传递参数时的以下更改提高了查询的性能,并且它在几秒钟内再次执行。
sqlCommand.Parameters.Add("@TrnDate", SqlDbType.VarChar).Value = TrnDate;
sqlCommand.Parameters.Add("@TxtID", SqlDbType.VarChar).Value = TxtID;
sqlCommand.Parameters.Add("@TxtKey", SqlDbType.VarChar).Value = TxtKey;
感谢大家的建议!
我有一个 table 结构,如下所示:
CREATE TABLE [dbo].[MainTbl](
[RowNum] [int] IDENTITY(1,1) NOT NULL,
[TxtID] [varchar](20) NOT NULL,
[TxtKey] [varchar](20) NOT NULL,
[TrnDate] [datetime] NOT NULL,
[SrcID] [varchar](20) NOT NULL DEFAULT (''),
[ElemName] [varchar](20) NOT NULL,
[TblXml] [varchar](max) NOT NULL,
[ActiveStatus] [varchar](20) NOT NULL DEFAULT (''),
[DevLvl] [int] NOT NULL DEFAULT ((0)),
[Archive] [bit] NULL CONSTRAINT [DF_MainTbl_Archive] DEFAULT ((0)),
CONSTRAINT [pkMainTbl] PRIMARY KEY CLUSTERED
([RowNum] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
并执行以下查询以获取 xml :
select TblXml from MainTbl with (nolock)
where TrnDate < @TrnDate and TxtID = @TxtID and TxtKey = @TxtKey
and ActiveStatus != 'NonActive' and ElemName = 'xyz' order by TrnDate desc
此查询在 SSMS 中的执行时间为 5 秒,但通过 C# 代码执行时, 大约需要 5 分钟以上。
我的 C# 代码接受查询案例号。在 "BuildQuery" 中准备上述语句和 return一个sqlcommand,我传参执行,但是"sqlCommand.ExecuteReader();"耗时多
private bool MainFunction(parameters)
{
CheckXMLDate(4,'9/6/2016 1:00:00 PM','ABC','123' );
}
________________________________
public bool CheckXMLDate(bldqry,TrnDate,TxtID,TxtKey )
{
SqlCommand sqlCommand = BuildQuery(bldqry);
sqlCommand.Parameters.AddWithValue("@TrnDate", TrnDate);
sqlCommand.Parameters.AddWithValue("@TxtID", TxtID);
sqlCommand.Parameters.AddWithValue("@TxtKey", TxtKey);
SqlDataReader dataReader = sqlCommand.ExecuteReader();
//Some statements like below...
if (check == success)
{ dataReader.Close();
return true;}
dataReader.Close();
return false;
}
_______________________________________
public SqlCommand BuildQuery(int caseNum)
{
string QryString = string.Empty;
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = CommandType.Text;
sqlCommand.Connection = con;
switch (caseNum)
{
case 4:
{
QryString = "select TblXml from MainTbl with (nolock) "+
"where TrnDate < @TrnDate "+
"and TxtID = @TxtID "+
"and TxtKey = @TxtKey " +
"and ActiveStatus != 'NonActive' and ElemName = 'xyz' order by TrnDate desc";
break;
}
default:
{ break; }
}
sqlCommand.CommandText = QryString;
sqlCommand.CommandTimeout = 800;
return sqlCommand;
}
此处参数均为字符串类型
您似乎没有任何索引来优化查询。在 (ElemName, TxtID, TxtKey, ActiveStatus, TrnDate)
上创建多字段索引传递参数时的以下更改提高了查询的性能,并且它在几秒钟内再次执行。
sqlCommand.Parameters.Add("@TrnDate", SqlDbType.VarChar).Value = TrnDate;
sqlCommand.Parameters.Add("@TxtID", SqlDbType.VarChar).Value = TxtID;
sqlCommand.Parameters.Add("@TxtKey", SqlDbType.VarChar).Value = TxtKey;
感谢大家的建议!