Delphi FireDac 的RecordCount 相关测试 记录


本文整理自网络,侵删。

 
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.


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

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

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

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

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

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

相关阅读 >>

Delphi下spcomm串口编程

Delphi 向其他程序发送模拟按键

Delphi 在firemonkey应用程序中使用torientationsensor获取设备倾斜和指南针航向

Delphi 2009 查看所有 unicode 字符

Delphi 用hook实现dll注入详解

Delphi 解析 png 图片的十六进制字符流

Delphi中利用钩子实现qq聊天窗口的修改

Delphi 怎么将一个文件流转换成字符串?

Delphi windows 编程[5] - 学习窗体生成的过程五

Delphi文本文件读写

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...