Delphi性能:读取数据集中某个字段下的所有值 [英] Delphi Performance: Reading all values under a field in a dataset

查看:218
本文介绍了Delphi性能:读取数据集中某个字段下的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们试图找出一些从TADOQuery读取的性能修复程序.当前,我们使用'而没有Q.eof开始... Q.next方法来遍历记录.对于每个记录,我们读取每个记录的ID和值,并将每个记录添加到组合框列表中.

We're trying to find out some performance fixes reading from a TADOQuery. Currently, we loop through the records using 'while not Q.eof do begin ... Q.next method. For each, we read ID and Value of each record, and add each to a combobox list.

有没有一种方法可以一次将指定字段的所有值转换为列表?而不是遍历数据集?如果我可以做类似的事情,那将真的很方便.

Is there a way to convert all values of a specified field into a list in one shot? Rather than looping through the dataset? It would be really handy if I can do something like...

TStrings(MyList).Assign(Q.ValuesOfField['Val']);

我知道这不是一个真正的命令,但这就是我要寻找的概念.寻找快速的响应和解决方案(一如既往,但这是为了解决一个非常紧急的性能问题).

I know that's not a real command, but that's the concept I'm looking for. Looking for a fast response and solution (as always but this is to fix a really urgent performance issue).

推荐答案

您应该在Delphi中实现其他人提出的一些出色的性能建议.您应该考虑它们.我将专注于ADO.

There are some great performance suggestions made by other folks that you should implement in Delphi. You should consider them. I will focus on ADO.

您尚未指定后端数据库服务器是什么,因此我不能太具体,但是关于ADO,您应该了解一些事情.

You haven't specified what the back end database server is, so I can't be too specific, but there are some things that you should know about ADO.

ADO RecordSet

在ADO中,有一个RecordSet对象.在这种情况下,该RecordSet对象基本上是您的ResultSet.遍历RecordSet的有趣之处在于它仍与提供程序耦合.

In ADO, there is a RecordSet object. That RecordSet object is basically your ResultSet in this case. The interesting thing about iterating through the RecordSet is that it's still coupled with the provider.

光标类型

如果您的光标类型是Dynamic或Delphi的默认键集,则每次RecordSet向提供程序请求新行时,提供程序都会在返回记录之前检查是否有任何更改.

If your cursor type is Dynamic or Delphi's default Keyset, then each time the RecordSet requests a new row from the provider, the provider will check to see if there were any changes before it returns the record.

因此,对于TADOQuery,您正在做的所有事情都是读取结果集以填充组合框,并且不可能更改,因此应该使用静态游标类型以避免检查更新的记录.

So, for the TADOQuery where all you're doing is reading the result set to populate the combobox, and it's not likely to have changed, you should use the Static cursor type to avoid checking for updated records.

如果您不知道光标是什么,则在调用诸如Next之类的函数时,您将移动代表当前记录的光标.

In case you don't know what a cursor is, when you call a function like Next, you are moving the cursor, which represents the current record.

并非每个提供程序都支持所有游标类型.

Not every provider supports all of the cursor types.

CacheSize

Delphi和ADO的RecordSet的默认缓存大小为1.即1条记录.与光标类型结合使用.缓存大小告诉RecordSet一次要提取和存储多少记录.

Delphi's and ADO's default cache size for a RecordSet is 1. That's 1 record. This works in combination with the cursor type. The cachesize tells the RecordSet how many records to fetch and store at a time.

当您发出诸如Next(在ADO中真正为MoveNext)之类的命令且缓存大小为1时,RecordSet仅将当前记录存储在内存中,因此,当它获取下一个记录时,它必须再次从提供者那里请求它.如果游标不是静态",则提供者有机会在返回下一条记录之前获取最新数据.因此,对于Keyset或Dynamic,大小为1有意义,因为您希望提供程序能够为您提供更新的数据.

When you issue a command like Next (really MoveNext in ADO) with a cache size of 1, the RecordSet only has the current record in memory, so when it fetches that next record, it must request it from the provider again. If the cursor is not Static, that gives the provider the opportunity to get the latest data before returning the next record. So, a size of 1 makes sense for Keyset or Dynamic, because you want the provider to be able to get you the updated data.

很显然,值为1时,每次移动光标时,提供程序和RecordSet之间都会进行通信.好吧,如果游标类型是静态的,那是我们不希望的开销.因此,增加缓存大小将减少RecordSet和提供程序之间的往返次数.这也会增加您的内存需求,但是应该更快.

Obviously, with a value of 1, there's communication between the provider and RecordSet each time move the cursor. Well, that's overhead that we don't want if the cursor type is static. So, increasing your cache size will reduce the number of round trips between the RecordSet and the provider. This also increases your memory requirements, but it should be faster.

还请注意,如果键集游标的缓存大小大于1,则如果所需记录在缓存中,它将不会再次从提供者处请求它,这意味着您将看不到更新.

Also note that with a cache size greater than 1 for Keyset cursors, if the record that you want is in the cache, it won't request it from the provider again, which means that you won't see the updates.

这篇关于Delphi性能:读取数据集中某个字段下的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆