Subject Re: [firebird-support] Re: Optimization Question
Author Dan Wilson
> First, I'd try creating a separate index for Action_Time.

I've started the process of creating a new index, but it'll take a while, as there are 38 million rows in that table.
However, it seemed to me that an index on action_time alone would be very bad given that the table contains that many rows and the relatively low hit rate I expect to get (a few tens of thousands of matches on that spread_id at most, I would think).

> If that doesn't help you could try

> select first 1 SpreadAction_ID from SpreadActionTable S
> where Spread_ID = 9007 AND ( Status IS NULL OR Status != 2 )
> AND Action_Time BETWEEN 1073030400 AND 1075535999
> and not exists(select * from SpreadActionTable S2
> where s2.Spread_ID = 9007 AND ( s2.Status IS NULL OR s2.Status != 2 )
> and s2.Action_Time < s.Action_Time)

Hmmm. That second (sub)query looks suspicious to me. If I have a row in the table with action_time = 1073010000 (below my "low action_time", and another row with action_time = 1073030401, won't the second query find the 1073010000 row, thereby returning true to "exists", and thus causing the 1073030401 row to be discarded rather than returned?

Thanks for your help,

Dan.