在 TQuery 结果集中查找记录的最快方法
我想知道最好的(读:最快的)方法是在 Tquery SQL 语句的结果集中定位记录。
到目前为止,我正在使用 TQuery.Locate,如果我是对的,这是唯一可用于在结果集中搜索的语句。那么我们该如何优化呢?
我有一些想法,但还没有时间在大型数据集上比较它们:
假设我们有一个包含以下字段的表:
Create Table aTable (
ID int,
Name1 varchar(50),
Name2 varchar(50));
以及以下查询:
SELECT ID, Name1, Name2 from aTable
我们希望通过结果中的 ID 来定位记录 ?
- 如果aTable 在ID 上有索引,locate 会更快吗
- 如果我在 SQL 语句中添加“Order By ID”,定位会更快吗?
对此有什么想法吗?
[编辑] 澄清其用途:查询由 Reportbuilder Dataview 执行,然后通过数据管道(即 TQuery.Dataset)提供。在自定义报告中,我需要根据一些更高级别的 ID 遍历管道。所以不使用查询在这里不适用。我只是想知道我上面的建议是否会加快速度。
I am wondering what the best (read: fastest) way is to locate a record in a resultset of a Tquery SQL statement.
Until now i'm using TQuery.Locate and if i'm right that is the only statement that can be used to search in the resultset. So how can we optimize this?
I have a few ideas, but haven't had the time yet to compare them all on large datasets:
Let say we have a table with the following fields:
Create Table aTable (
ID int,
Name1 varchar(50),
Name2 varchar(50));
And the following query:
SELECT ID, Name1, Name2 from aTable
We want to locate a record by its ID in the resulting set
- Will a locate be faster if aTable has an index on ID?
- Will a locate be faster if I add "Order By ID" to the SQL statement?
Any ideas on this?
[Edit] To clarify the use of this: The query is executed by a Reportbuilder Dataview, and then made available through a datapipeline (which is the TQuery.Dataset). In the custom report I need to travel the pipeline based on some higher level ID. So NOT using a query is not applicable here. I'm just wondering if any of my suggestions above would speed things up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是一些您可能会觉得有用的提示
如果您有大型数据集的主/详细信息,请不要获取所有详细信息记录 - 换句话说 - 不要使用本地主要细节- 让数据库引擎只为您提供所需的记录。
Here are some tips that you might find helpful
If you have master / detail on large datasets DO NOT fetch all details record - in other words - don't use local master detail - let the DB engine gives you only the desired records.
如果您需要在大型数据集上经常执行此操作,则最好将数据集转换为记录数组并在索引字段上实现一些自定义搜索例程。
If you need to do this very often on a large dataset you will be better of converting the dataset to an record array and implement some custom search routines on the index field.
您应该添加要搜索的 ID,以便仅检索预期的行。
在 ReportBuilder 数据视图中,转到查询设计器,然后转到搜索选项卡,然后添加带有“自动搜索”的字段设置选项。
然后转到 ReportBuilder Calc 视图,从菜单中选择“查看/事件”。单击“报告对象”树视图中的“报告”根项,然后双击“OnGetAutoSearchValues”并检索预期值:
You should add an ID to search for, in order to retrieve only the expected row.
In the ReportBuilder Data view, go to the Query Designer, then to the Search tab, then add a field setting with the "Auto search" option.
Then go to the ReportBuilder Calc view, select "View / Events" from the menu. Click on the Report root item in the "Report object" tree view, then double-click on "OnGetAutoSearchValues" and retrieve the expected value: