ADO,ADOStream,Dbexpress与TDataSetProvider的效率

唐麒
2023-12-01

在中间层服务器,打包DataSet的资料,客户端用DataSet接收,有两种格式:

(1)、TDataSetProvider.Data得到的格式,姑且叫做Midas格式

(2)、ADO _Stream得到的格式,姑且叫做ADOStream格式,这其中又分为ADTG格式和XML格式。

下面测试ADO和Dbexpress两种数据库引擎,在生成不同的格式时的效率。

测试代码为:

function  ADOReadData(ADataSet:TCustomADODataSet;AFormat:TPersistFormat=pfADTG):Variant;
var
  vStream:_Stream;
  vFilter:Variant;
begin
  vStream:=CoStream.Create;
  vFilter:=ADataSet.Recordset.Filter;
  try
    //先去掉Filter
    ADataSet.Recordset.Filter:='';
    if AFormat=pfADTG then
    begin
      vStream.type_:=adTypeBinary;
      ADODB_TLB._Recordset(ADataSet.Recordset).Save(vStream,adPersistADTG);
      vStream.Position:=0;
      Result:=vStream.Read(vStream.Size);
    end
    else
    begin
      vStream.type_:=adTypeText;
      OLEVariant(ADataSet.Recordset).Save(vStream,adPersistXML);
      vStream.Position:=0;
      Result:=vStream.ReadText(vStream.Size);
    end;
  finally
    vStream:=nil;
    ADataSet.Recordset.Filter:=vFilter;
  end;
end;

 

procedure TForm4.Button1Click(Sender: TObject);
const
  CNST_RepeatTimes=10;
var
  i:integer;
  A:TDateTime;
  vData:Variant;
begin
  //无图片
  //SQLDataSet1.CommandText:='select * from csSystemLogs';
  //ADODataSet1.CommandText:='select * from csSystemLogs';
  //有图片
  SQLDataSet1.CommandText:='select * from csMachines';
  ADODataSet1.CommandText:='select * from csMachines';

  A:=Now;
  for i := 1 to CNST_RepeatTimes do
  begin
    SQLConnection1.Open;
    SQLDataSet1.Open;
    vData:=DataSetProvider1.Data;
    SQLDataSet1.Close;
    SQLConnection1.Close;
  end;
  Edit1.Text:=FloatToStr(Round((Now-A)*86400000));
  Edit1_1.Text:=InttoStr(VarArrayHighBound(vData,1));

  A:=Now;
  for i := 1 to CNST_RepeatTimes do
  begin
    ADOConnection1.Open;
    ADODataSet1.Open;
    vData:=DataSetProvider2.Data;
    ADODataSet1.Close;
    ADOConnection1.Close;
  end;
  Edit2.Text:=FloatToStr(Round((Now-A)*86400000));
  Edit2_1.Text:=InttoStr(VarArrayHighBound(vData,1));

  A:=Now;
  for i := 1 to CNST_RepeatTimes do
  begin
    ADOConnection1.Open;
    ADODataSet1.Open;
    vData:=ADOReadData(ADODataSet1,pfADTG);
    ADODataSet1.Close;
    ADOConnection1.Close;
  end;
  Edit3.Text:=FloatToStr(Round((Now-A)*86400000));
  Edit3_1.Text:=InttoStr(VarArrayHighBound(vData,1));

  A:=Now;
  for i := 1 to CNST_RepeatTimes do
  begin
    ADOConnection1.Open;
    ADODataSet1.Open;
    vData:=ADOReadData(ADODataSet1,pfXML);
    ADODataSet1.Close;
    ADOConnection1.Close;
  end;
  Edit4.Text:=FloatToStr(Round((Now-A)*86400000));
  Edit4_1.Text:=InttoStr(Length(WideString(vData))*2);
end;

测试结果:

1、资料表中有Image字段,连续打包10次。

序号Midas DbexpressMidas ADOADO Stream ADTGADO Stream XML
打包时间(毫秒):
1468610594108672
2484469578108985
3468454546109023
4484454562109434
5485453562109828
返回资料大小(字节):
 26032672603112260399811186048

速度最快的是“Midas ADO”,资料最小的也是“Midas ADO”。“ADO Stream XML”效率差241倍,资料大小差4倍。

 2、资料表中没有Image字段,连续打包10次。

序号Midas DbexpressMidas ADOADO Stream ADTGADO Stream XML
打包时间(毫秒):
1469562250469
2375562219516
3375547234500
4391547234500
5375562219500
返回资料大小(字节):
 177732156805182433534026
速度最快的是"ADO Stream ADTG",资料最小的是“Midas ADO”。
 类似资料: