I'm working on a Delphi XE-based desktop program with database, and I have noticed that it will be too big for the program to load an entire table before display it to user per page every time they want to browse for products, but it will be also inefficient for the program to load and reload each page as the user browse like in browser. I want to ask, is there any way for Delphi to cache a query call result, so that for the same query, the database component doesn't query again to the database, but return for the cache instead?
This is for scenario like this:
The program loads the first 20 lines for page 1 from database using query.
SELECT * FROM tbl_product_master LIMIT 0,20;
Then the user hit next page. The program loads the next 20 lines for page 2 from database using query.
SELECT * FROM tbl_product_master LIMIT 21,40;
Then the user hit previous page. The program attempts to load the first 20 lines again.
SELECT * FROM tbl_product_master LIMIT 0,20;
but because the database component knows I have already call this query before, and it already saves the result in the cache, it immediately returns the result from the cache, not firing the query again to the database, thus resulting in faster program.
Is there such a component or configuration exist for Delphi XE? I just need a nudge toward the right direction. Thank you.
There are different ways in Delphi to limit the number of records loaded by a query - it also depends on what database you're using and what data access components.
Some databases will be able to return you only a given number of records until you ask for more, if the Delphi data access library you use also supports that feature and can set it. Some can also cache query results, and avoid to re-execute a query if possible. Already fetched record will be retained.
The TClientDataset + Provider combo offers a similar functionality, independent from the database. It can load data incrementally, and it will cache already fetched rows. It requires a little more code to work, though.
In a native Windows application, you usually don't need the "paging" metaphor web UI uses, because it is far easier to use some control that can scroll up and down while caching data in local memory or disk.
But be aware that: