Problem with cds.RecordCount

3.2k Views Asked by At

Is there a problem with cds.RecordCount ?

I usually use it to determine if I have any records in a query.

But talking with a college he told that there is a performance penalty to that!

I have made some test and nothing major came up.

So is there a performance penalty or any other problem with RecordCount?!

If so, what is the best way to check if there are some records in a query?

Thanks

2

There are 2 best solutions below

3
On BEST ANSWER

If you're using TClientDataSet (as your 'cds' seems to imply) with PacketRecords set to -1 (default) or FetchOnDemand set to False there is no difference since the client dataset receives and loads all data into memory at once. The difference would be noticable with other datasets which fetch data on demand as you advance the cursor, using RecordCount would fetch all data first. In such cases it's better to use the dataset's EOF property after opening - if all you want to know is whether the result set was empty or not.

0
On

The best solution would be to execute a SELECT COUNT(*) query.

If you need all records locally, you should set the FetchOnDemand property to False or you can call cds.Last right before cds.RecordCount.

There is also a cds.IsEmpty method if the purpose is to check whether the dataset contains records...