FireDac 的RecordCount 相关测试 记录。
2024-08-19 21:13:05
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秒。
这两句都能显示全部数据。
最新文章
- QMap
- objective-c系列-NSArray
- Python基础之函数等等
- ios webView 默认有缓存
- ZOOKEEPER在CENTOS6上的再安装
- AES SHA-1 SM3 MD5
- [WP8] Binding时,依照DataType来选择DataTemplate
- Qt 的内部进程通信机制
- SCM白色幼儿系列(十二) Proteus仿真软件简介
- MySQL Study之--Mysql无法启动“mysql.host”
- [转]网上看到的关于C中内存分配的描述
- linux下c语言的多线程编程
- docker数据库
- SSM-MyBatis-17:Mybatis中一级缓存(主要是一级缓存存在性的证明,增删改对一级缓存会造成什么影响)
- 1.Python
- pycharm 2017最新激活码
- CRM JS
- poj3114 Contries in War (tarjan+dijkstra)
- 指定变形中心点CSS3
- (Mark)Myeclipse10.6 下怎么安装Jad插件