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 下载链接: https://files.cnblogs.com/files/del88/FireDac-DEMO.zip
事实证明,即保证速度 又保证 recordCount是总数的情况下,通过 FDQuery1.FetchOptions.RecordCountMode := cmTotal; 这行代码是最 快的
要想保证RecordCount是总数的情况下,且加载全部数据的情况下,测试结果:
FetchOptions.Mode := fmAll; ---- 耗时5秒
RecordCountMode := cmTotal; ----- 耗时2秒。
这两句都能显示全部数据。
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/p/6203588.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步