如何提高数据插入/更新性能? [英] How to improve data insert/update performance?

查看:84
本文介绍了如何提高数据插入/更新性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提高数据加载的性能。当前算法从表中进行完全选择:

I need to improve the performance of data loading. The current algorythm makes a full select from a table:

select Field1, Field2,...,FieldN from Table1 order by FieldM

新数据是从文本文件中读取的(例如,每个数据表行的文本文件行)。
该表具有一个主键,其中包含两个字段。对于文本文件的每一行,它通过这两个字段(即主键)找到必要的行。

The new data is read from a text file (say, textfile line per datatable row). The table has a primary key, containing two fields. For each line of a textfile it locates the necessary row by these two fields (i.e. the primary key).

query.Locate('Field1;Field2',VarArrayOf([Value1,Value2]),[]);

如果 Locate 返回 True ,它将编辑该行,否则将添加一个新行。

If Locate returns True, it edits the row, otherwise it appends a new one.

因此,只要该表包含约200000行,每个 Locate 操作都需要花费一定的时间...因此它每秒能够更新约5-6行。

So, as far as the table consists of about 200000 rows, each Locate operation takes certain amount of time...so it manages to update about 5-6 rows per second.

我应该考虑采取什么措施来改善它?

What things should I consider to improve it?

可能用单独的查询替换通过这种出色的选择找到的位置?

Probably replace locating through this great select with separate queries?

推荐答案

请勿使用Locate()。如果您使用locate(),则Delphi会在客户端搜索行,仅扫描查询中的行集就需要很多时间。

DON'T use Locate(). If you use locate() then Delphi searches row on the client side just scanning row set from your query it takes a LOT of time.

如果您有权访问MSSQL来创建存储过程,则创建以下过程,并在没有任何条件的情况下仅对TEXT文件中的每一行运行它(在Delphi中使用TAdoStoredProc.ExecProc )。因此,在这种情况下,您不需要先选择并定位程序。如果找到Filed1和Field2,它会更新记录;如果找不到,则将其插入。

If you have access to MSSQL to create stored procedures then create following procedure and just run it for each line from your TEXT file without any conditions (Use TAdoStoredProc.ExecProc in Delphi). So in this case your don't need first select and Locate procedure. It updates record if Filed1 and Field2 are found and insert if don't.

CREATE PROCEDURE dbo.update_table1 
@Field1 int, --key1
@Field2 int, --key2
@Field3 int, -- data fileds
@Field4 int

AS

SET NOCOUNT ON
update table1 set Field3=@Field3,Field4=@Field4 
        where Field1=@Field1 and Field2=@Field2;
IF(@@Rowcount=0)
BEGIN
     insert into table1(Field1,Field2,Field3,Field4) 
                values (@Field1,@Field2,@Field3,@Field4);
END
GO

以下是Delphi代码,用于使用ADO调用此存储过程:

Here is Delphi code to invoke this stored procedure with ADO:

......
var 
     ADOStoredP: TADOStoredProc;

  ......
begin

........
    ADOStoredP:=TADOStoredProc.Create(nil);
   try
      ADOStoredP.Connection:=DataMod.SQL_ADOConnection; //Your ADO Connection instance here
      ADOStoredP.ProcedureName:='Update_table1';
      ADOStoredP.Parameters.CreateParameter('@Field1', ftInteger, pdInput, 0, 0);
      ADOStoredP.Parameters.CreateParameter('@Field2', ftInteger, pdInput, 0, 0);
      ADOStoredP.Parameters.CreateParameter('@Field3', ftInteger, pdInput, 0, 0);
      ADOStoredP.Parameters.CreateParameter('@Field4', ftInteger, pdInput, 0, 0);

      While () -- Your text file loop here
      begin

      ADOStoredP.Parameters.ParamByName('@Field1').Value:=Field1 value from text file here;
      ADOStoredP.Parameters.ParamByName('@Field2').Value:=Field2 value from text file here;
      ADOStoredP.Parameters.ParamByName('@Field3').Value:=Field3 value from text file here;
      ADOStoredP.Parameters.ParamByName('@Field4').Value:=Field4 value from text file here;

      ADOStoredP.ExecProc;

      end

    finally
      if Assigned(ADOStoredP) then
        begin
         ADOStoredP.Free;
        end;
    end;

........
end;

这篇关于如何提高数据插入/更新性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆