David Rifki
Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Rookie Question about TQuery and TDBGrid
On Sun, 2 May 1999 03:31:23 +0900, "blacklungs" <nom...@nomail.com> wrote: Quote>> Select some fields from your table, don't load them all. >But what if I need all the fields that I am selecting? >What I am saying is that I can't change the dataset that I want to get. >I am just wondering if there is a faster way to get the dataset. >Maybe I should rephrase my original question: >Task: > 1. I need to select columns a, b, c, x, y, z from a table K. > 2. The table has 10000 records, and I need to display ALL of them in a >scrollable control. >Problem: > 1. The method of using TQuery and TDBGrid is too slow. > (if I execute TQuery.Last right after TQuery.Open, then it's too slow in >the beginning, and if I don't it's slow when the user is scrolling/browsing >the records) >Question: > 1. Find a solution that satisfies the Task at hand or find a way to make >the current method faster.
There are several things you might try. Each of these works well in some cases, but none of them solve all problems. The tradeoffs depend on the database you're using and the nature of the data. 1. Use a TTable instead of a TQuery. This often works well if you're reading most of the columns of a table. Not usable if you're trying to order the results in a way that doesn't correspond to an index. 2. Use a TTable connected to a view defined on the database server. Like method 1, but lets you limit the set of columns retrieved. Works, of course, only with a SQL-based database. 3. Use a TQuery which retrieves only the primary key from each record. Add lookup or calculated fields for each field you want to display in the grid, retrieving the data from another dataset pointing to the same table. The result can't be made editable in the grid, though if you use calculated fields and position the auxiliary dataset in the OnCalcFields event handler, you can hook single-record data-aware controls to it. If you need to search in the dataset, use a query or stored procedure to retrieve a primary key for the record you're looking for, then a Locate to reposition the keyset dataset. I've often claimed that tables give better performance than queries with SQL engines, but method 3 gives the best performance I've seen for general browsing, if you're willing to deal with the extra complexity. -- "Oh, shootings. Yes, but that doesn't mean Americans are more {*word*268} than other people. We're just better shots."
|