TADOQuery插入大数据量的记录效率较高的方法

不是每个时候每一种情况下都是“使用存储过程更快”,存储过程的快速在于其可以对批处理命令进行预编译,从而使得批量的命令执行更加有效率,这个主要体现在计算过程,而对于简单的insert和select则不尽如此了。对于mssqlserver,可以使用批量的insert:

var
  Insert_Lst: TstringList;
begin
  Insert_Lst := TStringList.Create;
  try
    for I := 0 to Count - 1 do //最好每500~1000条做一批
      Insert_lst.Add('Insert into tablename (Field1,Field2...) Value(''+StringValue1+''','+IntValue ...')');
    Insert_Lst.LineBrek := ';';//对于低版本如果不支持LineBreak的可以在上面的行末尾增加一个分号来解决
    ADOCommand.CommandText := Insert_Lst.Text;
   ADOCommand.Execute;
  finally
    Insert_Lst.Free;
  end;
end;


一个以前写的示例:

  const   AnsiString   SplitEmpty="",Split=";";     
    
  DWORD   StartTime=GetTickCount();     
    
  int   Count,iCacheSize=1000;     
    
  TStringList   *CacheList=new   TStringList();     
  try     
  {     
    
        for(int   i=0;i<200000;i++)     
        {     
              if(Count)     
                    CacheList->Add(";Insert   into   tablename   (field1,field2,field3[...,fieldn])   values(value1,value2,value3[...,valuen])");     
              else     
                    CacheList->Add("Insert   into   tablename   (field1,field2,field3[...,fieldn])   values(value1,value2,value3[...,valuen])");     
    
              Count++;     
    
              if(Count==iCacheSize)     
              {     
                    ADOCommand->CommandText=CacheList->Text;     
                    CacheList->Clear();     
                    Count=0;     
                    ADOCommand->Execute();     
              }     
                
        }     
    
        if(Count>0)     
        {     
              ADOCommand->CommandText=CacheList->Text;     
              ADOCommand->Execute();     
        }     
  }     
  __finally     
  {     
        CacheList->Free();     
  }     
    
  DWORD   EndTime=GetTickCount();     
    
  ShowMessage((int)(EndTime-StartTime));     

附注:此方法不适用于ACCESS数据库!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值