Search code examples
sqldelphifilterdelphi-7advantage-database-server

DBGrid Filter, Delphi.


I've recently delved into the world of Delphi, for my current mini project I'm obtaining data via an SQL query and then using the filter property to display exactly what I want.

I discovered the filter by mistake and now prefer it instead of making multiple connections or calls to the database. For example, I'm returning a person object that may own many cars, the app has a check box and depending which one is selected it will update the filter to display only he cars that are blue or pink or whatever.

As far as I understand it, the filter works like a where clause but on the Dataset that is returned from the initial query. So, my question is: Is it faster to use the filter property when working with a small dataset in this manner and I am completely wrong in thinking that Dataset is returned, stored and then the filter is applied to that as opposed to constantly being updated?

I've looked online, the resources do lead me to believe that it is more efficient but I'm still unsure. Thanks for any help.


Solution

  • A filter on a dataset does indeed work (or at least behave) like a WHERE clause, and in some cases can be very fast.

    The issues with depending on filters are:

    1. Increased network traffic. You're moving considerably more data from the server to the client that isn't needed, because you're just filtering it out anyway.

    2. Filters are applied to the data row-by-row. A WHERE clause can be optimized by the server to be all (or at least partially) based on existing indexes, whereas the client does not have those indexes available.

    3. Increased memory and CPU use on the client to maintain data it isn't using in memory and to process the rows for filtering.

    4. Data updated by other users or processes is not visible to the client app, as you're now working with all of the data in local memory and not refreshing from the server.

    IMO, using a filter on all but a trivial dataset isn't a good option, and if the amount of data is that small you can move the entire dataset into a TClientDataSet and keep it in memory yourself anyway. Like every other optimization being considered, the proper answer depends on the needs of your application and the actual data in question, and should be benchmarked using that criteria to determine what is actually the better solution.