Search code examples
delphidelphi-xe2tcxgrid

Load dataset / query faster


I have an application when at the click of a button all records from a database table get loaded into a cxGrid (via tadquery / datasource). The number of records will increase as users insert them.

I was just wondering if there is anything I need to do or should do to make sure that it doesn't take too long to load all records.

This is what I'm doing when button is clicked:

with query1 do
begin
  Close;
  SQL.Clear;
  SQL.Text := 'SELECT * FROM DBA.Table1;
  Open;
end;

Suggestions would be appreciated.


Solution

  • It's not a particularly rigorous test, obviously, but with my test app below, setting GridMode to True results in the dataset being opened and the grid populated in about 125ms (for 7k 22-column rows) compared to 900ms with it set to False.

    procedure TForm1.Button1Click(Sender: TObject);
    var
      T1 : Integer;
    begin
      if qNames.Active then
        qNames.Close;
      T1 := GetTickCount;
      cxGrid1DBTableView1.DataController.DataModeController.GridMode := CheckBox1.Checked;
      qNames.Open;
      Caption := IntToStr(qNames.RecordCount) + ':' + IntToStr(GetTickCount - T1) + ':' + IntToStr(qNames.FieldCount);
    end;
    

    This is with everything in the cxGrid set to their defaults except that I've set the KeyFieldNames of the DataController to the dataset's PK.