Subject Re: High selectivity
Author Fabrice Aeschbacher
Hi Jonathan,

> >Suppose for instance that I have a STATE field on a table, that has
two
> >possible values, 0 and 1.
> >All new records start out in STATE 0, and after a while, they end
up in
> >STATE 1. Obviously, the number of records in STATE 0 will remain
roughly
> >constant, whereas the number of records in STATE 1 will be
continually
> >growing. If (as it is highly likely), I know in advance that I will
> >almost never need to select all records in STATE 1, but very
frequently
> >all records in STATE 0, would it not be advantageous to define an
index?

I would write following before trigger:

CREATE GENERATOR STATE_GEN;

CREATE TRIGGER MY_TABLE_BI FOR MY_TABLE
ACTIVE BEFORE INSERT, UPDATE
AS
BEGIN
IF ( NEW.STATE IS NULL ) THEN NEW.STATE = 0;
IF ( NEW.STATE = 1 ) THEN NEW.STATE = GEN_ID( STATE_GEN, 1 );
END !!

(if you are using FB 1.0, make two different triggers for insert /
update)

So the STATE column will allways have either a unique non-zero value,
or a zero value. As long as the number of records having STATE=0 is
not too big, you can create an inder on that column.

HTH,
Fabrice