DBF, Dates and Advantage speed

I’ve optimized a query to work better with dates on Advantage Database Server using DBF’s and I figured I ought to make a note of what I did so that I don’t forget the next time I need to.  Of course I didn’t realise it was the dates that was causing the problems when I started, but that certainly appears to have been the case.

With DBF’s the date fields are stored as text so the trick when pulling the dates into another table is to convert them to text so that there’s no conversion take place.  My original query to take a subset of a table was like this,

select top 30 rowid as row, [date], [time] into #records from whaudit order by [date], [time]

This took 1 minute with a fairly large dataset, even with an index of DToS(Date) + Time.  By changing that to,

select top 30 rowid as row, convert([date], SQL_CHAR) as dt, [time] into #records from whaudit order by [date], [time]

It goes down to 3 seconds to execute on the same table.  The use of the temporary table is to implement a paged query so that I can take a subset of records, and the rowid in ADS SQL is particularly useful for selecting the exact records.

The one weird thing is that if you alias the converted field [date] the query goes back to executing in a minute.  I can’t quite figure that out, whether it’s because it’s because it’s the same column name as in the order by or because it’s a keyword but it gets a different execution plan and is really slow.  This might not happen to all tables, but it certainly happens to ours with it’s specific index setup.

select top 30 rowid as row, convert([date], SQL_CHAR) as [date], [time] into #records from whaudit order by [date], [time]