使用参数化和块语句来提高批处理SQL语句的执行效率
如果你的项目要求你的程序对高达几万条的数据在集中的时间内执行固定序列的操作,且不能完全使用存储过程时而需要使用程序来执行时。会需要这些优化。
我们知道,SQL服务器对一条语句的执行,需要分析、编译、执行这些步骤,通过参数化我们可以对一种命令只分析和编译一次,而执行多次,从而提高效率。在执行时,如果每次提交语句,可以完成多条SQL语句,则可以减少通讯时间,也可以提高效率。
通过 System.Data.IDbCommand.Prepare() 方法,我们可以在第一次执行语句时,分析和编译SQL语句,然后保存这个Command对象,下次使用时,直接设置参数执行。这个方法对Oracle和MsSql Server都有效。
如果执行一批语句,在T-SQL和PLSQL中有一点不一样。
在T-SQL中,多条SQL语句之间,使用分号";"分隔就行。
delete from TableA where id = @id;update TableB set Name=@name where id=@id
而在PLSQL中,则需要用 begin ... end; 包起来,中间语句用分号";"分隔。
begin delete from TableA where id = :id;update TableB set Name=:name where id=:id; end;
相信这样做之后,你的效率会有几倍或者十几倍的提升。当然,你还可以对只是查而修改的表,做一下缓存处理,来减小访问数据库的次数。
下面我示例一下访问Oracle执行PLSQL的一个函数的例子:
private void DeleteFlowInstanceData(string flowinstanceid)
{
OracleCommand cmd = this.cmdDeleteFlowInstanceData;
if(cmd == null)
{
//生成SQL
StringBuilder sb = new StringBuilder();
sb.Append("begin ");
sb.Append(@"delete from bak_WF_Log_EngineLog where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Log_ErrLog where FlowInstanceID= :instanceId;");
sb.Append(@"delete from WF_Running_MsgForEngineBak where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_MsgForUser where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_FlowActivity where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_FlowData where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_FlowInstance where FlowInstanceID= :instanceId;");
sb.Append("end;");
//准备 DbCommand
this.cmdDeleteFlowInstanceData = cmd = new OracleCommand();
cmd.Connection = this.connEngine;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sb.ToString();
cmd.Parameters.Clear();
cmd.Parameters.Add("instanceId", OracleType.VarChar,250);
//准备一下以提高性能。
cmd.Prepare();
}
//设置参数
cmd.Parameters["instanceId"].Value = flowinstanceid;
//设置事务
cmd.Transaction = this.tranEngine;
cmd.ExecuteNonQuery();
}
{
OracleCommand cmd = this.cmdDeleteFlowInstanceData;
if(cmd == null)
{
//生成SQL
StringBuilder sb = new StringBuilder();
sb.Append("begin ");
sb.Append(@"delete from bak_WF_Log_EngineLog where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Log_ErrLog where FlowInstanceID= :instanceId;");
sb.Append(@"delete from WF_Running_MsgForEngineBak where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_MsgForUser where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_FlowActivity where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_FlowData where FlowInstanceID= :instanceId;");
sb.Append(@"delete from bak_WF_Running_FlowInstance where FlowInstanceID= :instanceId;");
sb.Append("end;");
//准备 DbCommand
this.cmdDeleteFlowInstanceData = cmd = new OracleCommand();
cmd.Connection = this.connEngine;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sb.ToString();
cmd.Parameters.Clear();
cmd.Parameters.Add("instanceId", OracleType.VarChar,250);
//准备一下以提高性能。
cmd.Prepare();
}
//设置参数
cmd.Parameters["instanceId"].Value = flowinstanceid;
//设置事务
cmd.Transaction = this.tranEngine;
cmd.ExecuteNonQuery();
}
QQ:273352165
evlon#126.com
转载请注明出处。