Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to locate record in TQuery resultset

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.

like image 695
Bascy Avatar asked Aug 08 '11 11:08

Bascy


People also ask

How do you locate a record in a database table by searching for specific data within the table?

Open the table or form, and then click the field that you want to search. On the Home tab, in the Find group, click Find, or press CTRL+F. The Find and Replace dialog box appears, with the Find tab selected. In the Find What box, type the value for which you want to search.

Which option is used to see specific set of records?

You can also search for a specific record in a table or form by using the Find option. This is an effective choice for locating a specific record when the record that you want to locate satisfies specific criteria, such as search terms, and comparison operators, such as "equals" or "contains".

How do I get the most recent record in SQL?

Use the aggregate MAX(signin) grouped by id. This will list the most recent signin for each id . To get the whole single record, perform an INNER JOIN against a subquery which returns only the MAX(signin) per id.


1 Answers

Here are some tips that you might find helpful

  • Use Locate() method only on indexed columns
  • Use "order by" ONLY on indexed columns
  • Use prepare before opening query
  • Use DisableControls / EnableControls
  • Use Ascending / Descending index (or both) depending on your needs
  • Give a try to FastReports

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.

like image 96
JustMe Avatar answered Nov 15 '22 11:11

JustMe