FireDac 的RecordCount 相关测试 记录。

时间:2021-05-26 06:28:59
unit Unit4;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DBGridEhGrouping, ToolCtrlsEh,
DBGridEhToolCtrls, DynVarsEh, Vcl.StdCtrls, EhLibVCL, GridsEh, DBAxisGridsEh,
DBGridEh, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error,
FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait, Data.DB,
FireDAC.Comp.Client, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
FireDAC.DApt, FireDAC.Phys.MSSQLDef, FireDAC.Phys.ODBCBase,
FireDAC.Phys.MSSQL, FireDAC.Comp.UI, FireDAC.Comp.DataSet; type
TForm4 = class(TForm)
DBGridEh1: TDBGridEh;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
FDConnection1: TFDConnection;
FDGUIxWaitCursor1: TFDGUIxWaitCursor;
FDPhysMSSQLDriverLink1: TFDPhysMSSQLDriverLink;
DataSource1: TDataSource;
FDQuery1: TFDQuery;
Button5: TButton;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Button6: TButton;
Label2: TLabel;
Label1: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form4: TForm4; implementation {$R *.dfm} procedure TForm4.Button1Click(Sender: TObject);
var
start: Cardinal;
begin
start := GetTickCount;
FDQuery1.Close;
FDQuery1.FetchOptions.Mode := fmOnDemand;
FDQuery1.FetchOptions.RecordCountMode := cmVisible;
FDQuery1.Open('SELECT * FROM top_trade');
Label1.Caption := Format('总数:%d,耗时:%d',[FDQuery1.RecordCount, GetTickCount - start]);
end; procedure TForm4.Button2Click(Sender: TObject);
var
start: Cardinal;
begin
start := GetTickCount;
FDQuery1.Close;
FDQuery1.FetchOptions.Mode := fmAll;
FDQuery1.FetchOptions.RecordCountMode := cmVisible;
FDQuery1.Open('SELECT * FROM top_trade');
Label3.Caption := Format('总数:%d,耗时:%d',[FDQuery1.RecordCount, GetTickCount - start]);
end; /// <summary>
/// 方法1,重新根据官方的Mode参数设置,获取全部数据,重新查一次
/// </summary>
procedure TForm4.Button3Click(Sender: TObject);
var
start: Cardinal;
begin
start := GetTickCount;
with TFDQuery.Create(nil) do
begin
Connection := FDConnection1;
FetchOptions.Mode := fmAll;
Open(FDQuery1.SQL.Text);
Label4.Caption := Format('总数:%d,耗时:%d',[RecordCount, GetTickCount - start]);
Free;
end;
end; /// <summary>
/// 方法2,依然是重新查一次,但是不获取全部数据,保持官方的默认参数,
/// 开启数据集单向 然后 调用last方法 跳到最后 然后取RecordNo
/// </summary>
procedure TForm4.Button4Click(Sender: TObject);
var
start: Cardinal;
begin
start := GetTickCount;
with TFDQuery.Create(nil) do
begin
Connection := FDConnection1;
FetchOptions.CursorKind := ckForwardOnly;{开启单向}
Open(FDQuery1.SQL.Text);
Last;
Label5.Caption := Format('总数:%d,耗时:%d',[RecNo, GetTickCount - start]);
Free;
end;
end; /// <summary>
/// 方法3,测试 RecordCountMode
/// 经过测试这个 依然是查的全部 与 FetchOptions.Mode := fmAll 一样,但如果仅仅获取总数要快很多。
/// </summary>
procedure TForm4.Button5Click(Sender: TObject);
var
start: Cardinal;
begin
start := GetTickCount;
with TFDQuery.Create(nil) do
begin
Connection := FDConnection1;
FetchOptions.RecordCountMode := cmTotal;
Open(FDQuery1.SQL.Text);
Label6.Caption := Format('总数:%d,耗时:%d',[RecordCount, GetTickCount - start]);
Free;
end;
end; procedure TForm4.Button6Click(Sender: TObject);
var
start: Cardinal;
begin
start := GetTickCount;
FDQuery1.Close;
FDQuery1.FetchOptions.Mode := fmOnDemand;
FDQuery1.FetchOptions.RecordCountMode := cmTotal;
FDQuery1.Open('SELECT * FROM top_trade');
Label2.Caption := Format('总数:%d,耗时:%d',[FDQuery1.RecordCount, GetTickCount - start]);
end; end.

FireDac 的RecordCount 相关测试 记录。

DEMO 下载链接: http://files.cnblogs.com/files/del88/FireDac-DEMO.zip

事实证明,即保证速度 又保证 recordCount是总数的情况下,通过 FDQuery1.FetchOptions.RecordCountMode := cmTotal; 这行代码是最 快的

要想保证RecordCount是总数的情况下,且加载全部数据的情况下,测试结果:

FetchOptions.Mode := fmAll; ---- 耗时5秒

RecordCountMode := cmTotal; ----- 耗时2秒。

这两句都能显示全部数据。