Delphi,TcxGrid 或 TDataSet 中最快的记录访问方法是什么

发布于 2024-09-30 11:29:21 字数 625 浏览 10 评论 0原文

我正在使用 Delphi 2007 和 TcxGrid 来显示文件库的内容。该数据库包含有关文件类型、名称、路径和扩展名的信息。

在测试中,我通过 TDataSet 将 1700 条记录加载到网格中。我还在网格中为另外 3 个需要计算的字段腾出了空间。它们是文件是否存在、文件大小和修改日期。

我的目标是向用户显示存储的所有信息(效果很好而且速度很快),然后在后台线程中找到其他三个数据字段的信息,然后将它们插入到 TcxGrid 中。这个问题与我正在做的线程几乎没有关系。它工作正常。

我的问题是,访问已构建的网格中的字段时,速度会大大减慢。我尝试了两种不同的方法...

  1. Grid.DataController.Values[RecordIndex,FieldIndex] - 但这是一个变体,我怀疑这就是它如此慢的原因

  2. Grid.DataController.DataSet.FindFirst Grid.DataController.DataSet.FindNext Grid.DataController.DataSet.Fields[FieldIndex] 但使用这种“寻找”方法与我尝试的第一种方法一样慢。定位和移动也很慢。

这么长的问题很短,访问记录最快的方法是什么?

I am using Delphi 2007 and the TcxGrid to show the contents of a file library. The database houses information about the type of file, name, path, and extension.

In testing I am loading 1700 records into the grid through the TDataSet. I also make room in the grid for 3 more fields which need yet to be calculated. They are if the files exists, size of the file, and date modified.

My goal is to show the user all of the information that's stored (which works great and is fast) then in a background thread find the information for the other three fields of data and then insert them into the TcxGrid. This question has little if nothing to do with the threading I am doing. Its working fine.

My problem is that accessing the fields in the grid that is already constructed has a huge slowdown when I access it. I have tried two different ways ...

  1. Grid.DataController.Values[RecordIndex,FieldIndex] - but this is a variant and I suspect that is why it is so slow

  2. Grid.DataController.DataSet.FindFirst
    Grid.DataController.DataSet.FindNext
    Grid.DataController.DataSet.Fields[FieldIndex]
    But using this "seek" method is just as slow as the first method I tried. Locate and moveby are slow as well.

So long question short, What is the fastest way to access a record?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在梵高的星空下 2024-10-07 11:29:21

另请说明您使用的是 TcxGridDBTableView 还是 TcxGridTableView?

我认为使用非数据库感知 TcxGridTableView 或 TcxBandedGridTableView 与

View.DataController.Values[RecordIndex, FieldIndex] 是最快的。

即使您有一个数据库应用程序,您也可以在初始化时加载视图的非数据库版本,然后处理 DataController 事件来检测数据更改并发出相应的 SQL 命令来更新数据库。

您可以像这样填充视图:

class procedure TForm1.FillView(const View: TcxGridBandedTableView; const Sql: string);
var
  Reader: TMyOrmDataReader;
  i: Integer;
begin
  Assert(Assigned(View), 'View is not assigned parameter.');
  with View.DataController do
  begin
    BeginFullUpdate;
    try
      Reader := TMyOrm.GetDataReader(SQL);
      try
        i := 0;
        RecordCount := 50 * 1000; // make it something big in order to avoid constant resizing by 1
        while Reader.Read do
        begin
          // Fill the view
         Values[i,  0] := Reader.GetInt32(0);
         Values[i,  1] := Reader.GetString(1);
         Inc(i);
        end;
        RecordCount := i - 1;
      finally
        Reader.Free;
      end;
    finally
      EndFullUpdate;
    end;
  end;
end;

然后访问如下值:

View1.DataController.Values[View1.DataController.FocusedRecordIndex, View1Column1.Index].AsString

Please also mention whether you are using a TcxGridDBTableView or TcxGridTableView?

I think using non-db aware TcxGridTableView or TcxBandedGridTableView with

View.DataController.Values[RecordIndex, FieldIndex] is the fastest.

Even if you have a db application you can load the non-db version of the view on the initialization and than handle DataController events to detect data changes and issues the respective SQL commands for updating the database.

You can fill the view like this:

class procedure TForm1.FillView(const View: TcxGridBandedTableView; const Sql: string);
var
  Reader: TMyOrmDataReader;
  i: Integer;
begin
  Assert(Assigned(View), 'View is not assigned parameter.');
  with View.DataController do
  begin
    BeginFullUpdate;
    try
      Reader := TMyOrm.GetDataReader(SQL);
      try
        i := 0;
        RecordCount := 50 * 1000; // make it something big in order to avoid constant resizing by 1
        while Reader.Read do
        begin
          // Fill the view
         Values[i,  0] := Reader.GetInt32(0);
         Values[i,  1] := Reader.GetString(1);
         Inc(i);
        end;
        RecordCount := i - 1;
      finally
        Reader.Free;
      end;
    finally
      EndFullUpdate;
    end;
  end;
end;

And then access values like:

View1.DataController.Values[View1.DataController.FocusedRecordIndex, View1Column1.Index].AsString
Hello爱情风 2024-10-07 11:29:21

您可以首先使用表/数据集附加缺失的数据,然后将所有字段显示为数据绑定。

You could work with table/dataset first appending the missing data and then show all the fields as datbound.

沙沙粒小 2024-10-07 11:29:21

更新代码需要在开始/结束更新内是我的大问题。

感谢 Lieven 的评论,因为它解决了我的问题。感谢加德和丹尼尔的回复,我从中学到了东西。

如果我昨天有更多时间发布一些代码,这对每个人来说都会更加明显。

The updating code needed to be within a begin/end update was my big problem.

Thank you Lieven for your comment as it was the solution to my problem. And thank you Gad and Daniel for responses which I learned from.

This would have been more apparent to everyone had I more time yesterday to post some code.

jJeQQOZ5 2024-10-07 11:29:21

我知道这是一个旧线程,但我会提到将网格切换为使用提供者模式是提高加载速度的另一种方法,因为它本质上将网格加载变成了虚拟操作。 (直到需要显示或其他数据访问时才会加载记录。)之后,DataController 会缓存它们,因此加载后访问速度非常快。

I know this is an old thread, but I'll mention that switching the grid to using Provider mode is another way to improve loading speed, because it essentially turns the grid loading into a virtual operation. (Records aren't loaded until they're needed for display or other data access.) After that, the DataController caches them, so access is very fast once loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文